1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-19 08:23:40 +01:00

Removed facade on os.walk.

This commit is contained in:
2020-10-13 20:32:05 +02:00
parent b44926a6eb
commit 22fed6f4d3
9 changed files with 53 additions and 55 deletions

View File

@@ -27,13 +27,6 @@ class CurrentDir(object):
pass
def walk(dir):
"""A nice interface to os.walk"""
for record in os.walk(dir):
for f in record[-1]:
yield os.path.join(record[0], f)
def entity_to_unicode(match, exceptions=[], encoding='cp1252',
result_exceptions={}):
"""

View File

@@ -265,7 +265,6 @@ class EPUBInput(InputFormatPlugin):
def convert(self, stream, options, file_ext, log, accelerators):
from ebook_converter.utils.zipfile import ZipFile
from ebook_converter import walk
from ebook_converter.ebooks import DRMError
_path_or_stream = getattr(stream, 'name', 'stream')
@@ -281,11 +280,13 @@ class EPUBInput(InputFormatPlugin):
encfile = os.path.abspath(os.path.join('META-INF', 'encryption.xml'))
opf = self.find_opf()
if opf is None:
for f in walk('.'):
if f.lower().endswith('.opf') and '__MACOSX' not in f and \
not os.path.basename(f).startswith('.'):
opf = os.path.abspath(f)
break
for root, _, fnames in os.walk('.'):
for f in fnames:
f = os.path.join(root, f)
if f.lower().endswith('.opf') and '__MACOSX' not in f and \
not os.path.basename(f).startswith('.'):
opf = os.path.abspath(f)
break
if opf is None:
raise ValueError('%s is not a valid EPUB file (could not find '

View File

@@ -2,12 +2,6 @@ import os
from ebook_converter.customize.conversion import InputFormatPlugin, OptionRecommendation
from ebook_converter.constants_old import numeric_version
from ebook_converter import walk
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
class RecipeDisabled(Exception):
@@ -141,9 +135,11 @@ class RecipeInput(InputFormatPlugin):
if f.endswith('.opf'):
return os.path.abspath(f)
for f in walk('.'):
if f.endswith('.opf'):
return os.path.abspath(f)
for root, _, fnames in os.walk('.'):
for f in fnames:
f = os.path.join(root, f)
if f.endswith('.opf'):
return os.path.abspath(f)
def postprocess_book(self, oeb, opts, log):
if self.recipe_object is not None:

View File

@@ -1,6 +1,6 @@
import os
from ebook_converter import _ent_pat, walk, xml_entity_to_unicode
from ebook_converter import _ent_pat, xml_entity_to_unicode
from ebook_converter.customize.conversion import InputFormatPlugin, OptionRecommendation
@@ -157,10 +157,12 @@ class TXTInput(InputFormatPlugin):
zf = ZipFile(stream)
zf.extractall('.')
for x in walk('.'):
if os.path.splitext(x)[1].lower() in ('.txt', '.text'):
with open(x, 'rb') as tf:
txt += tf.read() + b'\n\n'
for root, _, fnames in os.walk('.'):
for x in fnames:
x = os.path.join(root, x)
if os.path.splitext(x)[1].lower() in ('.txt', '.text'):
with open(x, 'rb') as tf:
txt += tf.read() + b'\n\n'
else:
if getattr(stream, 'name', None):
base_dir = os.path.dirname(stream.name)

View File

@@ -5,7 +5,6 @@ import sys
from lxml import etree
from ebook_converter import walk
from ebook_converter.ebooks.metadata import authors_to_sort_string
from ebook_converter.ebooks.metadata import string_to_authors
from ebook_converter.ebooks.metadata.book.base import Metadata
@@ -113,9 +112,11 @@ class DOCX(object):
extractall(stream, self.tdir)
self.names = {}
for f in walk(self.tdir):
name = os.path.relpath(f, self.tdir).replace(os.sep, '/')
self.names[name] = f
for root, _, fnames in os.walk(self.tdir):
for f in fnames:
f = os.path.join(root, f)
name = os.path.relpath(f, self.tdir).replace(os.sep, '/')
self.names[name] = f
def exists(self, name):
return name in self.names

View File

@@ -3,23 +3,24 @@ import shutil
from lxml import etree
from ebook_converter import walk
from ebook_converter.utils.zipfile import ZipFile
from ebook_converter.utils.xml_parse import safe_xml_fromstring
def pretty_all_xml_in_dir(path):
for f in walk(path):
if f.endswith('.xml') or f.endswith('.rels'):
with open(f, 'r+b') as stream:
raw = stream.read()
if raw:
root = safe_xml_fromstring(raw)
stream.seek(0)
stream.truncate()
stream.write(etree.tostring(root, pretty_print=True,
encoding='utf-8',
xml_declaration=True))
for root, _, fnames in os.walk(path):
for f in fnames:
f = os.path.join(root, f)
if f.endswith('.xml') or f.endswith('.rels'):
with open(f, 'r+b') as stream:
raw = stream.read()
if raw:
root = safe_xml_fromstring(raw)
stream.seek(0)
stream.truncate()
stream.write(etree.tostring(root, pretty_print=True,
encoding='utf-8',
xml_declaration=True))
def do_dump(path, dest):

View File

@@ -13,7 +13,7 @@ from odf.opendocument import load as odLoad
from odf.draw import Frame as odFrame, Image as odImage
from odf.namespaces import TEXTNS as odTEXTNS
from ebook_converter import CurrentDir, walk
from ebook_converter import CurrentDir
from ebook_converter.ebooks.oeb.base import _css_logger
from ebook_converter.polyglot.builtins import as_bytes
@@ -296,8 +296,9 @@ class Extract(ODF2XHTML):
zf = ZipFile(stream, 'r')
self.extract_pictures(zf)
opf = OPFCreator(os.path.abspath(os.getcwd()), mi)
opf.create_manifest([(os.path.abspath(f2), None) for f2 in
walk(os.getcwd())])
opf.create_manifest([(os.path.abspath(os.path.join(r, f2)), None)
for r, _, fnames in os.walk(os.getcwd())
for f2 in fnames])
opf.create_spine([os.path.abspath('index.xhtml')])
with open('metadata.opf', 'wb') as f:
opf.render(f)

View File

@@ -16,7 +16,7 @@ import css_parser
from lxml import etree
from ebook_converter import constants as const
from ebook_converter import CurrentDir, walk
from ebook_converter import CurrentDir
from ebook_converter.customize.ui import plugin_for_input_format, plugin_for_output_format
from ebook_converter.ebooks import escape_xpath_attr
from ebook_converter.ebooks.chardet import xml_to_unicode
@@ -1153,12 +1153,14 @@ class EpubContainer(Container):
# Ensure all filenames are in NFC normalized form
# has no effect on HFS+ filesystems as they always store filenames
# in NFD form
for filename in walk(self.root):
n = unicodedata.normalize('NFC', filename)
if n != filename:
s = filename + 'suff1x'
os.rename(filename, s)
os.rename(s, n)
for root, _, fnames in os.walk(self.root):
for filename in fnames:
filename = os.path.join(root, filename)
n = unicodedata.normalize('NFC', filename)
if n != filename:
s = filename + 'suff1x'
os.rename(filename, s)
os.rename(s, n)
container_path = join(self.root, 'META-INF', 'container.xml')
if not exists(container_path):

View File

@@ -2,7 +2,6 @@ import os
from collections import defaultdict
from threading import Thread
from ebook_converter import walk
from ebook_converter.constants_old import DEBUG
from ebook_converter.constants_old import filesystem_encoding
from ebook_converter.utils.fonts.metadata import FontMetadata, UnsupportedFont
@@ -296,7 +295,9 @@ class FontScanner(Thread):
if not os.path.isdir(folder):
continue
try:
files = tuple(walk(folder))
files = tuple([os.path.join(root, f)
for root, _, fnames in os.walk(folder)
for f in fnames])
except EnvironmentError as e:
if DEBUG:
print(f'Failed to walk font folder: {folder}, {e}')