1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-11 20:15:46 +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 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', def entity_to_unicode(match, exceptions=[], encoding='cp1252',
result_exceptions={}): result_exceptions={}):
""" """

View File

@@ -265,7 +265,6 @@ class EPUBInput(InputFormatPlugin):
def convert(self, stream, options, file_ext, log, accelerators): def convert(self, stream, options, file_ext, log, accelerators):
from ebook_converter.utils.zipfile import ZipFile from ebook_converter.utils.zipfile import ZipFile
from ebook_converter import walk
from ebook_converter.ebooks import DRMError from ebook_converter.ebooks import DRMError
_path_or_stream = getattr(stream, 'name', 'stream') _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')) encfile = os.path.abspath(os.path.join('META-INF', 'encryption.xml'))
opf = self.find_opf() opf = self.find_opf()
if opf is None: if opf is None:
for f in walk('.'): for root, _, fnames in os.walk('.'):
if f.lower().endswith('.opf') and '__MACOSX' not in f and \ for f in fnames:
not os.path.basename(f).startswith('.'): f = os.path.join(root, f)
opf = os.path.abspath(f) if f.lower().endswith('.opf') and '__MACOSX' not in f and \
break not os.path.basename(f).startswith('.'):
opf = os.path.abspath(f)
break
if opf is None: if opf is None:
raise ValueError('%s is not a valid EPUB file (could not find ' 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.customize.conversion import InputFormatPlugin, OptionRecommendation
from ebook_converter.constants_old import numeric_version 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): class RecipeDisabled(Exception):
@@ -141,9 +135,11 @@ class RecipeInput(InputFormatPlugin):
if f.endswith('.opf'): if f.endswith('.opf'):
return os.path.abspath(f) return os.path.abspath(f)
for f in walk('.'): for root, _, fnames in os.walk('.'):
if f.endswith('.opf'): for f in fnames:
return os.path.abspath(f) f = os.path.join(root, f)
if f.endswith('.opf'):
return os.path.abspath(f)
def postprocess_book(self, oeb, opts, log): def postprocess_book(self, oeb, opts, log):
if self.recipe_object is not None: if self.recipe_object is not None:

View File

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

View File

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

View File

@@ -3,23 +3,24 @@ import shutil
from lxml import etree from lxml import etree
from ebook_converter import walk
from ebook_converter.utils.zipfile import ZipFile from ebook_converter.utils.zipfile import ZipFile
from ebook_converter.utils.xml_parse import safe_xml_fromstring from ebook_converter.utils.xml_parse import safe_xml_fromstring
def pretty_all_xml_in_dir(path): def pretty_all_xml_in_dir(path):
for f in walk(path): for root, _, fnames in os.walk(path):
if f.endswith('.xml') or f.endswith('.rels'): for f in fnames:
with open(f, 'r+b') as stream: f = os.path.join(root, f)
raw = stream.read() if f.endswith('.xml') or f.endswith('.rels'):
if raw: with open(f, 'r+b') as stream:
root = safe_xml_fromstring(raw) raw = stream.read()
stream.seek(0) if raw:
stream.truncate() root = safe_xml_fromstring(raw)
stream.write(etree.tostring(root, pretty_print=True, stream.seek(0)
encoding='utf-8', stream.truncate()
xml_declaration=True)) stream.write(etree.tostring(root, pretty_print=True,
encoding='utf-8',
xml_declaration=True))
def do_dump(path, dest): 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.draw import Frame as odFrame, Image as odImage
from odf.namespaces import TEXTNS as odTEXTNS 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.ebooks.oeb.base import _css_logger
from ebook_converter.polyglot.builtins import as_bytes from ebook_converter.polyglot.builtins import as_bytes
@@ -296,8 +296,9 @@ class Extract(ODF2XHTML):
zf = ZipFile(stream, 'r') zf = ZipFile(stream, 'r')
self.extract_pictures(zf) self.extract_pictures(zf)
opf = OPFCreator(os.path.abspath(os.getcwd()), mi) opf = OPFCreator(os.path.abspath(os.getcwd()), mi)
opf.create_manifest([(os.path.abspath(f2), None) for f2 in opf.create_manifest([(os.path.abspath(os.path.join(r, f2)), None)
walk(os.getcwd())]) for r, _, fnames in os.walk(os.getcwd())
for f2 in fnames])
opf.create_spine([os.path.abspath('index.xhtml')]) opf.create_spine([os.path.abspath('index.xhtml')])
with open('metadata.opf', 'wb') as f: with open('metadata.opf', 'wb') as f:
opf.render(f) opf.render(f)

View File

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

View File

@@ -2,7 +2,6 @@ import os
from collections import defaultdict from collections import defaultdict
from threading import Thread from threading import Thread
from ebook_converter import walk
from ebook_converter.constants_old import DEBUG from ebook_converter.constants_old import DEBUG
from ebook_converter.constants_old import filesystem_encoding from ebook_converter.constants_old import filesystem_encoding
from ebook_converter.utils.fonts.metadata import FontMetadata, UnsupportedFont from ebook_converter.utils.fonts.metadata import FontMetadata, UnsupportedFont
@@ -296,7 +295,9 @@ class FontScanner(Thread):
if not os.path.isdir(folder): if not os.path.isdir(folder):
continue continue
try: 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: except EnvironmentError as e:
if DEBUG: if DEBUG:
print(f'Failed to walk font folder: {folder}, {e}') print(f'Failed to walk font folder: {folder}, {e}')