1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-15 18:56:28 +00:00

Fool proofing file sorting

This commit is contained in:
Paweł Jastrzębski
2015-02-21 18:25:55 +01:00
parent 1ec07fe4ec
commit 44bdc0245b
3 changed files with 16 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ from copy import copy
from glob import glob from glob import glob
from json import loads from json import loads
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from re import split, sub from re import sub
from stat import S_IWRITE, S_IREAD, S_IEXEC from stat import S_IWRITE, S_IREAD, S_IEXEC
from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED
from tempfile import mkdtemp from tempfile import mkdtemp
@@ -41,7 +41,7 @@ try:
from PyQt5 import QtCore from PyQt5 import QtCore
except ImportError: except ImportError:
QtCore = None QtCore = None
from .shared import md5Checksum, getImageFileName, walkLevel, saferReplace from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, saferReplace
from . import comic2panel from . import comic2panel
from . import image from . import image
from . import cbxarchive from . import cbxarchive
@@ -422,14 +422,9 @@ def buildEPUB(path, chapterNames, tomeNumber):
"}", "}",
]) ])
f.close() f.close()
# Ensure we're sorting dirs, files naturally
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in split('([0-9]+)', key)]
for (dirpath, dirnames, filenames) in walk(os.path.join(path, 'OEBPS', 'Images')): for (dirpath, dirnames, filenames) in walk(os.path.join(path, 'OEBPS', 'Images')):
chapter = False chapter = False
# Traverse dirs in a sorted manner dirnames, filenames = walkSort(dirnames, filenames)
dirnames.sort(key=lambda name: alphanum_key(name.lower()))
filenames.sort(key=lambda name: alphanum_key(name.lower()))
for afile in filenames: for afile in filenames:
filename = getImageFileName(afile) filename = getImageFileName(afile)
if '-kcc-hq' not in filename[0]: if '-kcc-hq' not in filename[0]:
@@ -718,8 +713,7 @@ def sanitizeTree(filetree):
def sanitizeTreeKobo(filetree): def sanitizeTreeKobo(filetree):
pageNumber = 0 pageNumber = 0
for root, dirs, files in walk(filetree): for root, dirs, files in walk(filetree):
files.sort() dirs, files = walkSort(dirs, files)
dirs.sort()
for name in files: for name in files:
splitname = os.path.splitext(name) splitname = os.path.splitext(name)
slugified = str(pageNumber).zfill(5) slugified = str(pageNumber).zfill(5)

View File

@@ -25,7 +25,7 @@ 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 scandir import walk
from .shared import getImageFileName, walkLevel from .shared import getImageFileName, walkLevel, walkSort
try: try:
from PyQt5 import QtCore from PyQt5 import QtCore
except ImportError: except ImportError:
@@ -248,6 +248,7 @@ def main(argv=None, qtGUI=None):
mergeWorkerPool = Pool() mergeWorkerPool = Pool()
mergeWork.append([options.targetDir]) mergeWork.append([options.targetDir])
for root, dirs, files in walk(options.targetDir, False): for root, dirs, files in walk(options.targetDir, False):
dirs, files = walkSort(dirs, files)
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)])

View File

@@ -24,6 +24,7 @@ from time import sleep
from shutil import rmtree, move from shutil import rmtree, move
from tempfile import mkdtemp from tempfile import mkdtemp
from zipfile import ZipFile, ZIP_DEFLATED from zipfile import ZipFile, ZIP_DEFLATED
from re import split
try: try:
from scandir import walk from scandir import walk
except ImportError: except ImportError:
@@ -53,11 +54,20 @@ def getImageFileName(imgfile):
return [name, ext] return [name, ext]
def walkSort(dirnames, filenames):
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in split('([0-9]+)', key)]
dirnames.sort(key=lambda name: alphanum_key(name.lower()))
filenames.sort(key=lambda name: alphanum_key(name.lower()))
return dirnames, filenames
def walkLevel(some_dir, level=1): 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 walk(some_dir): for root, dirs, files in walk(some_dir):
dirs, files = walkSort(dirs, files)
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: