From 444b3024736efb8c7f0602b9a246e54882d72d38 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 7 Feb 2016 09:34:22 +0530 Subject: [PATCH] More inheritable file handles int he device code --- src/calibre/devices/__init__.py | 2 +- src/calibre/devices/android/driver.py | 8 ++++---- src/calibre/devices/apple/driver.py | 8 ++++---- src/calibre/devices/cli.py | 6 +++--- src/calibre/devices/cybook/driver.py | 4 ++-- src/calibre/devices/hanvon/driver.py | 2 +- src/calibre/devices/idevice/libimobiledevice.py | 4 ++-- src/calibre/devices/kindle/apnx.py | 10 +++++----- src/calibre/devices/kindle/bookmark.py | 14 +++++++------- src/calibre/devices/kindle/driver.py | 4 ++-- src/calibre/devices/kobo/driver.py | 14 +++++++------- src/calibre/devices/misc.py | 8 ++++---- src/calibre/devices/mtp/driver.py | 2 +- src/calibre/devices/mtp/unix/sysfs.py | 2 +- src/calibre/devices/mtp/unix/upstream/update.py | 2 +- src/calibre/devices/nook/driver.py | 4 ++-- src/calibre/devices/prs505/driver.py | 4 ++-- src/calibre/devices/prs505/sony_cache.py | 12 ++++++------ src/calibre/devices/prst1/driver.py | 2 +- src/calibre/devices/scanner.py | 2 +- src/calibre/devices/smart_device_app/driver.py | 8 ++++---- src/calibre/devices/usbms/device.py | 2 +- 22 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/calibre/devices/__init__.py b/src/calibre/devices/__init__.py index 3f35612a9e..022d4f8df3 100644 --- a/src/calibre/devices/__init__.py +++ b/src/calibre/devices/__init__.py @@ -175,7 +175,7 @@ def debug(ioreg_to_tmp=False, buf=None, plugins=None, ioreg = 'IOREG Output\n'+ioreg out(' ') if ioreg_to_tmp: - open('/tmp/ioreg.txt', 'wb').write(ioreg) + lopen('/tmp/ioreg.txt', 'wb').write(ioreg) out('Dont forget to send the contents of /tmp/ioreg.txt') out('You can open it with the command: open /tmp/ioreg.txt') else: diff --git a/src/calibre/devices/android/driver.py b/src/calibre/devices/android/driver.py index eba585da6e..0393b0cc19 100644 --- a/src/calibre/devices/android/driver.py +++ b/src/calibre/devices/android/driver.py @@ -392,7 +392,7 @@ class WEBOS(USBMS): if coverdata and coverdata[2]: cover = Image.open(cStringIO.StringIO(coverdata[2])) else: - coverdata = open(I('library.png'), 'rb').read() + coverdata = lopen(I('library.png'), 'rb').read() cover = Image.new('RGB', (120,160), 'black') im = Image.open(cStringIO.StringIO(coverdata)) @@ -409,7 +409,7 @@ class WEBOS(USBMS): cover.save(data, 'JPEG') coverdata = data.getvalue() - with open(os.path.join(path, 'coverCache', filename + '-medium.jpg'), 'wb') as coverfile: + with lopen(os.path.join(path, 'coverCache', filename + '-medium.jpg'), 'wb') as coverfile: coverfile.write(coverdata) fsync(coverfile) @@ -417,7 +417,7 @@ class WEBOS(USBMS): if coverdata and coverdata[2]: cover = Image.open(cStringIO.StringIO(coverdata[2])) else: - coverdata = open(I('library.png'), 'rb').read() + coverdata = lopen(I('library.png'), 'rb').read() cover = Image.new('RGB', (52,69), 'black') im = Image.open(cStringIO.StringIO(coverdata)) @@ -432,7 +432,7 @@ class WEBOS(USBMS): cover2.save(data, 'JPEG') coverdata = data.getvalue() - with open(os.path.join(path, 'coverCache', filename + + with lopen(os.path.join(path, 'coverCache', filename + '-small.jpg'), 'wb') as coverfile: coverfile.write(coverdata) fsync(coverfile) diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index db01da617a..b252c840e9 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -1516,7 +1516,7 @@ class ITUNES(DriverBase): cover_data = cd.getvalue() cd.close() else: - with open(metadata.cover, 'r+b') as cd: + with lopen(metadata.cover, 'r+b') as cd: cover_data = cd.read() except: self.problem_titles.append("'%s' by %s" % (metadata.title, authors_to_string(metadata.authors))) @@ -1592,7 +1592,7 @@ class ITUNES(DriverBase): elif iswindows: ''' Write the data to a real file for Windows iTunes ''' tc = os.path.join(tempfile.gettempdir(), "cover.jpg") - with open(tc, 'wb') as tmp_cover: + with lopen(tc, 'wb') as tmp_cover: tmp_cover.write(cover_data) if lb_added: @@ -2648,7 +2648,7 @@ class ITUNES(DriverBase): # Read the current storage path for iTunes media from the XML file media_dir = '' string = None - with open(self.iTunes.LibraryXMLPath, 'r') as xml: + with lopen(self.iTunes.LibraryXMLPath, 'r') as xml: for line in xml: if line.strip().startswith('Music Folder'): soup = BeautifulSoup(line) @@ -2907,7 +2907,7 @@ class ITUNES(DriverBase): metadata_x = self._xform_metadata_via_plugboard(metadata, 'epub') # Refresh epub metadata - with open(fpath, 'r+b') as zfo: + with lopen(fpath, 'r+b') as zfo: if False: try: zf_opf = ZipFile(fpath, 'r') diff --git a/src/calibre/devices/cli.py b/src/calibre/devices/cli.py index af2ad937fd..f8276105d0 100755 --- a/src/calibre/devices/cli.py +++ b/src/calibre/devices/cli.py @@ -303,7 +303,7 @@ def main(): if os.path.isdir(outfile): outfile = os.path.join(outfile, path[path.rfind("/")+1:]) try: - outfile = open(outfile, "wb") + outfile = lopen(outfile, "wb") except IOError as e: print >> sys.stderr, e parser.print_help() @@ -313,7 +313,7 @@ def main(): outfile.close() elif args[1].startswith("dev:"): try: - infile = open(args[0], "rb") + infile = lopen(args[0], "rb") except IOError as e: print >> sys.stderr, e parser.print_help() @@ -364,7 +364,7 @@ def main(): return 1 path = args[0] from calibre.ebooks.metadata.meta import get_metadata - mi = get_metadata(open(path, 'rb'), path.rpartition('.')[-1].lower()) + mi = get_metadata(lopen(path, 'rb'), path.rpartition('.')[-1].lower()) print dev.upload_books([args[0]], [os.path.basename(args[0])], end_session=False, metadata=[mi]) dev.eject() diff --git a/src/calibre/devices/cybook/driver.py b/src/calibre/devices/cybook/driver.py index f36def1fb9..a4f82bbbdb 100644 --- a/src/calibre/devices/cybook/driver.py +++ b/src/calibre/devices/cybook/driver.py @@ -50,7 +50,7 @@ class CYBOOK(USBMS): coverdata = coverdata[2] else: coverdata = None - with open('%s_6090.t2b' % os.path.join(path, filename), 'wb') as t2bfile: + with lopen('%s_6090.t2b' % os.path.join(path, filename), 'wb') as t2bfile: t2b.write_t2b(t2bfile, coverdata) fsync(t2bfile) @@ -89,7 +89,7 @@ class ORIZON(CYBOOK): coverdata = coverdata[2] else: coverdata = None - with open('%s.thn' % filepath, 'wb') as thnfile: + with lopen('%s.thn' % filepath, 'wb') as thnfile: t4b.write_t4b(thnfile, coverdata) fsync(thnfile) diff --git a/src/calibre/devices/hanvon/driver.py b/src/calibre/devices/hanvon/driver.py index ea13e5a6d0..92ca295b4b 100644 --- a/src/calibre/devices/hanvon/driver.py +++ b/src/calibre/devices/hanvon/driver.py @@ -122,7 +122,7 @@ class ALEX(N516): cdir = os.path.dirname(cpath) if not os.path.exists(cdir): os.makedirs(cdir) - with open(cpath, 'wb') as coverfile: + with lopen(cpath, 'wb') as coverfile: coverfile.write(cover) fsync(coverfile) diff --git a/src/calibre/devices/idevice/libimobiledevice.py b/src/calibre/devices/idevice/libimobiledevice.py index f7195007d4..406eabe63a 100644 --- a/src/calibre/devices/idevice/libimobiledevice.py +++ b/src/calibre/devices/idevice/libimobiledevice.py @@ -252,7 +252,7 @@ class libiMobileDevice(): self._log("file_size: {:,} bytes".format(file_size)) if file_size > BUFFER_SIZE: bytes_remaining = file_size - with open(src, 'rb') as f: + with lopen(src, 'rb') as f: while bytes_remaining: if bytes_remaining > BUFFER_SIZE: self._log("copying {:,} byte chunk".format(BUFFER_SIZE)) @@ -266,7 +266,7 @@ class libiMobileDevice(): bytes_remaining = 0 self._log(" success: {0}".format(success)) else: - with open(src, 'rb') as f: + with lopen(src, 'rb') as f: content = bytearray(f.read()) success = self._afc_file_write(handle, content, mode='wb') self._log(" success: {0}".format(success)) diff --git a/src/calibre/devices/kindle/apnx.py b/src/calibre/devices/kindle/apnx.py index d7d69e1f0a..a34eef1838 100644 --- a/src/calibre/devices/kindle/apnx.py +++ b/src/calibre/devices/kindle/apnx.py @@ -33,7 +33,7 @@ class APNXBuilder(object): apnx_meta = {'guid': str(uuid.uuid4()).replace('-', '')[:8], 'asin': '', 'cdetype': 'EBOK', 'format': 'MOBI_7', 'acr': ''} - with open(mobi_file_path, 'rb') as mf: + with lopen(mobi_file_path, 'rb') as mf: ident = PdbHeaderReader(mf).identity() if ident != 'BOOKMOBI': # Check that this is really a MOBI file. @@ -41,7 +41,7 @@ class APNXBuilder(object): apnx_meta['acr'] = str(PdbHeaderReader(mf).name()) # We'll need the PDB name, the MOBI version, and some metadata to make FW 3.4 happy with KF8 files... - with open(mobi_file_path, 'rb') as mf: + with lopen(mobi_file_path, 'rb') as mf: mh = MetadataHeader(mf, default_log) if mh.mobi_version == 8: apnx_meta['format'] = 'MOBI_8' @@ -85,7 +85,7 @@ class APNXBuilder(object): apnx = self.generate_apnx(pages, apnx_meta) # Write the APNX. - with open(apnx_path, 'wb') as apnxf: + with lopen(apnx_path, 'wb') as apnxf: apnxf.write(apnx) fsync(apnxf) @@ -137,7 +137,7 @@ class APNXBuilder(object): pages = [] count = 0 - with open(mobi_file_path, 'rb') as mf: + with lopen(mobi_file_path, 'rb') as mf: phead = PdbHeaderReader(mf) r0 = phead.section_data(0) text_length = struct.unpack('>I', r0[4:8])[0] @@ -173,7 +173,7 @@ class APNXBuilder(object): pages = [] count = 0 - with open(mobi_file_path, 'rb') as mf: + with lopen(mobi_file_path, 'rb') as mf: phead = PdbHeaderReader(mf) r0 = phead.section_data(0) text_length = struct.unpack('>I', r0[4:8])[0] diff --git a/src/calibre/devices/kindle/bookmark.py b/src/calibre/devices/kindle/bookmark.py index aed498a093..b5eac371ef 100644 --- a/src/calibre/devices/kindle/bookmark.py +++ b/src/calibre/devices/kindle/bookmark.py @@ -47,7 +47,7 @@ class Bookmark(): # {{{ user_notes = {} if self.bookmark_extension == 'mbp': MAGIC_MOBI_CONSTANT = 150 - with open(self.path,'rb') as f: + with lopen(self.path,'rb') as f: stream = StringIO(f.read()) data = StreamSlicer(stream) self.timestamp, = unpack('>I', data[0x24:0x28]) @@ -144,14 +144,14 @@ class Bookmark(): # {{{ # Author is not matched # This will find the first instance of a clipping only book_fs = self.path.replace('.%s' % self.bookmark_extension,'.%s' % self.book_format) - with open(book_fs,'rb') as f2: + with lopen(book_fs,'rb') as f2: stream = StringIO(f2.read()) mi = get_topaz_metadata(stream) my_clippings = self.path split = my_clippings.find('documents') + len('documents/') my_clippings = my_clippings[:split] + "My Clippings.txt" try: - with open(my_clippings, 'r') as f2: + with lopen(my_clippings, 'r') as f2: marker_found = 0 text = '' search_str1 = '%s' % (mi.title) @@ -175,7 +175,7 @@ class Bookmark(): # {{{ MAGIC_TOPAZ_CONSTANT = 33.33 self.timestamp = os.path.getmtime(self.path) - with open(self.path,'rb') as f: + with lopen(self.path,'rb') as f: stream = StringIO(f.read()) data = StreamSlicer(stream) self.last_read = int(unpack('>I', data[5:9])[0]) @@ -216,7 +216,7 @@ class Bookmark(): # {{{ elif self.bookmark_extension == 'pdr': self.timestamp = os.path.getmtime(self.path) - with open(self.path,'rb') as f: + with lopen(self.path,'rb') as f: stream = StringIO(f.read()) data = StreamSlicer(stream) self.last_read = int(unpack('>I', data[5:9])[0]) @@ -285,7 +285,7 @@ class Bookmark(): # {{{ if self.bookmark_extension == 'mbp': # Read the book len from the header try: - with open(book_fs,'rb') as f: + with lopen(book_fs,'rb') as f: self.stream = StringIO(f.read()) self.data = StreamSlicer(self.stream) self.nrecs, = unpack('>H', self.data[76:78]) @@ -297,7 +297,7 @@ class Bookmark(): # {{{ # Read bookLength from metadata from calibre.ebooks.metadata.topaz import MetadataUpdater try: - with open(book_fs,'rb') as f: + with lopen(book_fs,'rb') as f: mu = MetadataUpdater(f) self.book_length = mu.book_length except: diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 1f9d137dc1..9dfc0e0c21 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -428,7 +428,7 @@ class KINDLE2(KINDLE): return bl def kindle_update_booklist(self, bl, collections): - with open(collections, 'rb') as f: + with lopen(collections, 'rb') as f: collections = f.read() collections = json.loads(collections) path_map = {} @@ -489,7 +489,7 @@ class KINDLE2(KINDLE): thumbfile = os.path.join(thumb_dir, 'thumbnail_{uuid}_{cdetype}_portrait.jpg'.format( uuid=mh.exth.uuid, cdetype=mh.exth.cdetype)) - with open(thumbfile, 'wb') as f: + with lopen(thumbfile, 'wb') as f: f.write(coverdata[2]) fsync(f) diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index 436977be16..91fe5c6a5e 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -177,7 +177,7 @@ class KOBO(USBMS): # Determine the firmware version try: - with open(self.normalize_path(self._main_prefix + '.kobo/version'), + with lopen(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f: self.fwversion = f.readline().split(',')[2] except: @@ -1011,7 +1011,7 @@ class KOBO(USBMS): fpath = self.normalize_path(fpath.replace('/', os.sep)) if os.path.exists(fpath): - with open(cover, 'rb') as f: + with lopen(cover, 'rb') as f: data = f.read() # Return the data resized and in Grayscale if @@ -1020,7 +1020,7 @@ class KOBO(USBMS): grayscale=uploadgrayscale, resize_to=resize, return_data=True) - with open(fpath, 'wb') as f: + with lopen(fpath, 'wb') as f: f.write(data) fsync(f) @@ -1040,7 +1040,7 @@ class KOBO(USBMS): ''' for idx, path in enumerate(paths): if path.find('kepub') >= 0: - with closing(open(path, 'rb')) as r: + with closing(lopen(path, 'rb')) as r: tf = PersistentTemporaryFile(suffix='.epub') shutil.copyfileobj(r, tf) # tf.write(r.read()) @@ -1451,7 +1451,7 @@ class KOBOTOUCH(KOBO): # Determine the firmware version try: - with open(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f: + with lopen(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f: self.fwversion = f.readline().split(',')[2] self.fwversion = tuple((int(x) for x in self.fwversion.split('.'))) except: @@ -2448,7 +2448,7 @@ class KOBOTOUCH(KOBO): fpath = path + ending fpath = self.normalize_path(fpath.replace('/', os.sep)) - with open(cover, 'rb') as f: + with lopen(cover, 'rb') as f: data = f.read() if keep_cover_aspect: @@ -2468,7 +2468,7 @@ class KOBOTOUCH(KOBO): grayscale=uploadgrayscale, resize_to=resize, return_data=True) - with open(fpath, 'wb') as f: + with lopen(fpath, 'wb') as f: f.write(data) fsync(f) except Exception as e: diff --git a/src/calibre/devices/misc.py b/src/calibre/devices/misc.py index 32a5bb0695..bdb9a5723c 100644 --- a/src/calibre/devices/misc.py +++ b/src/calibre/devices/misc.py @@ -97,7 +97,7 @@ class PDNOVEL(USBMS): def upload_cover(self, path, filename, metadata, filepath): coverdata = getattr(metadata, 'thumbnail', None) if coverdata and coverdata[2]: - with open('%s.jpg' % os.path.join(path, filename), 'wb') as coverfile: + with lopen('%s.jpg' % os.path.join(path, filename), 'wb') as coverfile: coverfile.write(coverdata[2]) fsync(coverfile) @@ -116,7 +116,7 @@ class PDNOVEL_KOBO(PDNOVEL): dirpath = os.path.join(path, '.thumbnail') if not os.path.exists(dirpath): os.makedirs(dirpath) - with open(os.path.join(dirpath, filename+'.jpg'), 'wb') as coverfile: + with lopen(os.path.join(dirpath, filename+'.jpg'), 'wb') as coverfile: coverfile.write(coverdata[2]) fsync(coverfile) @@ -189,7 +189,7 @@ class LUMIREAD(USBMS): pdir = os.path.dirname(cfilepath) if not os.path.exists(pdir): os.makedirs(pdir) - with open(cfilepath+'.jpg', 'wb') as f: + with lopen(cfilepath+'.jpg', 'wb') as f: f.write(metadata.thumbnail[-1]) fsync(f) @@ -336,7 +336,7 @@ class NEXTBOOK(USBMS): thumbnail_dir = os.path.join(thumbnail_dir, relpath) if not os.path.exists(thumbnail_dir): os.makedirs(thumbnail_dir) - with open(os.path.join(thumbnail_dir, filename+'.jpg'), 'wb') as f: + with lopen(os.path.join(thumbnail_dir, filename+'.jpg'), 'wb') as f: f.write(metadata.thumbnail[-1]) fsync(f) ''' diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py index 53610e6e7a..b3b424e88a 100644 --- a/src/calibre/devices/mtp/driver.py +++ b/src/calibre/devices/mtp/driver.py @@ -341,7 +341,7 @@ class MTP_DEVICE(BASE): if iswindows: plen = len(base) name = ''.join(shorten_components_to(245-plen, [name])) - with open(os.path.join(base, name), 'wb') as out: + with lopen(os.path.join(base, name), 'wb') as out: try: self.get_mtp_file(f, out) except Exception as e: diff --git a/src/calibre/devices/mtp/unix/sysfs.py b/src/calibre/devices/mtp/unix/sysfs.py index cc4171cb43..ed290eb876 100644 --- a/src/calibre/devices/mtp/unix/sysfs.py +++ b/src/calibre/devices/mtp/unix/sysfs.py @@ -28,7 +28,7 @@ class MTPDetect(object): def read(x): try: - with open(x, 'rb') as f: + with lopen(x, 'rb') as f: return f.read() except EnvironmentError: pass diff --git a/src/calibre/devices/mtp/unix/upstream/update.py b/src/calibre/devices/mtp/unix/upstream/update.py index 0fc6a2b41c..3ad7f5f7bb 100644 --- a/src/calibre/devices/mtp/unix/upstream/update.py +++ b/src/calibre/devices/mtp/unix/upstream/update.py @@ -17,6 +17,6 @@ if os.path.exists('libmtp'): subprocess.check_call(['git', 'clone', 'git://git.code.sf.net/p/libmtp/code', 'libmtp']) for x in ('src/music-players.h', 'src/device-flags.h'): - with open(os.path.join(base, os.path.basename(x)), 'wb') as f: + with lopen(os.path.join(base, os.path.basename(x)), 'wb') as f: shutil.copyfileobj(open('libmtp/'+x), f) diff --git a/src/calibre/devices/nook/driver.py b/src/calibre/devices/nook/driver.py index 918cf333cb..b66e896ae9 100644 --- a/src/calibre/devices/nook/driver.py +++ b/src/calibre/devices/nook/driver.py @@ -58,7 +58,7 @@ class NOOK(USBMS): if coverdata and coverdata[2]: cover = Image.open(cStringIO.StringIO(coverdata[2])) else: - coverdata = open(I('library.png'), 'rb').read() + coverdata = lopen(I('library.png'), 'rb').read() cover = Image.new('RGB', (96, 144), 'black') im = Image.open(cStringIO.StringIO(coverdata)) @@ -75,7 +75,7 @@ class NOOK(USBMS): cover.save(data, 'JPEG') coverdata = data.getvalue() - with open('%s.jpg' % os.path.join(path, filename), 'wb') as coverfile: + with lopen('%s.jpg' % os.path.join(path, filename), 'wb') as coverfile: coverfile.write(coverdata) fsync(coverfile) diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index 10cb7df22c..9441b3085b 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -138,7 +138,7 @@ class PRS505(USBMS): except: time.sleep(5) os.makedirs(dname, mode=0777) - with open(cachep, 'wb') as f: + with lopen(cachep, 'wb') as f: f.write(u''' @@ -300,7 +300,7 @@ class PRS505(USBMS): if not os.path.exists(thumbnail_dir): os.makedirs(thumbnail_dir) cpath = os.path.join(thumbnail_dir, 'main_thumbnail.jpg') - with open(cpath, 'wb') as f: + with lopen(cpath, 'wb') as f: f.write(metadata.thumbnail[-1]) debug_print('Cover uploaded to: %r'%cpath) diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index 668ff80c54..8360919d29 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -100,12 +100,12 @@ class XMLCache(object): if not os.path.exists(path): raise DeviceError(('The SONY XML cache %r does not exist. Try' ' disconnecting and reconnecting your reader.')%repr(path)) - with open(path, 'rb') as f: + with lopen(path, 'rb') as f: raw = f.read() else: raw = EMPTY_CARD_CACHE if os.access(path, os.R_OK): - with open(path, 'rb') as f: + with lopen(path, 'rb') as f: raw = f.read() self.roots[source_id] = etree.fromstring(xml_to_unicode( @@ -120,14 +120,14 @@ class XMLCache(object): for source_id, path in ext_paths.items(): if not os.path.exists(path): try: - with open(path, 'wb') as f: + with lopen(path, 'wb') as f: f.write(EMPTY_EXT_CACHE) fsync(f) except: pass if os.access(path, os.W_OK): try: - with open(path, 'rb') as f: + with lopen(path, 'rb') as f: self.ext_roots[source_id] = etree.fromstring( xml_to_unicode(f.read(), strip_encoding_pats=True, assume_utf8=True, @@ -725,7 +725,7 @@ class XMLCache(object): xml_declaration=True) raw = raw.replace("", '') - with open(path, 'wb') as f: + with lopen(path, 'wb') as f: f.write(raw) fsync(f) @@ -737,7 +737,7 @@ class XMLCache(object): continue raw = raw.replace("", '') - with open(path, 'wb') as f: + with lopen(path, 'wb') as f: f.write(raw) fsync(f) diff --git a/src/calibre/devices/prst1/driver.py b/src/calibre/devices/prst1/driver.py index 6c9f365695..5f3b19867b 100644 --- a/src/calibre/devices/prst1/driver.py +++ b/src/calibre/devices/prst1/driver.py @@ -761,7 +761,7 @@ class PRST1(USBMS): if not os.path.exists(thumbnail_dir_path): os.makedirs(thumbnail_dir_path) - with open(thumbnail_file_path, 'wb') as f: + with lopen(thumbnail_file_path, 'wb') as f: f.write(book.thumbnail[-1]) fsync(f) diff --git a/src/calibre/devices/scanner.py b/src/calibre/devices/scanner.py index 794b331d51..fc22c05c3d 100644 --- a/src/calibre/devices/scanner.py +++ b/src/calibre/devices/scanner.py @@ -111,7 +111,7 @@ class LinuxScanner(object): raise RuntimeError('DeviceScanner requires the /sys filesystem to work.') def read(f): - with open(f, 'rb') as s: + with lopen(f, 'rb') as s: return s.read().strip() for x in os.listdir(self.base): diff --git a/src/calibre/devices/smart_device_app/driver.py b/src/calibre/devices/smart_device_app/driver.py index 56544be30c..6bf0504d64 100644 --- a/src/calibre/devices/smart_device_app/driver.py +++ b/src/calibre/devices/smart_device_app/driver.py @@ -658,7 +658,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): def _put_file(self, infile, lpath, book_metadata, this_book, total_books): close_ = False if not hasattr(infile, 'read'): - infile, close_ = open(infile, 'rb'), True + infile, close_ = lopen(infile, 'rb'), True infile.seek(0, os.SEEK_END) length = infile.tell() book_metadata.size = length @@ -783,7 +783,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): try: count = 0 if os.path.exists(cache_file_name): - with open(cache_file_name, mode='rb') as fd: + with lopen(cache_file_name, mode='rb') as fd: while True: rec_len = fd.readline() if len(rec_len) != 8: @@ -820,7 +820,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): count = 0 prefix = os.path.join(cache_dir(), 'wireless_device_' + self.device_uuid + '_metadata_cache') - with open(prefix + '.tmp', mode='wb') as fd: + with lopen(prefix + '.tmp', mode='wb') as fd: for key,book in self.device_book_cache.iteritems(): if (now_ - book['last_used']).days > self.PURGE_CACHE_ENTRIES_DAYS: purged += 1 @@ -913,7 +913,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): from calibre.ebooks.metadata.meta import get_metadata from calibre.customize.ui import quick_metadata ext = temp_file_name.rpartition('.')[-1].lower() - with open(temp_file_name, 'rb') as stream: + with lopen(temp_file_name, 'rb') as stream: with quick_metadata: return get_metadata(stream, stream_type=ext, force_read_metadata=True, diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index 973fedd235..0cfaffb341 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -627,7 +627,7 @@ class Device(DeviceConfig, DevicePlugin): path = os.path.join(mp, 'calibre_readonly_test') ro = True try: - with open(path, 'wb'): + with lopen(path, 'wb'): ro = False except: pass