mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Close a bunch of file objects explicitly
This commit is contained in:
parent
cf44d16964
commit
cff4d491ea
@ -611,7 +611,8 @@ class Container(ContainerBase): # {{{
|
|||||||
:param decode: If True and the file has a text based MIME type, decode it and return a unicode object instead of raw bytes.
|
:param decode: If True and the file has a text based MIME type, decode it and return a unicode object instead of raw bytes.
|
||||||
:param normalize_to_nfc: If True the returned unicode object is normalized to the NFC normal form as is required for the EPUB and AZW3 file formats.
|
:param normalize_to_nfc: If True the returned unicode object is normalized to the NFC normal form as is required for the EPUB and AZW3 file formats.
|
||||||
'''
|
'''
|
||||||
ans = self.open(name).read()
|
with self.open(name) as nf:
|
||||||
|
ans = nf.read()
|
||||||
mime = self.mime_map.get(name, guess_type(name))
|
mime = self.mime_map.get(name, guess_type(name))
|
||||||
if decode and (mime in OEB_STYLES or mime in OEB_DOCS or mime == 'text/plain' or mime[-4:] in {'+xml', '/xml'}):
|
if decode and (mime in OEB_STYLES or mime in OEB_DOCS or mime == 'text/plain' or mime[-4:] in {'+xml', '/xml'}):
|
||||||
ans = self.decode(ans, normalize_to_nfc=normalize_to_nfc)
|
ans = self.decode(ans, normalize_to_nfc=normalize_to_nfc)
|
||||||
@ -1205,7 +1206,8 @@ class EpubContainer(Container):
|
|||||||
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):
|
||||||
raise InvalidEpub('No META-INF/container.xml in epub')
|
raise InvalidEpub('No META-INF/container.xml in epub')
|
||||||
container = safe_xml_fromstring(open(container_path, 'rb').read())
|
with open(container_path, 'rb') as cf:
|
||||||
|
container = safe_xml_fromstring(cf.read())
|
||||||
opf_files = container.xpath((
|
opf_files = container.xpath((
|
||||||
r'child::ocf:rootfiles/ocf:rootfile'
|
r'child::ocf:rootfiles/ocf:rootfile'
|
||||||
'[@media-type="%s" and @full-path]'%guess_type('a.opf')
|
'[@media-type="%s" and @full-path]'%guess_type('a.opf')
|
||||||
|
@ -52,7 +52,8 @@ def builtin_dictionaries():
|
|||||||
if _builtins is None:
|
if _builtins is None:
|
||||||
dics = []
|
dics = []
|
||||||
for lc in glob.glob(os.path.join(P('dictionaries', allow_user_override=False), '*/locales')):
|
for lc in glob.glob(os.path.join(P('dictionaries', allow_user_override=False), '*/locales')):
|
||||||
locales = list(filter(None, open(lc, 'rb').read().decode('utf-8').splitlines()))
|
with open(lc, 'rb') as lcf:
|
||||||
|
locales = list(filter(None, lcf.read().decode('utf-8').splitlines()))
|
||||||
locale = locales[0]
|
locale = locales[0]
|
||||||
base = os.path.dirname(lc)
|
base = os.path.dirname(lc)
|
||||||
dics.append(Dictionary(
|
dics.append(Dictionary(
|
||||||
@ -67,7 +68,8 @@ def custom_dictionaries(reread=False):
|
|||||||
if _custom is None or reread:
|
if _custom is None or reread:
|
||||||
dics = []
|
dics = []
|
||||||
for lc in glob.glob(os.path.join(config_dir, 'dictionaries', '*/locales')):
|
for lc in glob.glob(os.path.join(config_dir, 'dictionaries', '*/locales')):
|
||||||
locales = list(filter(None, open(lc, 'rb').read().decode('utf-8').splitlines()))
|
with open(lc, 'rb') as cdf:
|
||||||
|
locales = list(filter(None, cdf.read().decode('utf-8').splitlines()))
|
||||||
try:
|
try:
|
||||||
name, locale, locales = locales[0], locales[1], locales[1:]
|
name, locale, locales = locales[0], locales[1], locales[1:]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
@ -40,14 +40,24 @@ def identify(src):
|
|||||||
''' Recognize file format and sizes. Returns format, width, height. width
|
''' Recognize file format and sizes. Returns format, width, height. width
|
||||||
and height will be -1 if not found and fmt will be None if the image is not
|
and height will be -1 if not found and fmt will be None if the image is not
|
||||||
recognized. '''
|
recognized. '''
|
||||||
width = height = -1
|
needs_close = False
|
||||||
|
|
||||||
if isinstance(src, unicode_type):
|
if isinstance(src, unicode_type):
|
||||||
stream = lopen(src, 'rb')
|
stream = lopen(src, 'rb')
|
||||||
|
needs_close = True
|
||||||
elif isinstance(src, bytes):
|
elif isinstance(src, bytes):
|
||||||
stream = ReadOnlyFileBuffer(src)
|
stream = ReadOnlyFileBuffer(src)
|
||||||
else:
|
else:
|
||||||
stream = src
|
stream = src
|
||||||
|
try:
|
||||||
|
return _identify(stream)
|
||||||
|
finally:
|
||||||
|
if needs_close:
|
||||||
|
stream.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _identify(stream):
|
||||||
|
width = height = -1
|
||||||
|
|
||||||
pos = stream.tell()
|
pos = stream.tell()
|
||||||
head = stream.read(HSIZE)
|
head = stream.read(HSIZE)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user