1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-21 05:31:49 +00:00

Replaced os.walk

This commit is contained in:
Paweł Jastrzębski
2015-01-21 18:19:54 +01:00
parent 12684d6562
commit 5a36a13105
4 changed files with 22 additions and 12 deletions

View File

@@ -48,11 +48,12 @@ You can find the latest released binary at the following links:
- [Pillow](http://pypi.python.org/pypi/Pillow/) 2.7.0+ - [Pillow](http://pypi.python.org/pypi/Pillow/) 2.7.0+
- [psutil](https://pypi.python.org/pypi/psutil) 2.0+ - [psutil](https://pypi.python.org/pypi/psutil) 2.0+
- [python-slugify](http://pypi.python.org/pypi/python-slugify) 0.1.0+ - [python-slugify](http://pypi.python.org/pypi/python-slugify) 0.1.0+
- [scandir](https://pypi.python.org/pypi/scandir) 0.9+
On Debian based distributions these two commands should install all dependencies: On Debian based distributions these two commands should install all dependencies:
``` ```
sudo apt-get install python3 python3-dev python3-pip python3-pyqt5 libpng-dev libjpeg-dev p7zip-full unrar sudo apt-get install python3 python3-dev python3-pip python3-pyqt5 libpng-dev libjpeg-dev p7zip-full unrar
sudo pip3 install pillow python-slugify psutil sudo pip3 install pillow python-slugify psutil scandir
``` ```
### For freezing code: ### For freezing code:

View File

@@ -37,6 +37,7 @@ from slugify import slugify as slugifyExt
from PIL import Image from PIL import Image
from subprocess import STDOUT, PIPE from subprocess import STDOUT, PIPE
from psutil import Popen, virtual_memory from psutil import Popen, virtual_memory
from scandir import walk
try: try:
from PyQt5 import QtCore from PyQt5 import QtCore
except ImportError: except ImportError:
@@ -412,7 +413,7 @@ def buildEPUB(path, chapterNames, tomeNumber):
"}", "}",
]) ])
f.close() f.close()
for (dirpath, dirnames, filenames) in os.walk(os.path.join(path, 'OEBPS', 'Images')): for (dirpath, dirnames, filenames) in walk(os.path.join(path, 'OEBPS', 'Images')):
chapter = False chapter = False
for afile in filenames: for afile in filenames:
filename = getImageFileName(afile) filename = getImageFileName(afile)
@@ -463,7 +464,7 @@ def imgDirectoryProcessing(path):
options.imgPurgeIndex = [] options.imgPurgeIndex = []
work = [] work = []
pagenumber = 0 pagenumber = 0
for (dirpath, dirnames, filenames) in os.walk(path): for (dirpath, dirnames, filenames) in walk(path):
for afile in filenames: for afile in filenames:
pagenumber += 1 pagenumber += 1
work.append([afile, dirpath, options]) work.append([afile, dirpath, options])
@@ -698,7 +699,7 @@ def getCoversFromMCB(mangaID):
def getDirectorySize(start_path='.'): def getDirectorySize(start_path='.'):
total_size = 0 total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path): for dirpath, dirnames, filenames in walk(start_path):
for f in filenames: for f in filenames:
fp = os.path.join(dirpath, f) fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp) total_size += os.path.getsize(fp)
@@ -707,7 +708,7 @@ def getDirectorySize(start_path='.'):
def sanitizeTree(filetree): def sanitizeTree(filetree):
chapterNames = {} chapterNames = {}
for root, dirs, files in os.walk(filetree, False): for root, dirs, files in walk(filetree, False):
for name in files: for name in files:
splitname = os.path.splitext(name) splitname = os.path.splitext(name)
slugified = slugify(splitname[0]) slugified = slugify(splitname[0])
@@ -733,7 +734,7 @@ def sanitizeTree(filetree):
def sanitizeTreeKobo(filetree): def sanitizeTreeKobo(filetree):
pageNumber = 0 pageNumber = 0
for root, dirs, files in os.walk(filetree): for root, dirs, files in walk(filetree):
files.sort() files.sort()
dirs.sort() dirs.sort()
for name in files: for name in files:
@@ -750,7 +751,7 @@ def sanitizeTreeKobo(filetree):
def sanitizePermissions(filetree): def sanitizePermissions(filetree):
for root, dirs, files in os.walk(filetree, False): for root, dirs, files in walk(filetree, False):
for name in files: for name in files:
os.chmod(os.path.join(root, name), S_IWRITE | S_IREAD) os.chmod(os.path.join(root, name), S_IWRITE | S_IREAD)
for name in dirs: for name in dirs:
@@ -882,7 +883,7 @@ def splitProcess(path, mode):
def detectCorruption(tmpPath, orgPath): def detectCorruption(tmpPath, orgPath):
for root, dirs, files in os.walk(tmpPath, False): for root, dirs, files in walk(tmpPath, False):
for name in files: for name in files:
if getImageFileName(name) is not None: if getImageFileName(name) is not None:
path = os.path.join(root, name) path = os.path.join(root, name)
@@ -949,7 +950,7 @@ def makeZIP(zipFilename, baseDir, isEPUB=False):
zipOutput = ZipFile(zipFilename, 'w', ZIP_DEFLATED) zipOutput = ZipFile(zipFilename, 'w', ZIP_DEFLATED)
if isEPUB: if isEPUB:
zipOutput.writestr('mimetype', 'application/epub+zip', ZIP_STORED) zipOutput.writestr('mimetype', 'application/epub+zip', ZIP_STORED)
for dirpath, dirnames, filenames in os.walk(baseDir): for dirpath, dirnames, filenames in walk(baseDir):
for name in filenames: for name in filenames:
path = os.path.normpath(os.path.join(dirpath, name)) path = os.path.normpath(os.path.join(dirpath, name))
aPath = os.path.normpath(os.path.join(dirpath.replace(baseDir, ''), name)) aPath = os.path.normpath(os.path.join(dirpath.replace(baseDir, ''), name))

View File

@@ -24,6 +24,7 @@ from shutil import rmtree, copytree, move
from optparse import OptionParser, OptionGroup from optparse import OptionParser, OptionGroup
from multiprocessing import Pool from multiprocessing import Pool
from PIL import Image, ImageStat, ImageOps from PIL import Image, ImageStat, ImageOps
from scandir import walk
from .shared import getImageFileName, walkLevel from .shared import getImageFileName, walkLevel
try: try:
from PyQt5 import QtCore from PyQt5 import QtCore
@@ -246,7 +247,7 @@ def main(argv=None, qtGUI=None):
mergeWorkerOutput = [] mergeWorkerOutput = []
mergeWorkerPool = Pool() mergeWorkerPool = Pool()
mergeWork.append([options.targetDir]) mergeWork.append([options.targetDir])
for root, dirs, files in os.walk(options.targetDir, False): for root, dirs, files in walk(options.targetDir, False):
for directory in dirs: for directory in dirs:
directoryNumer += 1 directoryNumer += 1
mergeWork.append([os.path.join(root, directory)]) mergeWork.append([os.path.join(root, directory)])
@@ -264,7 +265,7 @@ def main(argv=None, qtGUI=None):
rmtree(options.targetDir, True) rmtree(options.targetDir, True)
raise RuntimeError("One of workers crashed. Cause: " + mergeWorkerOutput[0]) raise RuntimeError("One of workers crashed. Cause: " + mergeWorkerOutput[0])
print("\nSplitting images...") print("\nSplitting images...")
for root, dirs, files in os.walk(options.targetDir, False): for root, dirs, files in walk(options.targetDir, False):
for name in files: for name in files:
if getImageFileName(name) is not None: if getImageFileName(name) is not None:
pagenumber += 1 pagenumber += 1

View File

@@ -20,6 +20,7 @@ import os
from hashlib import md5 from hashlib import md5
from html.parser import HTMLParser from html.parser import HTMLParser
from distutils.version import StrictVersion from distutils.version import StrictVersion
from scandir import walk
class HTMLStripper(HTMLParser): class HTMLStripper(HTMLParser):
@@ -49,7 +50,7 @@ def walkLevel(some_dir, level=1):
some_dir = some_dir.rstrip(os.path.sep) some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir) assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep) num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir): for root, dirs, files in walk(some_dir):
yield root, dirs, files yield root, dirs, files
num_sep_this = root.count(os.path.sep) num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this: if num_sep + level <= num_sep_this:
@@ -102,6 +103,12 @@ def dependencyCheck(level):
missing.append('Pillow 2.7.0+') missing.append('Pillow 2.7.0+')
except ImportError: except ImportError:
missing.append('Pillow 2.7.0+') missing.append('Pillow 2.7.0+')
try:
from scandir import __version__ as scandirVersion
if StrictVersion('0.9') > StrictVersion(scandirVersion):
missing.append('scandir 0.9+')
except ImportError:
missing.append('scandir 0.9+')
if len(missing) > 0: if len(missing) > 0:
print('ERROR: ' + ', '.join(missing) + ' is not installed!') print('ERROR: ' + ', '.join(missing) + ' is not installed!')
exit(1) exit(1)