perform various automated cleanups of python2-compatible code

performed using:

```
pyupgrade --keep-percent-format --py3-only
```

and committing the results.

This changes various things, including:
- remove u from u'' strings, since python3 strings are always unicode
- consolidate on OSError for various exceptions that are deprecated aliases
- dict/set literals and comprehensions
- yield from
- remove extraneous () produced by accident by some modernization code
- modern super()
- remove __future__ imports

etc.
This commit is contained in:
Eli Schwartz 2021-10-20 19:51:56 -04:00
parent 39a22268b9
commit fd467c4527
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
459 changed files with 1332 additions and 1691 deletions

View File

@ -1,4 +1,3 @@
''' E-book management software'''
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>'
@ -13,7 +12,7 @@ if not hasenv('CALIBRE_SHOW_DEPRECATION_WARNINGS'):
warnings.simplefilter('ignore', DeprecationWarning)
try:
os.getcwd()
except EnvironmentError:
except OSError:
os.chdir(os.path.expanduser('~'))
from calibre.constants import (iswindows, ismacos, islinux, isfrozen,
@ -372,7 +371,7 @@ class CurrentDir:
def __exit__(self, *args):
try:
os.chdir(self.cwd)
except EnvironmentError:
except OSError:
# The previous CWD no longer exists
pass

View File

@ -118,7 +118,7 @@ def _get_cache_dir():
confcache = os.path.join(config_dir, 'caches')
try:
os.makedirs(confcache)
except EnvironmentError as err:
except OSError as err:
if err.errno != errno.EEXIST:
raise
if isportable:
@ -129,7 +129,7 @@ def _get_cache_dir():
try:
os.makedirs(ans)
return ans
except EnvironmentError as err:
except OSError as err:
if err.errno == errno.EEXIST:
return ans
@ -151,7 +151,7 @@ def _get_cache_dir():
candidate = confcache
try:
os.makedirs(candidate)
except EnvironmentError as err:
except OSError as err:
if err.errno != errno.EEXIST:
candidate = confcache
return candidate

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

View File

@ -1814,7 +1814,7 @@ class StoreWeightlessBooksStore(StoreBase):
class StoreWHSmithUKStore(StoreBase):
name = 'WH Smith UK'
author = 'Charles Haley'
description = u"Shop for savings on Books, discounted Magazine subscriptions and great prices on Stationery, Toys & Games"
description = "Shop for savings on Books, discounted Magazine subscriptions and great prices on Stationery, Toys & Games"
actual_plugin = 'calibre.gui2.store.stores.whsmith_uk_plugin:WHSmithUKStore'
headquarters = 'UK'

View File

@ -31,8 +31,8 @@ class Plugin(_Plugin):
self.fsizes = []
for (name, num), size in zip(FONT_SIZES, fsizes):
self.fsizes.append((name, num, float(size)))
self.fnames = dict((name, sz) for name, _, sz in self.fsizes if name)
self.fnums = dict((num, sz) for _, num, sz in self.fsizes if num)
self.fnames = {name: sz for name, _, sz in self.fsizes if name}
self.fnums = {num: sz for _, num, sz in self.fsizes if num}
self.width_pts = self.width * 72./self.dpi
self.height_pts = self.height * 72./self.dpi
@ -486,7 +486,7 @@ class SonyReaderOutput(OutputProfile):
dpi = 168.451
fbase = 12
fsizes = [7.5, 9, 10, 12, 15.5, 20, 22, 24]
unsupported_unicode_chars = [u'\u201f', u'\u201b']
unsupported_unicode_chars = ['\u201f', '\u201b']
epub_periodical_format = 'sony'
# periodical_date_in_title = False

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
@ -769,8 +768,7 @@ initialize_plugins()
def initialized_plugins():
for plugin in _initialized_plugins:
yield plugin
yield from _initialized_plugins
# }}}
@ -786,7 +784,7 @@ def build_plugin(path):
if '__init__.py' not in names:
prints(path, ' is not a valid plugin')
raise SystemExit(1)
t = PersistentTemporaryFile(u'.zip')
t = PersistentTemporaryFile('.zip')
with ZipFile(t, 'w', ZIP_STORED) as zf:
zf.add_dir(path, simple_filter=lambda x:x in {'.git', '.bzr', '.svn', '.hg'})
t.close()
@ -852,7 +850,7 @@ def main(args=sys.argv):
for plugin in initialized_plugins():
type_len, name_len = max(type_len, len(plugin.type)), max(name_len, len(plugin.name))
fmt = '%-{}s%-{}s%-15s%-15s%s'.format(type_len+1, name_len+1)
print(fmt%tuple(('Type|Name|Version|Disabled|Site Customization'.split('|'))))
print(fmt%tuple('Type|Name|Version|Disabled|Site Customization'.split('|')))
print()
for plugin in initialized_plugins():
print(fmt%(

View File

@ -340,9 +340,9 @@ class CalibrePluginFinder:
break
else:
if self._identifier_pat.match(plugin_name) is None:
raise InvalidPlugin((
raise InvalidPlugin(
'The plugin at %r uses an invalid import name: %r' %
(path_to_zip_file, plugin_name)))
(path_to_zip_file, plugin_name))
pynames = [x for x in names if x.endswith('.py')]

View File

@ -77,10 +77,10 @@ def get_data_as_dict(self, prefix=None, authors_as_string=False, ids=None, conve
prefix = backend.library_path
fdata = backend.custom_column_num_map
FIELDS = set(['title', 'sort', 'authors', 'author_sort', 'publisher',
FIELDS = {'title', 'sort', 'authors', 'author_sort', 'publisher',
'rating', 'timestamp', 'size', 'tags', 'comments', 'series',
'series_index', 'uuid', 'pubdate', 'last_modified', 'identifiers',
'languages']).union(set(fdata))
'languages'}.union(set(fdata))
for x, data in iteritems(fdata):
if data['datatype'] == 'series':
FIELDS.add('%d_index'%x)

View File

@ -93,7 +93,7 @@ def listdir(root, sort_by_mtime=False):
def safe_mtime(x):
try:
return os.path.getmtime(x)
except EnvironmentError:
except OSError:
return time.time()
items = sorted(items, key=safe_mtime)
@ -230,8 +230,7 @@ def cdb_find_in_dir(dirpath, single_book_per_directory, compiled_rules):
def cdb_recursive_find(root, single_book_per_directory=True, compiled_rules=()):
root = os.path.abspath(root)
for dirpath in os.walk(root):
for formats in cdb_find_in_dir(dirpath[0], single_book_per_directory, compiled_rules):
yield formats
yield from cdb_find_in_dir(dirpath[0], single_book_per_directory, compiled_rules)
def add_catalog(cache, path, title, dbapi=None):

View File

@ -151,18 +151,18 @@ class DBPrefs(dict): # {{{
self.__setitem__(key, val)
def get_namespaced(self, namespace, key, default=None):
key = u'namespaced:%s:%s'%(namespace, key)
key = 'namespaced:%s:%s'%(namespace, key)
try:
return dict.__getitem__(self, key)
except KeyError:
return default
def set_namespaced(self, namespace, key, val):
if u':' in key:
if ':' in key:
raise KeyError('Colons are not allowed in keys')
if u':' in namespace:
if ':' in namespace:
raise KeyError('Colons are not allowed in the namespace')
key = u'namespaced:%s:%s'%(namespace, key)
key = 'namespaced:%s:%s'%(namespace, key)
self[key] = val
def write_serialized(self, library_path):
@ -261,7 +261,7 @@ def IdentifiersConcat():
'''String concatenation aggregator for the identifiers map'''
def step(ctxt, key, val):
ctxt.append(u'%s:%s'%(key, val))
ctxt.append('%s:%s'%(key, val))
def finalize(ctxt):
try:
@ -402,7 +402,7 @@ def set_global_state(backend):
def rmtree_with_retry(path, sleep_time=1):
try:
shutil.rmtree(path)
except EnvironmentError as e:
except OSError as e:
if e.errno == errno.ENOENT and not os.path.exists(path):
return
time.sleep(sleep_time) # In case something has temporarily locked a file
@ -645,7 +645,7 @@ class DB:
prints('found user category case overlap', catmap[uc])
cat = catmap[uc][0]
suffix = 1
while icu_lower((cat + str(suffix))) in catmap:
while icu_lower(cat + str(suffix)) in catmap:
suffix += 1
prints('Renaming user category %s to %s'%(cat, cat+str(suffix)))
user_cats[cat + str(suffix)] = user_cats[cat]
@ -759,7 +759,7 @@ class DB:
x = [y.strip() for y in x if y.strip()]
x = [y.decode(preferred_encoding, 'replace') if not isinstance(y,
str) else y for y in x]
return [u' '.join(y.split()) for y in x]
return [' '.join(y.split()) for y in x]
else:
return x if x is None or isinstance(x, str) else \
x.decode(preferred_encoding, 'replace')
@ -824,7 +824,7 @@ class DB:
else:
is_category = False
is_m = v['multiple_seps']
tn = 'custom_column_{0}'.format(v['num'])
tn = 'custom_column_{}'.format(v['num'])
self.field_metadata.add_custom_field(label=v['label'],
table=tn, column='value', datatype=v['datatype'],
colnum=v['num'], name=v['name'], display=v['display'],
@ -1428,7 +1428,7 @@ class DB:
path = os.path.abspath(os.path.join(self.library_path, path, 'cover.jpg'))
try:
return utcfromtimestamp(os.stat(path).st_mtime)
except EnvironmentError:
except OSError:
pass # Cover doesn't exist
def copy_cover_to(self, path, dest, windows_atomic_move=None, use_hardlink=False, report_file_size=None):
@ -1444,11 +1444,11 @@ class DB:
if os.access(path, os.R_OK):
try:
f = lopen(path, 'rb')
except (IOError, OSError):
except OSError:
time.sleep(0.2)
try:
f = lopen(path, 'rb')
except (IOError, OSError) as e:
except OSError as e:
# Ensure the path that caused this error is reported
raise Exception('Failed to open %r with error: %s' % (path, e))
@ -1478,13 +1478,13 @@ class DB:
path = os.path.abspath(os.path.join(self.library_path, path, 'cover.jpg'))
try:
stat = os.stat(path)
except EnvironmentError:
except OSError:
return False, None, None
if abs(timestamp - stat.st_mtime) < 0.1:
return True, None, None
try:
f = lopen(path, 'rb')
except (IOError, OSError):
except OSError:
time.sleep(0.2)
f = lopen(path, 'rb')
with f:
@ -1519,7 +1519,7 @@ class DB:
if os.path.exists(path):
try:
os.remove(path)
except (IOError, OSError):
except OSError:
time.sleep(0.2)
os.remove(path)
else:
@ -1529,7 +1529,7 @@ class DB:
else:
try:
save_cover_data_to(data, path)
except (IOError, OSError):
except OSError:
time.sleep(0.2)
save_cover_data_to(data, path)
@ -1615,7 +1615,7 @@ class DB:
# wrong in the rest of this function, at least the file is
# not deleted
os.rename(old_path, dest)
except EnvironmentError as e:
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
# Failing to rename the old format will at worst leave a
# harmless orphan, so log and ignore the error
@ -1727,11 +1727,11 @@ class DB:
try:
with lopen(path, 'wb') as f:
f.write(raw)
except EnvironmentError:
except OSError:
exc_info = sys.exc_info()
try:
os.makedirs(os.path.dirname(path))
except EnvironmentError as err:
except OSError as err:
if err.errno == errno.EEXIST:
# Parent directory already exists, re-raise original exception
reraise(*exc_info)
@ -1810,8 +1810,7 @@ class DB:
return frozenset(r[0] for r in self.execute('SELECT book FROM books_plugin_data WHERE name=?', (name,)))
def annotations_for_book(self, book_id, fmt, user_type, user):
for x in annotations_for_book(self.conn, book_id, fmt, user_type, user):
yield x
yield from annotations_for_book(self.conn, book_id, fmt, user_type, user)
def search_annotations(self,
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, annotation_type,
@ -2080,18 +2079,18 @@ class DB:
for loc in old_dirs:
try:
rmtree_with_retry(loc)
except EnvironmentError as e:
except OSError as e:
if os.path.exists(loc):
prints('Failed to delete:', loc, 'with error:', as_unicode(e))
for loc in old_files:
try:
os.remove(loc)
except EnvironmentError as e:
except OSError as e:
if e.errno != errno.ENOENT:
prints('Failed to delete:', loc, 'with error:', as_unicode(e))
try:
os.rmdir(odir)
except EnvironmentError:
except OSError:
pass
self.conn # Connect to the moved metadata.db
progress(_('Completed'), total, total)

View File

@ -1327,7 +1327,7 @@ class Cache:
try:
return self.backend.read_backup(path)
except EnvironmentError:
except OSError:
return None
@write_api

View File

@ -44,7 +44,7 @@ class Tag:
@property
def string_representation(self):
return u'%s:%s:%s:%s:%s'%(self.name, self.count, self.id, self.state, self.category)
return '%s:%s:%s:%s:%s'%(self.name, self.count, self.id, self.state, self.category)
def __str__(self):
return self.string_representation

View File

@ -263,7 +263,7 @@ def do_add(
try:
with lopen(mi.cover, 'rb') as f:
cover_data = f.read()
except EnvironmentError:
except OSError:
pass
book_title, ids, mids, dups = dbctx.run(
@ -462,8 +462,8 @@ def main(opts, args, dbctx):
lcodes = [canonicalize_lang(x) for x in (opts.languages or '').split(',')]
lcodes = [x for x in lcodes if x]
identifiers = (x.partition(':')[::2] for x in opts.identifier)
identifiers = dict((k.strip(), v.strip()) for k, v in identifiers
if k.strip() and v.strip())
identifiers = {k.strip(): v.strip() for k, v in identifiers
if k.strip() and v.strip()}
if opts.empty:
do_add_empty(
dbctx, opts.title, aut, opts.isbn, tags, opts.series, opts.series_index,

View File

@ -54,7 +54,7 @@ class BackupProgress:
else:
self.count += 1
prints(
u'%.1f%% %s - %s' % ((self.count * 100) / float(self.total), book_id,
'%.1f%% %s - %s' % ((self.count * 100) / float(self.total), book_id,
getattr(mi, 'title', 'Unknown'))
)

View File

@ -88,7 +88,7 @@ class DeleteService(Thread):
basename = '%d - %s' % (c, os.path.basename(path))
try:
shutil.move(path, dest)
except EnvironmentError:
except OSError:
if os.path.isdir(path):
# shutil.move may have partially copied the directory,
# so the subsequent call to move() will fail as the

View File

@ -328,8 +328,7 @@ class CompositeField(OneToOneField):
for v in vals:
if v:
val_map[v].add(book_id)
for val, book_ids in iteritems(val_map):
yield val, book_ids
yield from iteritems(val_map)
def iter_counts(self, candidates, get_metadata=None):
val_map = defaultdict(set)
@ -343,8 +342,7 @@ class CompositeField(OneToOneField):
else:
length = 0
val_map[length].add(book_id)
for val, book_ids in iteritems(val_map):
yield val, book_ids
yield from iteritems(val_map)
def get_composite_categories(self, tag_class, book_rating_map, book_ids,
is_multiple, get_metadata):
@ -437,8 +435,7 @@ class OnDeviceField(OneToOneField):
val_map = defaultdict(set)
for book_id in candidates:
val_map[self.for_book(book_id, default_value=default_value)].add(book_id)
for val, book_ids in iteritems(val_map):
yield val, book_ids
yield from iteritems(val_map)
class LazySortMap:
@ -562,8 +559,7 @@ class ManyToManyField(Field):
cbm = self.table.book_col_map
for book_id in candidates:
val_map[len(cbm.get(book_id, ()))].add(book_id)
for count, book_ids in iteritems(val_map):
yield count, book_ids
yield from iteritems(val_map)
@property
def book_value_map(self):

View File

@ -29,7 +29,7 @@ def cleanup_tags(tags):
tags = [x.strip().replace(',', ';') for x in tags if x.strip()]
tags = [x.decode(preferred_encoding, 'replace')
if isbytestring(x) else x for x in tags]
tags = [u' '.join(x.split()) for x in tags]
tags = [' '.join(x.split()) for x in tags]
ans, seen = [], set()
for tag in tags:
if tag.lower() not in seen:
@ -684,8 +684,7 @@ class LibraryDatabase:
self.new_api.refresh_ondevice()
def tags_older_than(self, tag, delta, must_have_tag=None, must_have_authors=None):
for book_id in sorted(self.new_api.tags_older_than(tag, delta=delta, must_have_tag=must_have_tag, must_have_authors=must_have_authors)):
yield book_id
yield from sorted(self.new_api.tags_older_than(tag, delta=delta, must_have_tag=must_have_tag, must_have_authors=must_have_authors))
def sizeof_format(self, index, fmt, index_is_id=False):
book_id = index if index_is_id else self.id(index)
@ -848,8 +847,7 @@ class LibraryDatabase:
# Private interface {{{
def __iter__(self):
for row in self.data.iterall():
yield row
yield from self.data.iterall()
def _get_next_series_num_for_list(self, series_indices):
return _get_next_series_num_for_list(series_indices)

View File

@ -42,7 +42,7 @@ class Restorer(Cache):
class Restore(Thread):
def __init__(self, library_path, progress_callback=None):
super(Restore, self).__init__()
super().__init__()
if isbytestring(library_path):
library_path = library_path.decode(filesystem_encoding)
self.src_library_path = os.path.abspath(library_path)
@ -107,7 +107,7 @@ class Restore(Thread):
try:
tdir = TemporaryDirectory('_rlib', dir=basedir)
tdir.__enter__()
except EnvironmentError:
except OSError:
# In case we dont have permissions to create directories in the
# parent folder of the src library
tdir = TemporaryDirectory('_rlib')
@ -279,7 +279,7 @@ class Restore(Thread):
if os.path.exists(dbpath):
try:
os.rename(dbpath, save_path)
except EnvironmentError:
except OSError:
time.sleep(30) # Wait a little for dropbox or the antivirus or whatever to release the file
shutil.copyfile(dbpath, save_path)
os.remove(dbpath)

View File

@ -103,7 +103,7 @@ class OneToOneTable(Table):
def read(self, db):
idcol = 'id' if self.metadata['table'] == 'books' else 'book'
query = db.execute('SELECT {0}, {1} FROM {2}'.format(idcol,
query = db.execute('SELECT {}, {} FROM {}'.format(idcol,
self.metadata['column'], self.metadata['table']))
if self.unserialize is None:
try:
@ -111,7 +111,7 @@ class OneToOneTable(Table):
except UnicodeDecodeError:
# The db is damaged, try to work around it by ignoring
# failures to decode utf-8
query = db.execute('SELECT {0}, cast({1} as blob) FROM {2}'.format(idcol,
query = db.execute('SELECT {}, cast({} as blob) FROM {}'.format(idcol,
self.metadata['column'], self.metadata['table']))
self.book_col_map = {k:bytes(val).decode('utf-8', 'replace') for k, val in query}
else:
@ -205,7 +205,7 @@ class ManyToOneTable(Table):
self.read_maps(db)
def read_id_maps(self, db):
query = db.execute('SELECT id, {0} FROM {1}'.format(
query = db.execute('SELECT id, {} FROM {}'.format(
self.metadata['column'], self.metadata['table']))
if self.unserialize is None:
self.id_map = dict(query)
@ -217,7 +217,7 @@ class ManyToOneTable(Table):
cbm = self.col_book_map
bcm = self.book_col_map
for book, item_id in db.execute(
'SELECT book, {0} FROM {1}'.format(
'SELECT book, {} FROM {}'.format(
self.metadata['link_column'], self.link_table)):
cbm[item_id].add(book)
bcm[book] = item_id
@ -230,7 +230,7 @@ class ManyToOneTable(Table):
book_ids = self.col_book_map.pop(item_id, ())
for book_id in book_ids:
self.book_col_map.pop(book_id, None)
db.executemany('DELETE FROM {0} WHERE {1}=?'.format(
db.executemany('DELETE FROM {} WHERE {}=?'.format(
self.link_table, self.metadata['link_column']), tuple((x,) for x in extra_item_ids))
def fix_case_duplicates(self, db):
@ -250,7 +250,7 @@ class ManyToOneTable(Table):
db.executemany('UPDATE {0} SET {1}=? WHERE {1}=?'.format(
self.link_table, self.metadata['link_column']),
tuple((main_id, x) for x in v))
db.executemany('DELETE FROM {0} WHERE id=?'.format(self.metadata['table']),
db.executemany('DELETE FROM {} WHERE id=?'.format(self.metadata['table']),
tuple((x,) for x in v))
def remove_books(self, book_ids, db):
@ -270,7 +270,7 @@ class ManyToOneTable(Table):
clean.add(item_id)
if clean:
db.executemany(
'DELETE FROM {0} WHERE id=?'.format(self.metadata['table']),
'DELETE FROM {} WHERE id=?'.format(self.metadata['table']),
[(x,) for x in clean])
return clean
@ -296,7 +296,7 @@ class ManyToOneTable(Table):
# this is a many-to-one mapping we know that we can delete
# links without checking the item ID
db.executemany(
'DELETE FROM {0} WHERE book=?'.format(self.link_table), tuple((x,) for x in books_to_delete))
'DELETE FROM {} WHERE book=?'.format(self.link_table), tuple((x,) for x in books_to_delete))
affected_books |= books_to_delete
else:
# Process normally any items where the VL was not significant
@ -314,8 +314,8 @@ class ManyToOneTable(Table):
self.book_col_map.pop(book_id, None)
affected_books.update(book_ids)
item_ids = tuple((x,) for x in item_ids)
db.executemany('DELETE FROM {0} WHERE {1}=?'.format(self.link_table, self.metadata['link_column']), item_ids)
db.executemany('DELETE FROM {0} WHERE id=?'.format(self.metadata['table']), item_ids)
db.executemany('DELETE FROM {} WHERE {}=?'.format(self.link_table, self.metadata['link_column']), item_ids)
db.executemany('DELETE FROM {} WHERE id=?'.format(self.metadata['table']), item_ids)
return affected_books
def rename_item(self, item_id, new_name, db):
@ -327,7 +327,7 @@ class ManyToOneTable(Table):
if existing_item is None or existing_item == item_id:
# A simple rename will do the trick
self.id_map[item_id] = new_name
db.execute('UPDATE {0} SET {1}=? WHERE id=?'.format(table, col), (new_name, item_id))
db.execute('UPDATE {} SET {}=? WHERE id=?'.format(table, col), (new_name, item_id))
else:
# We have to replace
new_id = existing_item
@ -353,9 +353,9 @@ class RatingTable(ManyToOneTable):
bad_ids = {item_id for item_id, rating in iteritems(self.id_map) if rating == 0}
if bad_ids:
self.id_map = {item_id:rating for item_id, rating in iteritems(self.id_map) if rating != 0}
db.executemany('DELETE FROM {0} WHERE {1}=?'.format(self.link_table, self.metadata['link_column']),
db.executemany('DELETE FROM {} WHERE {}=?'.format(self.link_table, self.metadata['link_column']),
tuple((x,) for x in bad_ids))
db.execute('DELETE FROM {0} WHERE {1}=0'.format(
db.execute('DELETE FROM {} WHERE {}=0'.format(
self.metadata['table'], self.metadata['column']))
@ -389,7 +389,7 @@ class ManyToManyTable(ManyToOneTable):
book_ids = self.col_book_map.pop(item_id, ())
for book_id in book_ids:
self.book_col_map[book_id] = tuple(iid for iid in self.book_col_map.pop(book_id, ()) if iid not in extra_item_ids)
db.executemany('DELETE FROM {0} WHERE {1}=?'.format(
db.executemany('DELETE FROM {} WHERE {}=?'.format(
self.link_table, self.metadata['link_column']), tuple((x,) for x in extra_item_ids))
def remove_books(self, book_ids, db):
@ -409,7 +409,7 @@ class ManyToManyTable(ManyToOneTable):
clean.add(item_id)
if clean and self.do_clean_on_remove:
db.executemany(
'DELETE FROM {0} WHERE id=?'.format(self.metadata['table']),
'DELETE FROM {} WHERE id=?'.format(self.metadata['table']),
[(x,) for x in clean])
return clean
@ -436,7 +436,7 @@ class ManyToManyTable(ManyToOneTable):
# Delete book/item pairs from the link table. We don't need to do
# anything with the main table because books with the old ID are
# still in the library.
db.executemany('DELETE FROM {0} WHERE {1}=? and {2}=?'.format(
db.executemany('DELETE FROM {} WHERE {}=? and {}=?'.format(
self.link_table, 'book', self.metadata['link_column']),
[(b, i) for b in affected_books for i in item_ids])
# Take care of any items where the VL was not significant
@ -453,8 +453,8 @@ class ManyToManyTable(ManyToOneTable):
self.book_col_map[book_id] = tuple(x for x in self.book_col_map.get(book_id, ()) if x != item_id)
affected_books.update(book_ids)
item_ids = tuple((x,) for x in item_ids)
db.executemany('DELETE FROM {0} WHERE {1}=?'.format(self.link_table, self.metadata['link_column']), item_ids)
db.executemany('DELETE FROM {0} WHERE id=?'.format(self.metadata['table']), item_ids)
db.executemany('DELETE FROM {} WHERE {}=?'.format(self.link_table, self.metadata['link_column']), item_ids)
db.executemany('DELETE FROM {} WHERE id=?'.format(self.metadata['table']), item_ids)
return affected_books
def rename_item(self, item_id, new_name, db):
@ -466,7 +466,7 @@ class ManyToManyTable(ManyToOneTable):
if existing_item is None or existing_item == item_id:
# A simple rename will do the trick
self.id_map[item_id] = new_name
db.execute('UPDATE {0} SET {1}=? WHERE id=?'.format(table, col), (new_name, item_id))
db.execute('UPDATE {} SET {}=? WHERE id=?'.format(table, col), (new_name, item_id))
else:
# We have to replace
new_id = existing_item
@ -478,7 +478,7 @@ class ManyToManyTable(ManyToOneTable):
for book_id in books:
self.book_col_map[book_id] = tuple((existing_item if x == item_id else x) for x in self.book_col_map.get(book_id, ()) if x != existing_item)
self.col_book_map[existing_item].update(books)
db.executemany('DELETE FROM {0} WHERE book=? AND {1}=?'.format(self.link_table, lcol), [
db.executemany('DELETE FROM {} WHERE book=? AND {}=?'.format(self.link_table, lcol), [
(book_id, existing_item) for book_id in books])
db.execute('UPDATE {0} SET {1}=? WHERE {1}=?; DELETE FROM {2} WHERE id=?'.format(
self.link_table, lcol, table), (existing_item, item_id, item_id))
@ -515,11 +515,11 @@ class ManyToManyTable(ManyToOneTable):
tuple((main_id, x, book_id) for x in v))
else:
# duplicates
db.execute('DELETE FROM {0} WHERE book=?'.format(self.link_table), (book_id,))
db.execute('DELETE FROM {} WHERE book=?'.format(self.link_table), (book_id,))
db.executemany(
'INSERT INTO {0} (book,{1}) VALUES (?,?)'.format(self.link_table, self.metadata['link_column']),
'INSERT INTO {} (book,{}) VALUES (?,?)'.format(self.link_table, self.metadata['link_column']),
tuple((book_id, x) for x in vals))
db.executemany('DELETE FROM {0} WHERE id=?'.format(self.metadata['table']),
db.executemany('DELETE FROM {} WHERE id=?'.format(self.metadata['table']),
tuple((x,) for x in v))

View File

@ -32,7 +32,7 @@ class BaseTest(unittest.TestCase):
gc.collect(), gc.collect()
try:
shutil.rmtree(self.library_path)
except EnvironmentError:
except OSError:
# Try again in case something transient has a file lock on windows
gc.collect(), gc.collect()
time.sleep(2)

View File

@ -62,7 +62,7 @@ def run_funcs(self, db, ndb, funcs):
if meth[0] in {'!', '@', '#', '+', '$', '-', '%'}:
if meth[0] != '+':
fmt = {'!':dict, '@':lambda x:frozenset(x or ()), '#':lambda x:set((x or '').split(',')),
'$':lambda x:set(tuple(y) for y in x), '-':lambda x:None,
'$':lambda x:{tuple(y) for y in x}, '-':lambda x:None,
'%':lambda x: set((x or '').split(','))}[meth[0]]
else:
fmt = args[-1]
@ -516,7 +516,7 @@ class LegacyTest(BaseTest):
T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
T((name, object()))
T = partial(ET, 'delete_all_custom_book_data', old=old, legacy=legacy)
T((name))
T(name)
T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
T((name, object()))

View File

@ -136,7 +136,7 @@ class ThumbnailCache:
def _do_delete(self, path):
try:
os.remove(path)
except EnvironmentError as err:
except OSError as err:
self.log('Failed to delete cached thumbnail file:', as_unicode(err))
def _load_index(self):
@ -153,7 +153,7 @@ class ThumbnailCache:
def listdir(*args):
try:
return os.listdir(os.path.join(*args))
except EnvironmentError:
except OSError:
return () # not a directory or no permission or whatever
entries = ('/'.join((parent, subdir, entry))
for parent in listdir(self.location)
@ -164,13 +164,13 @@ class ThumbnailCache:
try:
with open(os.path.join(self.location, 'invalidate'), 'rb') as f:
raw = f.read().decode('utf-8')
except EnvironmentError as err:
except OSError as err:
if getattr(err, 'errno', None) != errno.ENOENT:
self.log('Failed to read thumbnail invalidate data:', as_unicode(err))
else:
try:
os.remove(os.path.join(self.location, 'invalidate'))
except EnvironmentError as err:
except OSError as err:
self.log('Failed to remove thumbnail invalidate data:', as_unicode(err))
else:
def record(line):
@ -198,7 +198,7 @@ class ThumbnailCache:
self.total_size += size
else:
self._do_delete(path)
except EnvironmentError as err:
except OSError as err:
self.log('Failed to read thumbnail cache dir:', as_unicode(err))
self.items = OrderedDict(sorted(items, key=lambda x:order.get(x[0], 0)))
@ -230,7 +230,7 @@ class ThumbnailCache:
data = '\n'.join(group_id + ' ' + str(book_id) for (group_id, book_id) in self.items)
with lopen(os.path.join(self.location, 'order'), 'wb') as f:
f.write(data.encode('utf-8'))
except EnvironmentError as err:
except OSError as err:
self.log('Failed to save thumbnail cache order:', as_unicode(err))
def _read_order(self):
@ -281,14 +281,14 @@ class ThumbnailCache:
try:
with open(path, 'wb') as f:
f.write(data)
except EnvironmentError as err:
except OSError as err:
d = os.path.dirname(path)
if not os.path.exists(d):
try:
os.makedirs(d)
with open(path, 'wb') as f:
f.write(data)
except EnvironmentError as err:
except OSError as err:
self.log('Failed to write cached thumbnail:', path, as_unicode(err))
return self._apply_size()
else:
@ -326,7 +326,7 @@ class ThumbnailCache:
if entry.thumbnail_size != self.thumbnail_size:
try:
os.remove(entry.path)
except EnvironmentError as err:
except OSError as err:
if getattr(err, 'errno', None) != errno.ENOENT:
self.log('Failed to remove cached thumbnail:', entry.path, as_unicode(err))
self.total_size -= entry.size
@ -335,7 +335,7 @@ class ThumbnailCache:
try:
with open(entry.path, 'rb') as f:
data = f.read()
except EnvironmentError as err:
except OSError as err:
self.log('Failed to read cached thumbnail:', entry.path, as_unicode(err))
return None, None
return data, entry.timestamp
@ -350,7 +350,7 @@ class ThumbnailCache:
raw = '\n'.join('%s %d' % (self.group_id, book_id) for book_id in book_ids)
with open(os.path.join(self.location, 'invalidate'), 'ab') as f:
f.write(raw.encode('ascii'))
except EnvironmentError as err:
except OSError as err:
self.log('Failed to write invalidate thumbnail record:', as_unicode(err))
@property
@ -364,7 +364,7 @@ class ThumbnailCache:
with self.lock:
try:
os.remove(os.path.join(self.location, 'order'))
except EnvironmentError:
except OSError:
pass
if not hasattr(self, 'total_size'):
self._load_index()

View File

@ -174,8 +174,7 @@ class View:
yield TableRow(book_id, self)
def iterallids(self):
for book_id in sorted(self._map):
yield book_id
yield from sorted(self._map)
def tablerow_for_id(self, book_id):
return TableRow(book_id, self)
@ -280,7 +279,7 @@ class View:
def _build_restriction_string(self, restriction):
if self.base_restriction:
if restriction:
return u'(%s) and (%s)' % (self.base_restriction, restriction)
return '(%s) and (%s)' % (self.base_restriction, restriction)
else:
return self.base_restriction
else:
@ -296,7 +295,7 @@ class View:
else:
q = query
if search_restriction:
q = u'(%s) and (%s)' % (search_restriction, query)
q = '(%s) and (%s)' % (search_restriction, query)
if not q:
if set_restriction_count:
self.search_restriction_book_count = len(self._map)
@ -373,7 +372,7 @@ class View:
old_marked_ids = set(self.marked_ids)
if not hasattr(id_dict, 'items'):
# Simple list. Make it a dict of string 'true'
self.marked_ids = dict.fromkeys(id_dict, u'true')
self.marked_ids = dict.fromkeys(id_dict, 'true')
else:
# Ensure that all the items in the dict are text
self.marked_ids = {k: str(v) for k, v in iteritems(id_dict)}

View File

@ -471,7 +471,7 @@ def many_many(book_id_val_map, db, field, allow_case_change, *args):
)
db.executemany('DELETE FROM %s WHERE book=?'%table.link_table,
((k,) for k in updated))
db.executemany('INSERT INTO {0}(book,{1}) VALUES(?, ?)'.format(
db.executemany('INSERT INTO {}(book,{}) VALUES(?, ?)'.format(
table.link_table, m['link_column']), vals)
if is_authors:
aus_map = {book_id:field.author_sort_for_book(book_id) for book_id

View File

@ -208,7 +208,7 @@ def print_basic_debug_info(out=None):
from calibre.customize.ui import has_external_plugins, initialized_plugins
if has_external_plugins():
from calibre.customize import PluginInstallationType
names = ('{0} {1}'.format(p.name, p.version) for p in initialized_plugins()
names = ('{} {}'.format(p.name, p.version) for p in initialized_plugins()
if getattr(p, 'installation_type', None) is not PluginInstallationType.BUILTIN)
out('Successfully initialized third party plugins:', ' && '.join(names))

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2011, Ken <ken at szboeye.com>'
__docformat__ = 'restructuredtext en'

View File

@ -301,7 +301,7 @@ def main():
outfile = os.path.join(outfile, path[path.rfind("/")+1:])
try:
outfile = lopen(outfile, "wb")
except IOError as e:
except OSError as e:
print(e, file=sys.stderr)
parser.print_help()
return 1
@ -311,7 +311,7 @@ def main():
elif args[1].startswith("dev:"):
try:
infile = lopen(args[0], "rb")
except IOError as e:
except OSError as e:
print(e, file=sys.stderr)
parser.print_help()
return 1

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john at nachtimwald.com>'
'''

View File

@ -1 +0,0 @@

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
"""

View File

@ -1,5 +1,3 @@
'''
Created on 15 May 2010
@ -63,7 +61,7 @@ class FOLDER_DEVICE(USBMS):
def __init__(self, path):
if not os.path.isdir(path):
raise IOError('Path is not a folder')
raise OSError('Path is not a folder')
path = USBMS.normalize_path(path)
if path.endswith(os.sep):
self._main_prefix = path

View File

@ -1,4 +1,2 @@
__license__ = 'GPL v3'
__copyright__ = '2009, Tijmen Ruizendaal <tijmen at mybebook.com>'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import os

View File

@ -1 +0,0 @@

View File

@ -154,7 +154,7 @@ class Bookmark(): # {{{
split = my_clippings.find('documents') + len('documents/')
my_clippings = my_clippings[:split] + "My Clippings.txt"
try:
with io.open(my_clippings, encoding='utf-8', errors='replace') as f2:
with open(my_clippings, encoding='utf-8', errors='replace') as f2:
marker_found = 0
text = ''
search_str1 = '%s' % (mi.title)

View File

@ -232,7 +232,7 @@ class KINDLE(USBMS):
pr=percent_read)
else:
markup = _("%(time)s<br />Last page read: Location %(loc)d (%(pr)d%%)") % dict(
time=strftime(u'%x', timestamp.timetuple()),
time=strftime('%x', timestamp.timetuple()),
loc=last_read_location,
pr=percent_read)
spanTag = BeautifulSoup('<span style="font-weight:bold">' + markup + '</span>').find('span')
@ -313,7 +313,7 @@ class KINDLE(USBMS):
bm.value.path, index_is_id=True)
elif bm.type == 'kindle_clippings':
# Find 'My Clippings' author=Kindle in database, or add
last_update = 'Last modified %s' % strftime(u'%x %X',bm.value['timestamp'].timetuple())
last_update = 'Last modified %s' % strftime('%x %X',bm.value['timestamp'].timetuple())
mc_id = list(db.data.search_getting_ids('title:"My Clippings"', '', sort_results=False))
if mc_id:
db.add_format_with_hooks(mc_id[0], 'TXT', bm.value['path'],
@ -524,7 +524,7 @@ class KINDLE2(KINDLE):
cache_dir = self.amazon_cover_bug_cache_dir()
try:
os.mkdir(cache_dir)
except EnvironmentError:
except OSError:
pass
with lopen(os.path.join(cache_dir, os.path.basename(tp)), 'wb') as f:
f.write(coverdata[2])
@ -545,7 +545,7 @@ class KINDLE2(KINDLE):
dest_path = os.path.join(dest_dir, name)
try:
dest_stat_result = os.lstat(dest_path)
except EnvironmentError:
except OSError:
needs_sync = True
else:
needs_sync = src_stat_result.st_size != dest_stat_result.st_size
@ -567,7 +567,7 @@ class KINDLE2(KINDLE):
for tp in (tp1, tp2):
try:
os.remove(tp)
except EnvironmentError as err:
except OSError as err:
if err.errno != errno.ENOENT:
prints('Failed to delete thumbnail for {!r} at {!r} with error: {}'.format(path, tp, err))
except Exception:

View File

@ -57,7 +57,7 @@ class Bookmark(): # {{{
'ORDER BY bm.ContentID, bm.chapterprogress'
)
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapters: contentId={0}".format(self.contentId))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapters: contentId={}".format(self.contentId))
cursor.execute(kepub_chapter_query, book_query_values)
kepub_chapters = {}
if self.kepub:
@ -69,7 +69,7 @@ class Bookmark(): # {{{
'chapter_title': chapter_row['Title'],
'chapter_index': chapter_row['VolumeIndex']
}
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapter: kepub chapters={0}".format(kepub_chapters))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapter: kepub chapters={}".format(kepub_chapters))
except:
debug_print("Kobo::Bookmark::get_bookmark_data - No chapters found")
@ -83,20 +83,20 @@ class Bookmark(): # {{{
# For kepubs on newer firmware, the title needs to come from an 899 row.
if self.kepub:
chapter_contentID = row['ContentID']
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter chapter_contentID='{0}'".format(chapter_contentID))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter chapter_contentID='{}'".format(chapter_contentID))
filename_index = chapter_contentID.find('!')
book_contentID_part = chapter_contentID[:filename_index]
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter book_contentID_part='{0}'".format(book_contentID_part))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter book_contentID_part='{}'".format(book_contentID_part))
file_contentID_part = chapter_contentID[filename_index + 1:]
filename_index = file_contentID_part.find('!')
opf_reference = file_contentID_part[:filename_index]
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter opf_reference='{0}'".format(opf_reference))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter opf_reference='{}'".format(opf_reference))
file_contentID_part = file_contentID_part[filename_index + 1:]
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter file_contentID_part='{0}'".format(file_contentID_part))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub: chapter file_contentID_part='{}'".format(file_contentID_part))
# from urllib import quote
# file_contentID_part = quote(file_contentID_part)
chapter_contentID = book_contentID_part + "!" + opf_reference + "!" + file_contentID_part
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapter chapter_contentID='{0}'".format(chapter_contentID))
debug_print("Kobo::Bookmark::get_bookmark_data - getting kepub chapter chapter_contentID='{}'".format(chapter_contentID))
kepub_chapter = kepub_chapters.get(chapter_contentID, None)
if kepub_chapter is not None:
chapter_title = kepub_chapter['chapter_title']

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2010-2012, , Timothy Legge <timlegge at gmail.com> and David Forrester <davidfor@internode.on.net>'
__docformat__ = 'restructuredtext en'
@ -29,7 +28,7 @@ class Book(Book_):
if show_debug:
debug_print("Book::__init__ - title=", title, 'authors=', authors)
debug_print("Book::__init__ - other=", other)
super(Book, self).__init__(prefix, lpath, size, other)
super().__init__(prefix, lpath, size, other)
if title is not None and len(title) > 0:
self.title = title
@ -117,7 +116,7 @@ class Book(Book_):
ans = '\n'.join(ans)
return super(Book,self).__str__() + "\n" + ans
return super().__str__() + "\n" + ans
class ImageWrapper:
@ -129,7 +128,7 @@ class ImageWrapper:
class KTCollectionsBookList(CollectionsBookList):
def __init__(self, oncard, prefix, settings):
super(KTCollectionsBookList, self).__init__(oncard, prefix, settings)
super().__init__(oncard, prefix, settings)
self.set_device_managed_collections([])
def get_collections(self, collection_attributes):

View File

@ -203,7 +203,7 @@ class KOBO(USBMS):
try:
with lopen(self.normalize_path(self._main_prefix + '.kobo/version'), 'rb') as f:
fwversion = f.readline().split(b',')[2]
fwversion = tuple((int(x) for x in fwversion.split(b'.')))
fwversion = tuple(int(x) for x in fwversion.split(b'.'))
except Exception:
debug_print("Kobo::get_firmware_version - didn't get firmware version from file'")
fwversion = (0,0,0)
@ -1138,7 +1138,7 @@ class KOBO(USBMS):
def get_annotations(self, path_map):
from calibre.devices.kobo.bookmark import Bookmark
EPUB_FORMATS = [u'epub']
EPUB_FORMATS = ['epub']
epub_formats = set(EPUB_FORMATS)
def get_storage():
@ -1519,21 +1519,21 @@ class KOBOTOUCH(KOBO):
self.plugboards = self.plugboard_func = None
def initialize(self):
super(KOBOTOUCH, self).initialize()
super().initialize()
self.bookshelvelist = []
def get_device_information(self, end_session=True):
self.set_device_name()
return super(KOBOTOUCH, self).get_device_information(end_session)
return super().get_device_information(end_session)
def open_linux(self):
super(KOBOTOUCH, self).open_linux()
super().open_linux()
self.swap_drives_if_needed()
def open_osx(self):
# Just dump some info to the logs.
super(KOBOTOUCH, self).open_osx()
super().open_osx()
# Wrap some debugging output in a try/except so that it is unlikely to break things completely.
try:
@ -2049,7 +2049,7 @@ class KOBOTOUCH(KOBO):
path = ContentID
if not externalId:
return super(KOBOTOUCH, self).path_from_contentid(ContentID, ContentType, MimeType, oncard)
return super().path_from_contentid(ContentID, ContentType, MimeType, oncard)
if oncard == 'cardb':
print('path from_contentid cardb')
@ -2099,13 +2099,13 @@ class KOBOTOUCH(KOBO):
from css_parser import parseFile as cssparseFile
try:
extra_sheet = cssparseFile(extra_css_path)
debug_print("KoboTouch:get_extra_css: Using extra CSS in {0} ({1} rules)".format(extra_css_path, len(extra_sheet.cssRules)))
debug_print("KoboTouch:get_extra_css: Using extra CSS in {} ({} rules)".format(extra_css_path, len(extra_sheet.cssRules)))
if len(extra_sheet.cssRules) ==0:
debug_print("KoboTouch:get_extra_css: Extra CSS file has no valid rules. CSS will not be modified.")
extra_sheet = None
except Exception as e:
debug_print("KoboTouch:get_extra_css: Problem parsing extra CSS file {0}".format(extra_css_path))
debug_print("KoboTouch:get_extra_css: Exception {0}".format(e))
debug_print("KoboTouch:get_extra_css: Problem parsing extra CSS file {}".format(extra_css_path))
debug_print("KoboTouch:get_extra_css: Exception {}".format(e))
# create dictionary of features enabled in kobo extra css
self.extra_css_options = {}
@ -2136,16 +2136,16 @@ class KOBOTOUCH(KOBO):
self.extra_sheet = self.get_extra_css()
i = 0
for file, n, mi in zip(files, names, metadata):
debug_print("KoboTouch:upload_books: Processing book: {0} by {1}".format(mi.title, " and ".join(mi.authors)))
debug_print("KoboTouch:upload_books: Processing book: {} by {}".format(mi.title, " and ".join(mi.authors)))
debug_print("KoboTouch:upload_books: file=%s, name=%s" % (file, n))
self.report_progress(i / float(len(files)), "Processing book: {0} by {1}".format(mi.title, " and ".join(mi.authors)))
self.report_progress(i / float(len(files)), "Processing book: {} by {}".format(mi.title, " and ".join(mi.authors)))
mi.kte_calibre_name = n
self._modify_epub(file, mi)
i += 1
self.report_progress(0, 'Working...')
result = super(KOBOTOUCH, self).upload_books(files, names, on_card, end_session, metadata)
result = super().upload_books(files, names, on_card, end_session, metadata)
# debug_print('KoboTouch:upload_books - result=', result)
if self.dbversion >= 53:
@ -2179,7 +2179,7 @@ class KOBOTOUCH(KOBO):
return result
def _modify_epub(self, book_file, metadata, container=None):
debug_print("KoboTouch:_modify_epub:Processing {0} - {1}".format(metadata.author_sort, metadata.title))
debug_print("KoboTouch:_modify_epub:Processing {} - {}".format(metadata.author_sort, metadata.title))
# Currently only modifying CSS, so if no stylesheet, don't do anything
if not self.extra_sheet:
@ -2200,9 +2200,9 @@ class KOBOTOUCH(KOBO):
# future css mods may be epub/kepub specific, so pass file extension arg
fileext = os.path.splitext(book_file)[-1].lower()
debug_print("KoboTouch:_modify_epub: Modifying {0}".format(cssname))
debug_print("KoboTouch:_modify_epub: Modifying {}".format(cssname))
if self._modify_stylesheet(newsheet, fileext):
debug_print("KoboTouch:_modify_epub:CSS rules {0} -> {1} ({2})".format(oldrules, len(newsheet.cssRules), cssname))
debug_print("KoboTouch:_modify_epub:CSS rules {} -> {} ({})".format(oldrules, len(newsheet.cssRules), cssname))
container.dirty(cssname)
is_dirty = True
@ -2256,8 +2256,8 @@ class KOBOTOUCH(KOBO):
container = get_container(book_file)
container.css_preprocessor = DummyCSSPreProcessor()
except Exception as e:
debug_print("KoboTouch:create_container: exception from get_container {0} - {1}".format(metadata.author_sort, metadata.title))
debug_print("KoboTouch:create_container: exception is: {0}".format(e))
debug_print("KoboTouch:create_container: exception from get_container {} - {}".format(metadata.author_sort, metadata.title))
debug_print("KoboTouch:create_container: exception is: {}".format(e))
else:
commit_container = False
debug_print("KoboTouch:create_container: received container")
@ -2277,7 +2277,7 @@ class KOBOTOUCH(KOBO):
pass
def delete_via_sql(self, ContentID, ContentType):
imageId = super(KOBOTOUCH, self).delete_via_sql(ContentID, ContentType)
imageId = super().delete_via_sql(ContentID, ContentType)
if self.dbversion >= 53:
debug_print('KoboTouch:delete_via_sql: ContentID="%s"'%ContentID, 'ContentType="%s"'%ContentType)
@ -2383,7 +2383,7 @@ class KOBOTOUCH(KOBO):
def get_content_type_from_path(self, path):
ContentType = 6
if self.fwversion < (1, 9, 17):
ContentType = super(KOBOTOUCH, self).get_content_type_from_path(path)
ContentType = super().get_content_type_from_path(path)
return ContentType
def get_content_type_from_extension(self, extension):
@ -2391,7 +2391,7 @@ class KOBOTOUCH(KOBO):
# With new firmware, ContentType appears to be 6 for all types of sideloaded books.
ContentType = 6
if self.fwversion < (1,9,17):
ContentType = super(KOBOTOUCH, self).get_content_type_from_extension(extension)
ContentType = super().get_content_type_from_extension(extension)
return ContentType
def set_plugboards(self, plugboards, pb_func):
@ -3329,7 +3329,7 @@ class KOBOTOUCH(KOBO):
@classmethod
def _config(cls):
c = super(KOBOTOUCH, cls)._config()
c = super()._config()
c.add_opt('manage_collections', default=True)
c.add_opt('collections_columns', default='')
@ -3819,7 +3819,7 @@ class KOBOTOUCH(KOBO):
try:
is_debugging = len(self.debugging_title) > 0 and title.lower().find(self.debugging_title.lower()) >= 0 or len(title) == 0
except:
debug_print(("KoboTouch::is_debugging_title - Exception checking debugging title for title '{0}'.").format(title))
debug_print(("KoboTouch::is_debugging_title - Exception checking debugging title for title '{}'.").format(title))
is_debugging = False
return is_debugging
@ -3864,7 +3864,7 @@ class KOBOTOUCH(KOBO):
def __str__(self, *args, **kwargs):
options = ', '.join(['%s: %s' % (x.name, self.get_pref(x.name)) for x in self._config().preferences])
return u"Driver:%s, Options - %s" % (self.name, options)
return "Driver:%s, Options - %s" % (self.name, options)
if __name__ == '__main__':

View File

@ -39,7 +39,7 @@ class KOBOTOUCHConfig(TabbedDeviceConfig):
must_read_metadata, supports_use_author_sort,
extra_customization_message, device, extra_customization_choices=None, parent=None):
super(KOBOTOUCHConfig, self).__init__(device_settings, all_formats, supports_subdirs,
super().__init__(device_settings, all_formats, supports_subdirs,
must_read_metadata, supports_use_author_sort,
extra_customization_message, device, extra_customization_choices, parent)
@ -65,7 +65,7 @@ class KOBOTOUCHConfig(TabbedDeviceConfig):
return self._device()
def validate(self):
validated = super(KOBOTOUCHConfig, self).validate()
validated = super().validate()
validated &= self.tab2.validate()
return validated
@ -95,7 +95,7 @@ class KOBOTOUCHConfig(TabbedDeviceConfig):
def commit(self):
debug_print("KOBOTOUCHConfig::commit: start")
p = super(KOBOTOUCHConfig, self).commit()
p = super().commit()
p['manage_collections'] = self.manage_collections
p['create_collections'] = self.create_collections
@ -135,7 +135,7 @@ class KOBOTOUCHConfig(TabbedDeviceConfig):
class Tab1Config(DeviceConfigTab): # {{{
def __init__(self, parent, device):
super(Tab1Config, self).__init__(parent)
super().__init__(parent)
self.l = QVBoxLayout(self)
self.setLayout(self.l)
@ -159,7 +159,7 @@ class Tab1Config(DeviceConfigTab): # {{{
class Tab2Config(DeviceConfigTab): # {{{
def __init__(self, parent, device):
super(Tab2Config, self).__init__(parent)
super().__init__(parent)
self.l = QVBoxLayout(self)
self.setLayout(self.l)
@ -187,7 +187,7 @@ class Tab2Config(DeviceConfigTab): # {{{
class BookUploadsGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(BookUploadsGroupBox, self).__init__(parent, device)
super().__init__(parent, device)
self.setTitle(_("Uploading of books"))
self.options_layout = QGridLayout()
@ -229,7 +229,7 @@ class BookUploadsGroupBox(DeviceOptionsGroupBox):
class CollectionsGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(CollectionsGroupBox, self).__init__(parent, device)
super().__init__(parent, device)
self.setTitle(_("Collections"))
self.options_layout = QGridLayout()
@ -296,7 +296,7 @@ class CollectionsGroupBox(DeviceOptionsGroupBox):
class CoversGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(CoversGroupBox, self).__init__(parent, device)
super().__init__(parent, device)
self.setTitle(_("Upload covers"))
self.options_layout = QGridLayout()
@ -415,7 +415,7 @@ class CoversGroupBox(DeviceOptionsGroupBox):
class DeviceListGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(DeviceListGroupBox, self).__init__(parent, device)
super().__init__(parent, device)
self.setTitle(_("Show as on device"))
self.options_layout = QGridLayout()
@ -465,7 +465,7 @@ class DeviceListGroupBox(DeviceOptionsGroupBox):
class AdvancedGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(AdvancedGroupBox, self).__init__(parent, device, _("Advanced options"))
super().__init__(parent, device, _("Advanced options"))
# self.setTitle(_("Advanced Options"))
self.options_layout = QGridLayout()
@ -514,7 +514,7 @@ class AdvancedGroupBox(DeviceOptionsGroupBox):
class MetadataGroupBox(DeviceOptionsGroupBox):
def __init__(self, parent, device):
super(MetadataGroupBox, self).__init__(parent, device)
super().__init__(parent, device)
self.setTitle(_("Update metadata on the device"))
self.options_layout = QGridLayout()

View File

@ -1,4 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -46,7 +46,7 @@ class Book(Metadata):
Metadata.__init__(self, _('Unknown'), other=other)
self.storage_id, self.lpath = storage_id, lpath
self.lpath = self.path = self.lpath.replace(os.sep, '/')
self.mtp_relpath = tuple([icu_lower(x) for x in self.lpath.split('/')])
self.mtp_relpath = tuple(icu_lower(x) for x in self.lpath.split('/'))
self.datetime = utcnow().timetuple()
self.thumbail = None

View File

@ -108,10 +108,8 @@ class FileOrFolder:
return tuple(parts)
def __iter__(self):
for e in self.folders:
yield e
for e in self.files:
yield e
yield from self.folders
yield from self.files
def add_child(self, entry):
ans = FileOrFolder(entry, self.fs_cache())

View File

@ -75,7 +75,7 @@ class MTP_DEVICE(MTPDeviceBase):
traceback.print_stack()
return False
if debug is not None and ans:
debug('Device {0} claims to be an MTP device in the IOKit registry'.format(d))
debug('Device {} claims to be an MTP device in the IOKit registry'.format(d))
return bool(ans)
def set_debug_level(self, lvl):

View File

@ -31,7 +31,7 @@ class MTPDetect:
try:
with lopen(x, 'rb') as f:
return f.read()
except EnvironmentError:
except OSError:
pass
ipath = os.path.join(self.base, '{0}-*/{0}-*/interface'.format(dev.busnum))
@ -44,7 +44,7 @@ class MTPDetect:
try:
if raw and int(raw) == dev.devnum:
if debug is not None:
debug('Unknown device {0} claims to be an MTP device'
debug('Unknown device {} claims to be an MTP device'
.format(dev))
return True
except (ValueError, TypeError):

View File

@ -111,7 +111,7 @@ class NOOK_COLOR(NOOK):
self.EBOOK_DIR_MAIN = 'NOOK/My Files'
try:
os.makedirs(os.path.join(self._main_prefix, *self.EBOOK_DIR_MAIN.split('/')))
except EnvironmentError as err:
except OSError as err:
if err.errno != errno.EEXIST:
self.EBOOK_DIR_MAIN = 'NOOK'

View File

@ -1 +0,0 @@

View File

@ -110,7 +110,7 @@ class PALADIN(USBMS):
for i, row in enumerate(cursor):
try:
comp_date = int(os.path.getmtime(self.normalize_path(prefix + row[0])) * 1000)
except (OSError, IOError, TypeError):
except (OSError, TypeError):
# In case the db has incorrect path info
continue
device_date = int(row[1])

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -758,7 +758,7 @@ class XMLCache:
return m
def book_by_lpath(self, lpath, root):
matches = root.xpath(u'//*[local-name()="text" and @path="%s"]'%lpath)
matches = root.xpath('//*[local-name()="text" and @path="%s"]'%lpath)
if matches:
return matches[0]

View File

@ -199,7 +199,7 @@ class PRST1(USBMS):
for i, row in enumerate(cursor):
try:
comp_date = int(os.path.getmtime(self.normalize_path(prefix + row[0])) * 1000)
except (OSError, IOError, TypeError):
except (OSError, TypeError):
# In case the db has incorrect path info
continue
device_date = int(row[1])

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''
@ -40,12 +39,12 @@ _USBDevice = namedtuple('USBDevice',
class USBDevice(_USBDevice):
def __new__(cls, *args, **kwargs):
self = super(USBDevice, cls).__new__(cls, *args)
self = super().__new__(cls, *args)
self.busnum = self.devnum = -1
return self
def __repr__(self):
return (u'USBDevice(busnum=%s, devnum=%s, '
return ('USBDevice(busnum=%s, devnum=%s, '
'vendor_id=0x%04x, product_id=0x%04x, bcd=0x%04x, '
'manufacturer=%s, product=%s, serial=%s)')%(
self.busnum, self.devnum, self.vendor_id, self.product_id,
@ -141,15 +140,15 @@ class LinuxScanner:
try:
dev.append(read(man).decode('utf-8'))
except Exception:
dev.append(u'')
dev.append('')
try:
dev.append(read(prod_string).decode('utf-8'))
except Exception:
dev.append(u'')
dev.append('')
try:
dev.append(read(serial).decode('utf-8'))
except Exception:
dev.append(u'')
dev.append('')
dev = USBDevice(*dev)
try:

View File

@ -164,7 +164,7 @@ class ConnectionListener(Thread):
except socket.timeout:
pass
except socket.error:
except OSError:
x = sys.exc_info()[1]
self.driver._debug('unexpected socket exception', x.args[0])
self._close_socket(device_socket)
@ -623,9 +623,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
amt_sent = sock.send(s[sent_len:])
sock.settimeout(None)
if amt_sent <= 0:
raise IOError('Bad write on socket')
raise OSError('Bad write on socket')
sent_len += amt_sent
except socket.error as e:
except OSError as e:
self._debug('socket error', e, e.errno)
if e.args[0] != EAGAIN and e.args[0] != EINTR:
self._close_device_socket()
@ -661,7 +661,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self._debug('timeout communicating with device')
self._close_device_socket()
raise TimeoutError('Device did not respond in reasonable time')
except socket.error:
except OSError:
self._debug('device went away')
self._close_device_socket()
raise ControlError(desc='Device closed the network connection')
@ -689,7 +689,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self._debug('timeout communicating with device')
self._close_device_socket()
raise TimeoutError('Device did not respond in reasonable time')
except socket.error:
except OSError:
self._debug('device went away')
self._close_device_socket()
raise ControlError(desc='Device closed the network connection')
@ -936,7 +936,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
sock.bind((ip_addr, port))
else:
sock.bind(('', port))
except socket.error:
except OSError:
self._debug('socket error on port', port)
port = 0
except:
@ -1213,7 +1213,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
return True
except socket.timeout:
self._close_device_socket()
except socket.error:
except OSError:
x = sys.exc_info()[1]
self._debug('unexpected socket exception', x.args[0])
self._close_device_socket()

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -54,7 +54,7 @@ class CLI:
with dest:
try:
shutil.copyfileobj(infile, dest)
except IOError:
except OSError:
print('WARNING: First attempt to send file to device failed')
time.sleep(0.2)
infile.seek(0)

View File

@ -296,7 +296,7 @@ class Device(DeviceConfig, DevicePlugin):
try:
return subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
except IOError: # Probably an interrupted system call
except OSError: # Probably an interrupted system call
if i == 2:
raise
time.sleep(2)
@ -310,7 +310,7 @@ class Device(DeviceConfig, DevicePlugin):
try:
return subprocess.Popen('mount',
stdout=subprocess.PIPE).communicate()[0]
except IOError: # Probably an interrupted system call
except OSError: # Probably an interrupted system call
if i == 2:
raise
time.sleep(2)
@ -440,8 +440,7 @@ class Device(DeviceConfig, DevicePlugin):
isfile = os.path.isfile(p)
yield p, isfile
if not isfile:
for y, q in walk(p):
yield y, q
yield from walk(p)
def raw2num(raw):
raw = raw.lower()

View File

@ -68,8 +68,7 @@ def safe_walk(top, topdown=True, onerror=None, followlinks=False, maxdepth=128):
for name in dirs:
new_path = join(top, name)
if followlinks or not islink(new_path):
for x in safe_walk(new_path, topdown, onerror, followlinks, maxdepth-1):
yield x
yield from safe_walk(new_path, topdown, onerror, followlinks, maxdepth-1)
if not topdown:
yield top, dirs, nondirs
@ -151,8 +150,8 @@ class USBMS(CLI, Device):
if self._main_prefix is not None:
try:
self.driveinfo['main'] = self._update_driveinfo_file(self._main_prefix, 'main')
except (IOError, OSError) as e:
raise IOError(_('Failed to access files in the main memory of'
except OSError as e:
raise OSError(_('Failed to access files in the main memory of'
' your device. You should contact the device'
' manufacturer for support. Common fixes are:'
' try a different USB cable/USB port on your computer.'
@ -164,8 +163,8 @@ class USBMS(CLI, Device):
self.driveinfo['A'] = self._update_driveinfo_file(self._card_a_prefix, 'A')
if self._card_b_prefix is not None:
self.driveinfo['B'] = self._update_driveinfo_file(self._card_b_prefix, 'B')
except (IOError, OSError) as e:
raise IOError(_('Failed to access files on the SD card in your'
except OSError as e:
raise OSError(_('Failed to access files on the SD card in your'
' device. This can happen for many reasons. The SD card may be'
' corrupted, it may be too large for your device, it may be'
' write-protected, etc. Try a different SD card, or reformat'

View File

@ -59,8 +59,8 @@ def build_template_regexp(template):
template = template.rpartition('/')[2]
return re.compile(re.sub('{([^}]*)}', f, template) + r'([_\d]*$)')
except:
prints(u'Failed to parse template: %r'%template)
template = u'{title} - {authors}'
prints('Failed to parse template: %r'%template)
template = '{title} - {authors}'
return re.compile(re.sub('{([^}]*)}', f, template) + r'([_\d]*$)')
@ -90,7 +90,7 @@ def create_upload_path(mdata, fname, template, sanitize,
except:
today = time.localtime()
date = (today[0], today[1], today[2])
template = u"{title}_%d-%d-%d" % date
template = "{title}_%d-%d-%d" % date
fname = sanitize(fname)
ext = path_type.splitext(fname)[1]

View File

@ -507,8 +507,7 @@ def iterchildren(parent_devinst):
def iterdescendants(parent_devinst):
for child in iterchildren(parent_devinst):
yield child
for gc in iterdescendants(child):
yield gc
yield from iterdescendants(child)
def iterancestors(devinst):

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
@ -81,7 +79,7 @@ def extract_calibre_cover(raw, base, log):
if matches is None:
body = soup.find('body')
if body is not None:
text = u''.join(map(str, body.findAll(text=True)))
text = ''.join(map(str, body.findAll(text=True)))
if text.strip():
# Body has text, abort
return

View File

@ -33,8 +33,7 @@ class LazyEncodingPats:
if pats is None:
pats = tuple(compile_pats(binary))
setattr(self, attr, pats)
for pat in pats:
yield pat
yield from pats
lazy_encoding_pats = LazyEncodingPats()
@ -51,7 +50,7 @@ def strip_encoding_declarations(raw, limit=50*1024, preserve_newlines=False):
else:
sub = lambda m: '\n' * m.group().count('\n')
else:
sub = b'' if is_binary else u''
sub = b'' if is_binary else ''
for pat in lazy_encoding_pats(is_binary):
prefix = pat.sub(sub, prefix)
raw = prefix + suffix

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
@ -372,7 +370,7 @@ def main(args=sys.argv):
parser, plumber = create_option_parser(args, log)
opts, leftover_args = parser.parse_args(args)
if len(leftover_args) > 3:
log.error('Extra arguments not understood:', u', '.join(leftover_args[3:]))
log.error('Extra arguments not understood:', ', '.join(leftover_args[3:]))
return 1
for x in ('read_metadata_from_opf', 'cover'):
if getattr(opts, x, None) is not None:

View File

@ -70,7 +70,7 @@ class GuiRecommendations(dict):
def __new__(cls, *args):
dict.__new__(cls)
obj = super(GuiRecommendations, cls).__new__(cls, *args)
obj = super().__new__(cls, *args)
obj.disabled_options = set()
return obj

View File

@ -1,5 +1,3 @@
''' CHM File decoding support '''
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>,' \

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -291,7 +291,7 @@ class EPUBOutput(OutputFormatPlugin):
from calibre.ebooks.oeb.polish.cover import fix_conversion_titlepage_links_in_nav
try:
os.mkdir(os.path.join(tdir, 'META-INF'))
except EnvironmentError:
except OSError:
pass
with open(os.path.join(tdir, 'META-INF', 'container.xml'), 'wb') as f:
f.write(simple_container_xml(os.path.basename(opf)).encode('utf-8'))
@ -307,7 +307,7 @@ class EPUBOutput(OutputFormatPlugin):
os.remove(f.name)
try:
os.rmdir(os.path.join(tdir, 'META-INF'))
except EnvironmentError:
except OSError:
pass
def encrypt_fonts(self, uris, tdir, uuid): # {{{

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Anatoly Shipitsin <norguhtar at gmail.com>'
"""
@ -146,7 +144,7 @@ class FB2Input(InputFormatPlugin):
break
opf = OPFCreator(os.getcwd(), mi)
entries = [(f2, guess_type(f2)[0]) for f2 in os.listdir(u'.')]
entries = [(f2, guess_type(f2)[0]) for f2 in os.listdir('.')]
opf.create_manifest(entries)
opf.create_spine(['index.xhtml'])
if cpath:

View File

@ -284,7 +284,7 @@ class HTMLInput(InputFormatPlugin):
# Check for the common case, images
try:
img = what(link)
except EnvironmentError:
except OSError:
pass
else:
if img:

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
__docformat__ = 'restructuredtext en'

View File

@ -26,7 +26,7 @@ class HTMLZInput(InputFormatPlugin):
from calibre.utils.zipfile import ZipFile
self.log = log
html = u''
html = ''
top_levels = []
# Extract content from zip archive.
@ -35,21 +35,21 @@ class HTMLZInput(InputFormatPlugin):
# Find the HTML file in the archive. It needs to be
# top level.
index = u''
index = ''
multiple_html = False
# Get a list of all top level files in the archive.
for x in os.listdir(u'.'):
for x in os.listdir('.'):
if os.path.isfile(x):
top_levels.append(x)
# Try to find an index. file.
for x in top_levels:
if x.lower() in (u'index.html', u'index.xhtml', u'index.htm'):
if x.lower() in ('index.html', 'index.xhtml', 'index.htm'):
index = x
break
# Look for multiple HTML files in the archive. We look at the
# top level files only as only they matter in HTMLZ.
for x in top_levels:
if os.path.splitext(x)[1].lower() in (u'.html', u'.xhtml', u'.htm'):
if os.path.splitext(x)[1].lower() in ('.html', '.xhtml', '.htm'):
# Set index to the first HTML file found if it's not
# called index.
if not index:
@ -86,11 +86,11 @@ class HTMLZInput(InputFormatPlugin):
setattr(options, opt.option.name, opt.recommended_value)
options.input_encoding = 'utf-8'
base = os.getcwd()
htmlfile = os.path.join(base, u'index.html')
htmlfile = os.path.join(base, 'index.html')
c = 0
while os.path.exists(htmlfile):
c += 1
htmlfile = u'index%d.html'%c
htmlfile = 'index%d.html'%c
with open(htmlfile, 'wb') as f:
f.write(html.encode('utf-8'))
odi = options.debug_pipeline
@ -112,7 +112,7 @@ class HTMLZInput(InputFormatPlugin):
cover_path = None
opf = None
for x in top_levels:
if os.path.splitext(x)[1].lower() == u'.opf':
if os.path.splitext(x)[1].lower() == '.opf':
opf = x
break
if opf:

View File

@ -72,36 +72,36 @@ class HTMLZOutput(OutputFormatPlugin):
else:
from calibre.ebooks.htmlz.oeb2html import OEB2HTMLClassCSSizer as OEB2HTMLizer
with TemporaryDirectory(u'_htmlz_output') as tdir:
with TemporaryDirectory('_htmlz_output') as tdir:
htmlizer = OEB2HTMLizer(log)
html = htmlizer.oeb2html(oeb_book, opts)
fname = u'index'
fname = 'index'
if opts.htmlz_title_filename:
from calibre.utils.filenames import shorten_components_to
fname = shorten_components_to(100, (ascii_filename(str(oeb_book.metadata.title[0])),))[0]
with open(os.path.join(tdir, fname+u'.html'), 'wb') as tf:
with open(os.path.join(tdir, fname+'.html'), 'wb') as tf:
if isinstance(html, str):
html = html.encode('utf-8')
tf.write(html)
# CSS
if opts.htmlz_css_type == 'class' and opts.htmlz_class_style == 'external':
with open(os.path.join(tdir, u'style.css'), 'wb') as tf:
with open(os.path.join(tdir, 'style.css'), 'wb') as tf:
tf.write(htmlizer.get_css(oeb_book).encode('utf-8'))
# Images
images = htmlizer.images
if images:
if not os.path.exists(os.path.join(tdir, u'images')):
os.makedirs(os.path.join(tdir, u'images'))
if not os.path.exists(os.path.join(tdir, 'images')):
os.makedirs(os.path.join(tdir, 'images'))
for item in oeb_book.manifest:
if item.media_type in OEB_IMAGES and item.href in images:
if item.media_type == SVG_MIME:
data = etree.tostring(item.data, encoding='unicode').encode('utf-8')
else:
data = item.data
fname = os.path.join(tdir, u'images', images[item.href])
fname = os.path.join(tdir, 'images', images[item.href])
with open(fname, 'wb') as img:
img.write(data)
@ -114,7 +114,7 @@ class HTMLZOutput(OutputFormatPlugin):
cover_data = oeb_book.guide[term].item.data
if cover_data:
from calibre.utils.img import save_cover_data_to
cover_path = os.path.join(tdir, u'cover.jpg')
cover_path = os.path.join(tdir, 'cover.jpg')
with lopen(cover_path, 'w') as cf:
cf.write('')
save_cover_data_to(cover_data, cover_path)
@ -123,11 +123,11 @@ class HTMLZOutput(OutputFormatPlugin):
traceback.print_exc()
# Metadata
with open(os.path.join(tdir, u'metadata.opf'), 'wb') as mdataf:
with open(os.path.join(tdir, 'metadata.opf'), 'wb') as mdataf:
opf = OPF(io.BytesIO(etree.tostring(oeb_book.metadata.to_opf1(), encoding='UTF-8')))
mi = opf.to_book_metadata()
if cover_path:
mi.cover = u'cover.jpg'
mi.cover = 'cover.jpg'
mdataf.write(metadata_to_opf(mi))
htmlz = ZipFile(output_path, 'w')

View File

@ -31,7 +31,7 @@ class LRFInput(InputFormatPlugin):
d.parse()
xml = d.to_xml(write_files=True)
if options.verbose > 2:
open(u'lrs.xml', 'wb').write(xml.encode('utf-8'))
open('lrs.xml', 'wb').write(xml.encode('utf-8'))
doc = safe_xml_fromstring(xml)
char_button_map = {}

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -1,4 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
@ -54,12 +53,12 @@ class RTFInput(InputFormatPlugin):
def generate_xml(self, stream):
from calibre.ebooks.rtf2xml.ParseRtf import ParseRtf
ofile = u'dataxml.xml'
ofile = 'dataxml.xml'
run_lev, debug_dir, indent_out = 1, None, 0
if getattr(self.opts, 'debug_pipeline', None) is not None:
try:
os.mkdir(u'rtfdebug')
debug_dir = u'rtfdebug'
os.mkdir('rtfdebug')
debug_dir = 'rtfdebug'
run_lev = 4
indent_out = 1
self.log('Running RTFParser in debug mode')
@ -137,7 +136,7 @@ class RTFInput(InputFormatPlugin):
if fmt is None:
fmt = 'wmf'
count += 1
name = u'%04d.%s' % (count, fmt)
name = '%04d.%s' % (count, fmt)
with open(name, 'wb') as f:
f.write(data)
imap[count] = name
@ -215,7 +214,7 @@ class RTFInput(InputFormatPlugin):
for cls, val in iteritems(border_styles):
css += '\n\n.%s {\n%s\n}'%(cls, val)
with open(u'styles.css', 'ab') as f:
with open('styles.css', 'ab') as f:
f.write(css.encode('utf-8'))
def convert_borders(self, doc):
@ -286,7 +285,7 @@ class RTFInput(InputFormatPlugin):
extensions = {('calibre', 'inline-class') : inline_class}
transform = etree.XSLT(styledoc, extensions=extensions)
result = transform(doc)
html = u'index.xhtml'
html = 'index.xhtml'
with open(html, 'wb') as f:
res = as_bytes(transform.tostring(result))
# res = res[:100].replace('xmlns:html', 'xmlns') + res[100:]
@ -305,10 +304,10 @@ class RTFInput(InputFormatPlugin):
if not mi.authors:
mi.authors = [_('Unknown')]
opf = OPFCreator(os.getcwd(), mi)
opf.create_manifest([(u'index.xhtml', None)])
opf.create_spine([u'index.xhtml'])
opf.render(open(u'metadata.opf', 'wb'))
return os.path.abspath(u'metadata.opf')
opf.create_manifest([('index.xhtml', None)])
opf.create_spine(['index.xhtml'])
opf.render(open('metadata.opf', 'wb'))
return os.path.abspath('metadata.opf')
def postprocess_book(self, oeb, opts, log):
for item in oeb.spine:

View File

@ -561,10 +561,10 @@ class Blocks(Style):
def all_styles():
return set(
return {
x.NAME for x in itervalues(globals()) if
isinstance(x, type) and issubclass(x, Style) and x is not Style
)
}
def load_styles(prefs, respect_disabled=True):

View File

@ -259,7 +259,7 @@ class DOCX:
else:
try:
shutil.rmtree(self.tdir)
except EnvironmentError:
except OSError:
pass

View File

@ -54,7 +54,7 @@ null = object()
def parser(name, field_map, default_field_name=None):
field_map = dict((x.split(':') for x in field_map.split()))
field_map = dict(x.split(':') for x in field_map.split())
def parse(raw, log=None):
ans = {}

View File

@ -18,8 +18,7 @@ class Note:
self.namespace = namespace
def __iter__(self):
for p in self.namespace.descendants(self.parent, 'w:p', 'w:tbl'):
yield p
yield from self.namespace.descendants(self.parent, 'w:p', 'w:tbl')
class Footnotes:

View File

@ -359,8 +359,6 @@ class Images:
os.mkdir(dest)
self.dest_dir, self.docx = dest, docx
if elem.tag.endswith('}drawing'):
for tag in self.drawing_to_html(elem, page):
yield tag
yield from self.drawing_to_html(elem, page)
else:
for tag in self.pict_to_html(elem, page):
yield tag
yield from self.pict_to_html(elem, page)

View File

@ -124,8 +124,7 @@ class Styles:
self.default_paragraph_style = self.default_character_style = None
def __iter__(self):
for s in itervalues(self.id_map):
yield s
yield from itervalues(self.id_map)
def __getitem__(self, key):
return self.id_map[key]

View File

@ -615,11 +615,9 @@ class Table:
tc.getparent().remove(tc)
def __iter__(self):
for p in self.paragraphs:
yield p
yield from self.paragraphs
for t in itervalues(self.sub_tables):
for p in t:
yield p
yield from t
def apply_markup(self, rmap, page, parent=None):
table = TABLE('\n\t\t')

View File

@ -311,7 +311,7 @@ class Convert:
seraw = self.docx.read(sename)
except KeyError:
self.log.warn('Settings %s do not exist' % sename)
except EnvironmentError as e:
except OSError as e:
if e.errno != errno.ENOENT:
raise
self.log.warn('Settings %s file missing' % sename)

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
@ -37,7 +35,7 @@ def initialize_container(path_to_container, opf_name='metadata.opf',
'''
rootfiles = ''
for path, mimetype, _ in extra_entries:
rootfiles += '<rootfile full-path="{0}" media-type="{1}"/>'.format(
rootfiles += '<rootfile full-path="{}" media-type="{}"/>'.format(
path, mimetype)
CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8')
zf = ZipFile(path_to_container, 'w')

View File

@ -124,7 +124,7 @@ class FB2MLizer:
lc = self.oeb_book.metadata.language[0].value
metadata['lang'] = lc or 'en'
else:
metadata['lang'] = u'en'
metadata['lang'] = 'en'
metadata['id'] = None
metadata['cover'] = self.get_cover()
metadata['genre'] = self.opts.fb2_genre
@ -483,7 +483,7 @@ class FB2MLizer:
tags += p_tag
fb2_out.append('<image l:href="#%s"/>' % self.image_hrefs[ihref])
else:
self.log.warn(u'Ignoring image not in manifest: %s' % ihref)
self.log.warn('Ignoring image not in manifest: %s' % ihref)
if tag in ('br', 'hr') or ems >= 1:
if ems < 1:
multiplier = 1

View File

@ -121,10 +121,10 @@ class HTMLFile:
self.is_binary = not bool(pat.search(header))
if not self.is_binary:
src += f.read()
except IOError as err:
except OSError as err:
msg = 'Could not read from file: %s with error: %s'%(self.path, as_unicode(err))
if level == 0:
raise IOError(msg)
raise OSError(msg)
raise IgnoreFile(msg, err.errno)
if not src:

View File

@ -1,5 +1,3 @@
__license__ = 'GPL 3'
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
__docformat__ = 'restructuredtext en'

View File

@ -58,7 +58,7 @@ class OEB2HTML:
def mlize_spine(self, oeb_book):
output = [
u'<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>%s</title></head><body>' % (
'<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>%s</title></head><body>' % (
prepare_string_for_xml(self.book_title))
]
for item in oeb_book.spine:
@ -139,10 +139,10 @@ class OEB2HTML:
def prepare_string_for_html(self, raw):
raw = prepare_string_for_xml(raw)
raw = raw.replace(u'\u00ad', '&shy;')
raw = raw.replace(u'\u2014', '&mdash;')
raw = raw.replace(u'\u2013', '&ndash;')
raw = raw.replace(u'\u00a0', '&nbsp;')
raw = raw.replace('\u00ad', '&shy;')
raw = raw.replace('\u2014', '&mdash;')
raw = raw.replace('\u2013', '&ndash;')
raw = raw.replace('\u00a0', '&nbsp;')
return raw
@ -340,9 +340,9 @@ class OEB2HTMLClassCSSizer(OEB2HTML):
css = '<link href="style.css" rel="stylesheet" type="text/css" />'
else:
css = '<style type="text/css">' + self.get_css(oeb_book) + '</style>'
title = u'<title>%s</title>' % prepare_string_for_xml(self.book_title)
output = [u'<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'] + \
[css] + [title, u'</head><body>'] + output + [u'</body></html>']
title = '<title>%s</title>' % prepare_string_for_xml(self.book_title)
output = ['<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'] + \
[css] + [title, '</head><body>'] + output + ['</body></html>']
return ''.join(output)
def dump_text(self, elem, stylizer, page):

View File

@ -1,5 +1,3 @@
""" Hyphenation, using Frank Liang's algorithm.
This module provides a single function to hyphenate words. hyphenate_word takes

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,5 +1,3 @@
'''
LZX compression/decompression wrapper.
'''

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,5 +1,3 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

Some files were not shown because too many files have changed in this diff Show More