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
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:

View File

@ -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)

View File

@ -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('<key>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')

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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]

View File

@ -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:

View File

@ -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)

View File

@ -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:

View File

@ -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)
'''

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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'''<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns="http://www.kinoma.com/FskCache/1">
</cache>
@ -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)

View File

@ -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("<?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)
fsync(f)
@ -737,7 +737,7 @@ class XMLCache(object):
continue
raw = raw.replace("<?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)
fsync(f)

View File

@ -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)

View File

@ -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):

View File

@ -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,

View File

@ -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