More inheritable file handles int he device code

This commit is contained in:
Kovid Goyal 2016-02-07 09:34:22 +05:30
parent 1ebaca2486
commit 444b302473
22 changed files with 62 additions and 62 deletions

View File

@ -175,7 +175,7 @@ def debug(ioreg_to_tmp=False, buf=None, plugins=None,
ioreg = 'IOREG Output\n'+ioreg ioreg = 'IOREG Output\n'+ioreg
out(' ') out(' ')
if ioreg_to_tmp: 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('Dont forget to send the contents of /tmp/ioreg.txt')
out('You can open it with the command: open /tmp/ioreg.txt') out('You can open it with the command: open /tmp/ioreg.txt')
else: else:

View File

@ -392,7 +392,7 @@ class WEBOS(USBMS):
if coverdata and coverdata[2]: if coverdata and coverdata[2]:
cover = Image.open(cStringIO.StringIO(coverdata[2])) cover = Image.open(cStringIO.StringIO(coverdata[2]))
else: else:
coverdata = open(I('library.png'), 'rb').read() coverdata = lopen(I('library.png'), 'rb').read()
cover = Image.new('RGB', (120,160), 'black') cover = Image.new('RGB', (120,160), 'black')
im = Image.open(cStringIO.StringIO(coverdata)) im = Image.open(cStringIO.StringIO(coverdata))
@ -409,7 +409,7 @@ class WEBOS(USBMS):
cover.save(data, 'JPEG') cover.save(data, 'JPEG')
coverdata = data.getvalue() 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) coverfile.write(coverdata)
fsync(coverfile) fsync(coverfile)
@ -417,7 +417,7 @@ class WEBOS(USBMS):
if coverdata and coverdata[2]: if coverdata and coverdata[2]:
cover = Image.open(cStringIO.StringIO(coverdata[2])) cover = Image.open(cStringIO.StringIO(coverdata[2]))
else: else:
coverdata = open(I('library.png'), 'rb').read() coverdata = lopen(I('library.png'), 'rb').read()
cover = Image.new('RGB', (52,69), 'black') cover = Image.new('RGB', (52,69), 'black')
im = Image.open(cStringIO.StringIO(coverdata)) im = Image.open(cStringIO.StringIO(coverdata))
@ -432,7 +432,7 @@ class WEBOS(USBMS):
cover2.save(data, 'JPEG') cover2.save(data, 'JPEG')
coverdata = data.getvalue() coverdata = data.getvalue()
with open(os.path.join(path, 'coverCache', filename + with lopen(os.path.join(path, 'coverCache', filename +
'-small.jpg'), 'wb') as coverfile: '-small.jpg'), 'wb') as coverfile:
coverfile.write(coverdata) coverfile.write(coverdata)
fsync(coverfile) fsync(coverfile)

View File

@ -1516,7 +1516,7 @@ class ITUNES(DriverBase):
cover_data = cd.getvalue() cover_data = cd.getvalue()
cd.close() cd.close()
else: else:
with open(metadata.cover, 'r+b') as cd: with lopen(metadata.cover, 'r+b') as cd:
cover_data = cd.read() cover_data = cd.read()
except: except:
self.problem_titles.append("'%s' by %s" % (metadata.title, authors_to_string(metadata.authors))) self.problem_titles.append("'%s' by %s" % (metadata.title, authors_to_string(metadata.authors)))
@ -1592,7 +1592,7 @@ class ITUNES(DriverBase):
elif iswindows: elif iswindows:
''' Write the data to a real file for Windows iTunes ''' ''' Write the data to a real file for Windows iTunes '''
tc = os.path.join(tempfile.gettempdir(), "cover.jpg") 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) tmp_cover.write(cover_data)
if lb_added: if lb_added:
@ -2648,7 +2648,7 @@ class ITUNES(DriverBase):
# Read the current storage path for iTunes media from the XML file # Read the current storage path for iTunes media from the XML file
media_dir = '' media_dir = ''
string = None string = None
with open(self.iTunes.LibraryXMLPath, 'r') as xml: with lopen(self.iTunes.LibraryXMLPath, 'r') as xml:
for line in xml: for line in xml:
if line.strip().startswith('<key>Music Folder'): if line.strip().startswith('<key>Music Folder'):
soup = BeautifulSoup(line) soup = BeautifulSoup(line)
@ -2907,7 +2907,7 @@ class ITUNES(DriverBase):
metadata_x = self._xform_metadata_via_plugboard(metadata, 'epub') metadata_x = self._xform_metadata_via_plugboard(metadata, 'epub')
# Refresh epub metadata # Refresh epub metadata
with open(fpath, 'r+b') as zfo: with lopen(fpath, 'r+b') as zfo:
if False: if False:
try: try:
zf_opf = ZipFile(fpath, 'r') zf_opf = ZipFile(fpath, 'r')

View File

@ -303,7 +303,7 @@ def main():
if os.path.isdir(outfile): if os.path.isdir(outfile):
outfile = os.path.join(outfile, path[path.rfind("/")+1:]) outfile = os.path.join(outfile, path[path.rfind("/")+1:])
try: try:
outfile = open(outfile, "wb") outfile = lopen(outfile, "wb")
except IOError as e: except IOError as e:
print >> sys.stderr, e print >> sys.stderr, e
parser.print_help() parser.print_help()
@ -313,7 +313,7 @@ def main():
outfile.close() outfile.close()
elif args[1].startswith("dev:"): elif args[1].startswith("dev:"):
try: try:
infile = open(args[0], "rb") infile = lopen(args[0], "rb")
except IOError as e: except IOError as e:
print >> sys.stderr, e print >> sys.stderr, e
parser.print_help() parser.print_help()
@ -364,7 +364,7 @@ def main():
return 1 return 1
path = args[0] path = args[0]
from calibre.ebooks.metadata.meta import get_metadata 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])], print dev.upload_books([args[0]], [os.path.basename(args[0])],
end_session=False, metadata=[mi]) end_session=False, metadata=[mi])
dev.eject() dev.eject()

View File

@ -50,7 +50,7 @@ class CYBOOK(USBMS):
coverdata = coverdata[2] coverdata = coverdata[2]
else: else:
coverdata = None 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) t2b.write_t2b(t2bfile, coverdata)
fsync(t2bfile) fsync(t2bfile)
@ -89,7 +89,7 @@ class ORIZON(CYBOOK):
coverdata = coverdata[2] coverdata = coverdata[2]
else: else:
coverdata = None coverdata = None
with open('%s.thn' % filepath, 'wb') as thnfile: with lopen('%s.thn' % filepath, 'wb') as thnfile:
t4b.write_t4b(thnfile, coverdata) t4b.write_t4b(thnfile, coverdata)
fsync(thnfile) fsync(thnfile)

View File

@ -122,7 +122,7 @@ class ALEX(N516):
cdir = os.path.dirname(cpath) cdir = os.path.dirname(cpath)
if not os.path.exists(cdir): if not os.path.exists(cdir):
os.makedirs(cdir) os.makedirs(cdir)
with open(cpath, 'wb') as coverfile: with lopen(cpath, 'wb') as coverfile:
coverfile.write(cover) coverfile.write(cover)
fsync(coverfile) fsync(coverfile)

View File

@ -252,7 +252,7 @@ class libiMobileDevice():
self._log("file_size: {:,} bytes".format(file_size)) self._log("file_size: {:,} bytes".format(file_size))
if file_size > BUFFER_SIZE: if file_size > BUFFER_SIZE:
bytes_remaining = file_size bytes_remaining = file_size
with open(src, 'rb') as f: with lopen(src, 'rb') as f:
while bytes_remaining: while bytes_remaining:
if bytes_remaining > BUFFER_SIZE: if bytes_remaining > BUFFER_SIZE:
self._log("copying {:,} byte chunk".format(BUFFER_SIZE)) self._log("copying {:,} byte chunk".format(BUFFER_SIZE))
@ -266,7 +266,7 @@ class libiMobileDevice():
bytes_remaining = 0 bytes_remaining = 0
self._log(" success: {0}".format(success)) self._log(" success: {0}".format(success))
else: else:
with open(src, 'rb') as f: with lopen(src, 'rb') as f:
content = bytearray(f.read()) content = bytearray(f.read())
success = self._afc_file_write(handle, content, mode='wb') success = self._afc_file_write(handle, content, mode='wb')
self._log(" success: {0}".format(success)) self._log(" success: {0}".format(success))

View File

@ -33,7 +33,7 @@ class APNXBuilder(object):
apnx_meta = {'guid': str(uuid.uuid4()).replace('-', '')[:8], 'asin': apnx_meta = {'guid': str(uuid.uuid4()).replace('-', '')[:8], 'asin':
'', 'cdetype': 'EBOK', 'format': 'MOBI_7', 'acr': ''} '', '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() ident = PdbHeaderReader(mf).identity()
if ident != 'BOOKMOBI': if ident != 'BOOKMOBI':
# Check that this is really a MOBI file. # Check that this is really a MOBI file.
@ -41,7 +41,7 @@ class APNXBuilder(object):
apnx_meta['acr'] = str(PdbHeaderReader(mf).name()) 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... # 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) mh = MetadataHeader(mf, default_log)
if mh.mobi_version == 8: if mh.mobi_version == 8:
apnx_meta['format'] = 'MOBI_8' apnx_meta['format'] = 'MOBI_8'
@ -85,7 +85,7 @@ class APNXBuilder(object):
apnx = self.generate_apnx(pages, apnx_meta) apnx = self.generate_apnx(pages, apnx_meta)
# Write the APNX. # Write the APNX.
with open(apnx_path, 'wb') as apnxf: with lopen(apnx_path, 'wb') as apnxf:
apnxf.write(apnx) apnxf.write(apnx)
fsync(apnxf) fsync(apnxf)
@ -137,7 +137,7 @@ class APNXBuilder(object):
pages = [] pages = []
count = 0 count = 0
with open(mobi_file_path, 'rb') as mf: with lopen(mobi_file_path, 'rb') as mf:
phead = PdbHeaderReader(mf) phead = PdbHeaderReader(mf)
r0 = phead.section_data(0) r0 = phead.section_data(0)
text_length = struct.unpack('>I', r0[4:8])[0] text_length = struct.unpack('>I', r0[4:8])[0]
@ -173,7 +173,7 @@ class APNXBuilder(object):
pages = [] pages = []
count = 0 count = 0
with open(mobi_file_path, 'rb') as mf: with lopen(mobi_file_path, 'rb') as mf:
phead = PdbHeaderReader(mf) phead = PdbHeaderReader(mf)
r0 = phead.section_data(0) r0 = phead.section_data(0)
text_length = struct.unpack('>I', r0[4:8])[0] text_length = struct.unpack('>I', r0[4:8])[0]

View File

@ -47,7 +47,7 @@ class Bookmark(): # {{{
user_notes = {} user_notes = {}
if self.bookmark_extension == 'mbp': if self.bookmark_extension == 'mbp':
MAGIC_MOBI_CONSTANT = 150 MAGIC_MOBI_CONSTANT = 150
with open(self.path,'rb') as f: with lopen(self.path,'rb') as f:
stream = StringIO(f.read()) stream = StringIO(f.read())
data = StreamSlicer(stream) data = StreamSlicer(stream)
self.timestamp, = unpack('>I', data[0x24:0x28]) self.timestamp, = unpack('>I', data[0x24:0x28])
@ -144,14 +144,14 @@ class Bookmark(): # {{{
# Author is not matched # Author is not matched
# This will find the first instance of a clipping only # This will find the first instance of a clipping only
book_fs = self.path.replace('.%s' % self.bookmark_extension,'.%s' % self.book_format) 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()) stream = StringIO(f2.read())
mi = get_topaz_metadata(stream) mi = get_topaz_metadata(stream)
my_clippings = self.path my_clippings = self.path
split = my_clippings.find('documents') + len('documents/') split = my_clippings.find('documents') + len('documents/')
my_clippings = my_clippings[:split] + "My Clippings.txt" my_clippings = my_clippings[:split] + "My Clippings.txt"
try: try:
with open(my_clippings, 'r') as f2: with lopen(my_clippings, 'r') as f2:
marker_found = 0 marker_found = 0
text = '' text = ''
search_str1 = '%s' % (mi.title) search_str1 = '%s' % (mi.title)
@ -175,7 +175,7 @@ class Bookmark(): # {{{
MAGIC_TOPAZ_CONSTANT = 33.33 MAGIC_TOPAZ_CONSTANT = 33.33
self.timestamp = os.path.getmtime(self.path) 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()) stream = StringIO(f.read())
data = StreamSlicer(stream) data = StreamSlicer(stream)
self.last_read = int(unpack('>I', data[5:9])[0]) self.last_read = int(unpack('>I', data[5:9])[0])
@ -216,7 +216,7 @@ class Bookmark(): # {{{
elif self.bookmark_extension == 'pdr': elif self.bookmark_extension == 'pdr':
self.timestamp = os.path.getmtime(self.path) 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()) stream = StringIO(f.read())
data = StreamSlicer(stream) data = StreamSlicer(stream)
self.last_read = int(unpack('>I', data[5:9])[0]) self.last_read = int(unpack('>I', data[5:9])[0])
@ -285,7 +285,7 @@ class Bookmark(): # {{{
if self.bookmark_extension == 'mbp': if self.bookmark_extension == 'mbp':
# Read the book len from the header # Read the book len from the header
try: try:
with open(book_fs,'rb') as f: with lopen(book_fs,'rb') as f:
self.stream = StringIO(f.read()) self.stream = StringIO(f.read())
self.data = StreamSlicer(self.stream) self.data = StreamSlicer(self.stream)
self.nrecs, = unpack('>H', self.data[76:78]) self.nrecs, = unpack('>H', self.data[76:78])
@ -297,7 +297,7 @@ class Bookmark(): # {{{
# Read bookLength from metadata # Read bookLength from metadata
from calibre.ebooks.metadata.topaz import MetadataUpdater from calibre.ebooks.metadata.topaz import MetadataUpdater
try: try:
with open(book_fs,'rb') as f: with lopen(book_fs,'rb') as f:
mu = MetadataUpdater(f) mu = MetadataUpdater(f)
self.book_length = mu.book_length self.book_length = mu.book_length
except: except:

View File

@ -428,7 +428,7 @@ class KINDLE2(KINDLE):
return bl return bl
def kindle_update_booklist(self, bl, collections): def kindle_update_booklist(self, bl, collections):
with open(collections, 'rb') as f: with lopen(collections, 'rb') as f:
collections = f.read() collections = f.read()
collections = json.loads(collections) collections = json.loads(collections)
path_map = {} path_map = {}
@ -489,7 +489,7 @@ class KINDLE2(KINDLE):
thumbfile = os.path.join(thumb_dir, thumbfile = os.path.join(thumb_dir,
'thumbnail_{uuid}_{cdetype}_portrait.jpg'.format( 'thumbnail_{uuid}_{cdetype}_portrait.jpg'.format(
uuid=mh.exth.uuid, cdetype=mh.exth.cdetype)) uuid=mh.exth.uuid, cdetype=mh.exth.cdetype))
with open(thumbfile, 'wb') as f: with lopen(thumbfile, 'wb') as f:
f.write(coverdata[2]) f.write(coverdata[2])
fsync(f) fsync(f)

View File

@ -177,7 +177,7 @@ class KOBO(USBMS):
# Determine the firmware version # Determine the firmware version
try: try:
with open(self.normalize_path(self._main_prefix + '.kobo/version'), with lopen(self.normalize_path(self._main_prefix + '.kobo/version'),
'rb') as f: 'rb') as f:
self.fwversion = f.readline().split(',')[2] self.fwversion = f.readline().split(',')[2]
except: except:
@ -1011,7 +1011,7 @@ class KOBO(USBMS):
fpath = self.normalize_path(fpath.replace('/', os.sep)) fpath = self.normalize_path(fpath.replace('/', os.sep))
if os.path.exists(fpath): if os.path.exists(fpath):
with open(cover, 'rb') as f: with lopen(cover, 'rb') as f:
data = f.read() data = f.read()
# Return the data resized and in Grayscale if # Return the data resized and in Grayscale if
@ -1020,7 +1020,7 @@ class KOBO(USBMS):
grayscale=uploadgrayscale, grayscale=uploadgrayscale,
resize_to=resize, return_data=True) resize_to=resize, return_data=True)
with open(fpath, 'wb') as f: with lopen(fpath, 'wb') as f:
f.write(data) f.write(data)
fsync(f) fsync(f)
@ -1040,7 +1040,7 @@ class KOBO(USBMS):
''' '''
for idx, path in enumerate(paths): for idx, path in enumerate(paths):
if path.find('kepub') >= 0: if path.find('kepub') >= 0:
with closing(open(path, 'rb')) as r: with closing(lopen(path, 'rb')) as r:
tf = PersistentTemporaryFile(suffix='.epub') tf = PersistentTemporaryFile(suffix='.epub')
shutil.copyfileobj(r, tf) shutil.copyfileobj(r, tf)
# tf.write(r.read()) # tf.write(r.read())
@ -1451,7 +1451,7 @@ class KOBOTOUCH(KOBO):
# Determine the firmware version # Determine the firmware version
try: 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 = f.readline().split(',')[2]
self.fwversion = tuple((int(x) for x in self.fwversion.split('.'))) self.fwversion = tuple((int(x) for x in self.fwversion.split('.')))
except: except:
@ -2448,7 +2448,7 @@ class KOBOTOUCH(KOBO):
fpath = path + ending fpath = path + ending
fpath = self.normalize_path(fpath.replace('/', os.sep)) fpath = self.normalize_path(fpath.replace('/', os.sep))
with open(cover, 'rb') as f: with lopen(cover, 'rb') as f:
data = f.read() data = f.read()
if keep_cover_aspect: if keep_cover_aspect:
@ -2468,7 +2468,7 @@ class KOBOTOUCH(KOBO):
grayscale=uploadgrayscale, grayscale=uploadgrayscale,
resize_to=resize, return_data=True) resize_to=resize, return_data=True)
with open(fpath, 'wb') as f: with lopen(fpath, 'wb') as f:
f.write(data) f.write(data)
fsync(f) fsync(f)
except Exception as e: except Exception as e:

View File

@ -97,7 +97,7 @@ class PDNOVEL(USBMS):
def upload_cover(self, path, filename, metadata, filepath): def upload_cover(self, path, filename, metadata, filepath):
coverdata = getattr(metadata, 'thumbnail', None) coverdata = getattr(metadata, 'thumbnail', None)
if coverdata and coverdata[2]: 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]) coverfile.write(coverdata[2])
fsync(coverfile) fsync(coverfile)
@ -116,7 +116,7 @@ class PDNOVEL_KOBO(PDNOVEL):
dirpath = os.path.join(path, '.thumbnail') dirpath = os.path.join(path, '.thumbnail')
if not os.path.exists(dirpath): if not os.path.exists(dirpath):
os.makedirs(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]) coverfile.write(coverdata[2])
fsync(coverfile) fsync(coverfile)
@ -189,7 +189,7 @@ class LUMIREAD(USBMS):
pdir = os.path.dirname(cfilepath) pdir = os.path.dirname(cfilepath)
if not os.path.exists(pdir): if not os.path.exists(pdir):
os.makedirs(pdir) os.makedirs(pdir)
with open(cfilepath+'.jpg', 'wb') as f: with lopen(cfilepath+'.jpg', 'wb') as f:
f.write(metadata.thumbnail[-1]) f.write(metadata.thumbnail[-1])
fsync(f) fsync(f)
@ -336,7 +336,7 @@ class NEXTBOOK(USBMS):
thumbnail_dir = os.path.join(thumbnail_dir, relpath) thumbnail_dir = os.path.join(thumbnail_dir, relpath)
if not os.path.exists(thumbnail_dir): if not os.path.exists(thumbnail_dir):
os.makedirs(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]) f.write(metadata.thumbnail[-1])
fsync(f) fsync(f)
''' '''

View File

@ -341,7 +341,7 @@ class MTP_DEVICE(BASE):
if iswindows: if iswindows:
plen = len(base) plen = len(base)
name = ''.join(shorten_components_to(245-plen, [name])) 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: try:
self.get_mtp_file(f, out) self.get_mtp_file(f, out)
except Exception as e: except Exception as e:

View File

@ -28,7 +28,7 @@ class MTPDetect(object):
def read(x): def read(x):
try: try:
with open(x, 'rb') as f: with lopen(x, 'rb') as f:
return f.read() return f.read()
except EnvironmentError: except EnvironmentError:
pass pass

View File

@ -17,6 +17,6 @@ if os.path.exists('libmtp'):
subprocess.check_call(['git', 'clone', 'git://git.code.sf.net/p/libmtp/code', subprocess.check_call(['git', 'clone', 'git://git.code.sf.net/p/libmtp/code',
'libmtp']) 'libmtp'])
for x in ('src/music-players.h', 'src/device-flags.h'): 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) shutil.copyfileobj(open('libmtp/'+x), f)

View File

@ -58,7 +58,7 @@ class NOOK(USBMS):
if coverdata and coverdata[2]: if coverdata and coverdata[2]:
cover = Image.open(cStringIO.StringIO(coverdata[2])) cover = Image.open(cStringIO.StringIO(coverdata[2]))
else: else:
coverdata = open(I('library.png'), 'rb').read() coverdata = lopen(I('library.png'), 'rb').read()
cover = Image.new('RGB', (96, 144), 'black') cover = Image.new('RGB', (96, 144), 'black')
im = Image.open(cStringIO.StringIO(coverdata)) im = Image.open(cStringIO.StringIO(coverdata))
@ -75,7 +75,7 @@ class NOOK(USBMS):
cover.save(data, 'JPEG') cover.save(data, 'JPEG')
coverdata = data.getvalue() 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) coverfile.write(coverdata)
fsync(coverfile) fsync(coverfile)

View File

@ -138,7 +138,7 @@ class PRS505(USBMS):
except: except:
time.sleep(5) time.sleep(5)
os.makedirs(dname, mode=0777) os.makedirs(dname, mode=0777)
with open(cachep, 'wb') as f: with lopen(cachep, 'wb') as f:
f.write(u'''<?xml version="1.0" encoding="UTF-8"?> f.write(u'''<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns="http://www.kinoma.com/FskCache/1"> <cache xmlns="http://www.kinoma.com/FskCache/1">
</cache> </cache>
@ -300,7 +300,7 @@ class PRS505(USBMS):
if not os.path.exists(thumbnail_dir): if not os.path.exists(thumbnail_dir):
os.makedirs(thumbnail_dir) os.makedirs(thumbnail_dir)
cpath = os.path.join(thumbnail_dir, 'main_thumbnail.jpg') 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]) f.write(metadata.thumbnail[-1])
debug_print('Cover uploaded to: %r'%cpath) debug_print('Cover uploaded to: %r'%cpath)

View File

@ -100,12 +100,12 @@ class XMLCache(object):
if not os.path.exists(path): if not os.path.exists(path):
raise DeviceError(('The SONY XML cache %r does not exist. Try' raise DeviceError(('The SONY XML cache %r does not exist. Try'
' disconnecting and reconnecting your reader.')%repr(path)) ' disconnecting and reconnecting your reader.')%repr(path))
with open(path, 'rb') as f: with lopen(path, 'rb') as f:
raw = f.read() raw = f.read()
else: else:
raw = EMPTY_CARD_CACHE raw = EMPTY_CARD_CACHE
if os.access(path, os.R_OK): if os.access(path, os.R_OK):
with open(path, 'rb') as f: with lopen(path, 'rb') as f:
raw = f.read() raw = f.read()
self.roots[source_id] = etree.fromstring(xml_to_unicode( self.roots[source_id] = etree.fromstring(xml_to_unicode(
@ -120,14 +120,14 @@ class XMLCache(object):
for source_id, path in ext_paths.items(): for source_id, path in ext_paths.items():
if not os.path.exists(path): if not os.path.exists(path):
try: try:
with open(path, 'wb') as f: with lopen(path, 'wb') as f:
f.write(EMPTY_EXT_CACHE) f.write(EMPTY_EXT_CACHE)
fsync(f) fsync(f)
except: except:
pass pass
if os.access(path, os.W_OK): if os.access(path, os.W_OK):
try: try:
with open(path, 'rb') as f: with lopen(path, 'rb') as f:
self.ext_roots[source_id] = etree.fromstring( self.ext_roots[source_id] = etree.fromstring(
xml_to_unicode(f.read(), xml_to_unicode(f.read(),
strip_encoding_pats=True, assume_utf8=True, strip_encoding_pats=True, assume_utf8=True,
@ -725,7 +725,7 @@ class XMLCache(object):
xml_declaration=True) xml_declaration=True)
raw = raw.replace("<?xml version='1.0' encoding='UTF-8'?>", raw = raw.replace("<?xml version='1.0' encoding='UTF-8'?>",
'<?xml version="1.0" encoding="UTF-8"?>') '<?xml version="1.0" encoding="UTF-8"?>')
with open(path, 'wb') as f: with lopen(path, 'wb') as f:
f.write(raw) f.write(raw)
fsync(f) fsync(f)
@ -737,7 +737,7 @@ class XMLCache(object):
continue continue
raw = raw.replace("<?xml version='1.0' encoding='UTF-8'?>", raw = raw.replace("<?xml version='1.0' encoding='UTF-8'?>",
'<?xml version="1.0" encoding="UTF-8"?>') '<?xml version="1.0" encoding="UTF-8"?>')
with open(path, 'wb') as f: with lopen(path, 'wb') as f:
f.write(raw) f.write(raw)
fsync(f) fsync(f)

View File

@ -761,7 +761,7 @@ class PRST1(USBMS):
if not os.path.exists(thumbnail_dir_path): if not os.path.exists(thumbnail_dir_path):
os.makedirs(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]) f.write(book.thumbnail[-1])
fsync(f) fsync(f)

View File

@ -111,7 +111,7 @@ class LinuxScanner(object):
raise RuntimeError('DeviceScanner requires the /sys filesystem to work.') raise RuntimeError('DeviceScanner requires the /sys filesystem to work.')
def read(f): def read(f):
with open(f, 'rb') as s: with lopen(f, 'rb') as s:
return s.read().strip() return s.read().strip()
for x in os.listdir(self.base): for x in os.listdir(self.base):

View File

@ -658,7 +658,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
def _put_file(self, infile, lpath, book_metadata, this_book, total_books): def _put_file(self, infile, lpath, book_metadata, this_book, total_books):
close_ = False close_ = False
if not hasattr(infile, 'read'): if not hasattr(infile, 'read'):
infile, close_ = open(infile, 'rb'), True infile, close_ = lopen(infile, 'rb'), True
infile.seek(0, os.SEEK_END) infile.seek(0, os.SEEK_END)
length = infile.tell() length = infile.tell()
book_metadata.size = length book_metadata.size = length
@ -783,7 +783,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
try: try:
count = 0 count = 0
if os.path.exists(cache_file_name): 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: while True:
rec_len = fd.readline() rec_len = fd.readline()
if len(rec_len) != 8: if len(rec_len) != 8:
@ -820,7 +820,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
count = 0 count = 0
prefix = os.path.join(cache_dir(), prefix = os.path.join(cache_dir(),
'wireless_device_' + self.device_uuid + '_metadata_cache') '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(): for key,book in self.device_book_cache.iteritems():
if (now_ - book['last_used']).days > self.PURGE_CACHE_ENTRIES_DAYS: if (now_ - book['last_used']).days > self.PURGE_CACHE_ENTRIES_DAYS:
purged += 1 purged += 1
@ -913,7 +913,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.meta import get_metadata
from calibre.customize.ui import quick_metadata from calibre.customize.ui import quick_metadata
ext = temp_file_name.rpartition('.')[-1].lower() 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: with quick_metadata:
return get_metadata(stream, stream_type=ext, return get_metadata(stream, stream_type=ext,
force_read_metadata=True, force_read_metadata=True,

View File

@ -627,7 +627,7 @@ class Device(DeviceConfig, DevicePlugin):
path = os.path.join(mp, 'calibre_readonly_test') path = os.path.join(mp, 'calibre_readonly_test')
ro = True ro = True
try: try:
with open(path, 'wb'): with lopen(path, 'wb'):
ro = False ro = False
except: except:
pass pass