Fix pep8 compliance and add replace parameter to add_formats_with_hooks

This commit is contained in:
Kovid Goyal 2013-04-09 13:01:21 +05:30
parent a7274a2c80
commit d9acb3091b
2 changed files with 45 additions and 47 deletions

View File

@ -1,4 +1,4 @@
[flake8] [flake8]
max-line-length = 160 max-line-length = 160
builtins = _,dynamic_property,__,P,I,lopen,icu_lower,icu_upper,icu_title,ngettext builtins = _,dynamic_property,__,P,I,lopen,icu_lower,icu_upper,icu_title,ngettext
ignore = E12,E221,E301,E302,E304,E401,W391 ignore = E12,E22,E231,E301,E302,E304,E401,W391

View File

@ -205,7 +205,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return row[loc] return row[loc]
def initialize_dynamic(self): def initialize_dynamic(self):
self.field_metadata = FieldMetadata() #Ensure we start with a clean copy self.field_metadata = FieldMetadata() # Ensure we start with a clean copy
self.prefs = DBPrefs(self) self.prefs = DBPrefs(self)
defs = self.prefs.defaults defs = self.prefs.defaults
defs['gui_restriction'] = defs['cs_restriction'] = '' defs['gui_restriction'] = defs['cs_restriction'] = ''
@ -352,7 +352,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
'''.format(_('News'))) '''.format(_('News')))
self.conn.commit() self.conn.commit()
CustomColumns.__init__(self) CustomColumns.__init__(self)
template = '''\ template = '''\
(SELECT {query} FROM books_{table}_link AS link INNER JOIN (SELECT {query} FROM books_{table}_link AS link INNER JOIN
@ -444,7 +443,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# Assumption is that someone else will fix them if they change. # Assumption is that someone else will fix them if they change.
self.field_metadata.remove_dynamic_categories() self.field_metadata.remove_dynamic_categories()
for user_cat in sorted(self.prefs.get('user_categories', {}).keys(), key=sort_key): for user_cat in sorted(self.prefs.get('user_categories', {}).keys(), key=sort_key):
cat_name = '@' + user_cat # add the '@' to avoid name collision cat_name = '@' + user_cat # add the '@' to avoid name collision
self.field_metadata.add_user_category(label=cat_name, name=user_cat) self.field_metadata.add_user_category(label=cat_name, name=user_cat)
# add grouped search term user categories # add grouped search term user categories
@ -596,7 +595,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
current title and author. If there was a previous directory, its contents current title and author. If there was a previous directory, its contents
are copied and it is deleted. are copied and it is deleted.
''' '''
id = index if index_is_id else self.id(index) id = index if index_is_id else self.id(index)
path = self.construct_path_name(id) path = self.construct_path_name(id)
current_path = self.path(id, index_is_id=True).replace(os.sep, '/') current_path = self.path(id, index_is_id=True).replace(os.sep, '/')
formats = self.formats(id, index_is_id=True) formats = self.formats(id, index_is_id=True)
@ -620,7 +619,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if not os.path.exists(tpath): if not os.path.exists(tpath):
os.makedirs(tpath) os.makedirs(tpath)
if source_ok: # Migrate existing files if source_ok: # Migrate existing files
self.copy_cover_to(id, os.path.join(tpath, 'cover.jpg'), self.copy_cover_to(id, os.path.join(tpath, 'cover.jpg'),
index_is_id=True, windows_atomic_move=wam, index_is_id=True, windows_atomic_move=wam,
use_hardlink=True) use_hardlink=True)
@ -668,7 +667,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
os.rename(os.path.join(curpath, oldseg), os.rename(os.path.join(curpath, oldseg),
os.path.join(curpath, newseg)) os.path.join(curpath, newseg))
except: except:
break # Fail silently since nothing catastrophic has happened break # Fail silently since nothing catastrophic has happened
curpath = os.path.join(curpath, newseg) curpath = os.path.join(curpath, newseg)
def add_listener(self, listener): def add_listener(self, listener):
@ -727,7 +726,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return ret return ret
def cover_last_modified(self, index, index_is_id=False): def cover_last_modified(self, index, index_is_id=False):
id = index if index_is_id else self.id(index) id = index if index_is_id else self.id(index)
path = os.path.join(self.library_path, self.path(id, index_is_id=True), 'cover.jpg') path = os.path.join(self.library_path, self.path(id, index_is_id=True), 'cover.jpg')
try: try:
return utcfromtimestamp(os.stat(path).st_mtime) return utcfromtimestamp(os.stat(path).st_mtime)
@ -1074,8 +1073,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
identical_book_ids = set([]) identical_book_ids = set([])
if mi.authors: if mi.authors:
try: try:
quathors = mi.authors[:10] # Too many authors causes parsing of quathors = mi.authors[:10] # Too many authors causes parsing of
# the search expression to fail # the search expression to fail
query = u' and '.join([u'author:"=%s"'%(a.replace('"', '')) for a in query = u' and '.join([u'author:"=%s"'%(a.replace('"', '')) for a in
quathors]) quathors])
qauthors = mi.authors[10:] qauthors = mi.authors[10:]
@ -1307,7 +1306,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return fmt_path return fmt_path
try: try:
candidates = glob.glob(os.path.join(path, '*'+format)) candidates = glob.glob(os.path.join(path, '*'+format))
except: # If path contains strange characters this throws an exc except: # If path contains strange characters this throws an exc
candidates = [] candidates = []
if format and candidates and os.path.exists(candidates[0]): if format and candidates and os.path.exists(candidates[0]):
try: try:
@ -1350,7 +1349,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if path != dest: if path != dest:
os.rename(path, dest) os.rename(path, dest)
except: except:
pass # Nothing too catastrophic happened, the cases mismatch, that's all pass # Nothing too catastrophic happened, the cases mismatch, that's all
else: else:
windows_atomic_move.copy_path_to(path, dest) windows_atomic_move.copy_path_to(path, dest)
else: else:
@ -1366,7 +1365,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
try: try:
os.rename(path, dest) os.rename(path, dest)
except: except:
pass # Nothing too catastrophic happened, the cases mismatch, that's all pass # Nothing too catastrophic happened, the cases mismatch, that's all
else: else:
if use_hardlink: if use_hardlink:
try: try:
@ -1476,12 +1475,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return ret return ret
def add_format_with_hooks(self, index, format, fpath, index_is_id=False, def add_format_with_hooks(self, index, format, fpath, index_is_id=False,
path=None, notify=True): path=None, notify=True, replace=True):
npath = self.run_import_plugins(fpath, format) npath = self.run_import_plugins(fpath, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() format = os.path.splitext(npath)[-1].lower().replace('.', '').upper()
stream = lopen(npath, 'rb') stream = lopen(npath, 'rb')
format = check_ebook_format(stream, format) format = check_ebook_format(stream, format)
retval = self.add_format(index, format, stream, retval = self.add_format(index, format, stream, replace=replace,
index_is_id=index_is_id, path=path, notify=notify) index_is_id=index_is_id, path=path, notify=notify)
run_plugins_on_postimport(self, id, format) run_plugins_on_postimport(self, id, format)
return retval return retval
@ -1489,7 +1488,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
def add_format(self, index, format, stream, index_is_id=False, path=None, def add_format(self, index, format, stream, index_is_id=False, path=None,
notify=True, replace=True, copy_function=None): notify=True, replace=True, copy_function=None):
id = index if index_is_id else self.id(index) id = index if index_is_id else self.id(index)
if not format: format = '' if not format:
format = ''
self.format_metadata_cache[id].pop(format.upper(), None) self.format_metadata_cache[id].pop(format.upper(), None)
name = self.format_filename_cache[id].get(format.upper(), None) name = self.format_filename_cache[id].get(format.upper(), None)
if path is None: if path is None:
@ -1576,7 +1576,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
def remove_format(self, index, format, index_is_id=False, notify=True, def remove_format(self, index, format, index_is_id=False, notify=True,
commit=True, db_only=False): commit=True, db_only=False):
id = index if index_is_id else self.id(index) id = index if index_is_id else self.id(index)
if not format: format = '' if not format:
format = ''
self.format_metadata_cache[id].pop(format.upper(), None) self.format_metadata_cache[id].pop(format.upper(), None)
name = self.format_filename_cache[id].get(format.upper(), None) name = self.format_filename_cache[id].get(format.upper(), None)
if name: if name:
@ -1745,12 +1746,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# Get the ids for the item values # Get the ids for the item values
if not cat['is_custom']: if not cat['is_custom']:
funcs = { funcs = {
'authors' : self.get_authors_with_ids, 'authors': self.get_authors_with_ids,
'series' : self.get_series_with_ids, 'series': self.get_series_with_ids,
'publisher': self.get_publishers_with_ids, 'publisher': self.get_publishers_with_ids,
'tags' : self.get_tags_with_ids, 'tags': self.get_tags_with_ids,
'languages': self.get_languages_with_ids, 'languages': self.get_languages_with_ids,
'rating' : self.get_ratings_with_ids, 'rating': self.get_ratings_with_ids,
} }
func = funcs.get(category, None) func = funcs.get(category, None)
if func: if func:
@ -1833,7 +1834,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
item.rc += 1 item.rc += 1
continue continue
try: try:
(item_id, sort_val) = tid_cat[val] # let exceptions fly (item_id, sort_val) = tid_cat[val] # let exceptions fly
item = tcats_cat.get(val, None) item = tcats_cat.get(val, None)
if not item: if not item:
item = tag_class(val, sort_val) item = tag_class(val, sort_val)
@ -1855,7 +1856,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
tid_cat[val] = (val, val) tid_cat[val] = (val, val)
for val in vals: for val in vals:
try: try:
(item_id, sort_val) = tid_cat[val] # let exceptions fly (item_id, sort_val) = tid_cat[val] # let exceptions fly
item = tcats_cat.get(val, None) item = tcats_cat.get(val, None)
if not item: if not item:
item = tag_class(val, sort_val) item = tag_class(val, sort_val)
@ -1923,7 +1924,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# in the main Tag loop. Saves a few % # in the main Tag loop. Saves a few %
if datatype == 'rating': if datatype == 'rating':
formatter = (lambda x:u'\u2605'*int(x/2)) formatter = (lambda x:u'\u2605'*int(x/2))
avgr = lambda x : x.n avgr = lambda x: x.n
# eliminate the zero ratings line as well as count == 0 # eliminate the zero ratings line as well as count == 0
items = [v for v in tcategories[category].values() if v.c > 0 and v.n != 0] items = [v for v in tcategories[category].values() if v.c > 0 and v.n != 0]
elif category == 'authors': elif category == 'authors':
@ -1940,7 +1941,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# sort the list # sort the list
if sort == 'name': if sort == 'name':
kf = lambda x :sort_key(x.s) kf = lambda x:sort_key(x.s)
reverse=False reverse=False
elif sort == 'popularity': elif sort == 'popularity':
kf = lambda x: x.c kf = lambda x: x.c
@ -2005,9 +2006,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if sort == 'popularity': if sort == 'popularity':
categories['formats'].sort(key=lambda x: x.count, reverse=True) categories['formats'].sort(key=lambda x: x.count, reverse=True)
else: # no ratings exist to sort on else: # no ratings exist to sort on
# No need for ICU here. # No need for ICU here.
categories['formats'].sort(key = lambda x:x.name) categories['formats'].sort(key=lambda x:x.name)
# Now do identifiers. This works like formats # Now do identifiers. This works like formats
categories['identifiers'] = [] categories['identifiers'] = []
@ -2034,9 +2035,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if sort == 'popularity': if sort == 'popularity':
categories['identifiers'].sort(key=lambda x: x.count, reverse=True) categories['identifiers'].sort(key=lambda x: x.count, reverse=True)
else: # no ratings exist to sort on else: # no ratings exist to sort on
# No need for ICU here. # No need for ICU here.
categories['identifiers'].sort(key = lambda x:x.name) categories['identifiers'].sort(key=lambda x:x.name)
#### Now do the user-defined categories. #### #### Now do the user-defined categories. ####
user_categories = dict.copy(self.clean_user_categories()) user_categories = dict.copy(self.clean_user_categories())
@ -2083,7 +2084,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
else: else:
items.append(taglist[label][n]) items.append(taglist[label][n])
# else: do nothing, to not include nodes w zero counts # else: do nothing, to not include nodes w zero counts
cat_name = '@' + user_cat # add the '@' to avoid name collision cat_name = '@' + user_cat # add the '@' to avoid name collision
# Not a problem if we accumulate entries in the icon map # Not a problem if we accumulate entries in the icon map
if icon_map is not None: if icon_map is not None:
icon_map[cat_name] = icon_map['user:'] icon_map[cat_name] = icon_map['user:']
@ -2331,11 +2332,10 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
elif mi_idents: elif mi_idents:
identifiers = self.get_identifiers(id, index_is_id=True) identifiers = self.get_identifiers(id, index_is_id=True)
for key, val in mi_idents.iteritems(): for key, val in mi_idents.iteritems():
if val and val.strip(): # Don't delete an existing identifier if val and val.strip(): # Don't delete an existing identifier
identifiers[icu_lower(key)] = val identifiers[icu_lower(key)] = val
self.set_identifiers(id, identifiers, notify=False, commit=False) self.set_identifiers(id, identifiers, notify=False, commit=False)
user_mi = mi.get_all_user_metadata(make_copy=False) user_mi = mi.get_all_user_metadata(make_copy=False)
for key in user_mi.iterkeys(): for key in user_mi.iterkeys():
if key in self.field_metadata and \ if key in self.field_metadata and \
@ -2455,7 +2455,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
try: try:
self.conn.execute('''INSERT INTO books_authors_link(book, author) self.conn.execute('''INSERT INTO books_authors_link(book, author)
VALUES (?,?)''', (id, aid)) VALUES (?,?)''', (id, aid))
except IntegrityError: # Sometimes books specify the same author twice in their metadata except IntegrityError: # Sometimes books specify the same author twice in their metadata
pass pass
if case_change: if case_change:
bks = self.conn.get('''SELECT book FROM books_authors_link bks = self.conn.get('''SELECT book FROM books_authors_link
@ -2614,7 +2614,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if notify: if notify:
self.notify('metadata', [id]) self.notify('metadata', [id])
def set_publisher(self, id, publisher, notify=True, commit=True, def set_publisher(self, id, publisher, notify=True, commit=True,
allow_case_change=False): allow_case_change=False):
self.conn.execute('DELETE FROM books_publishers_link WHERE book=?',(id,)) self.conn.execute('DELETE FROM books_publishers_link WHERE book=?',(id,))
@ -2820,7 +2819,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if new_id is None or old_id == new_id: if new_id is None or old_id == new_id:
new_id = old_id new_id = old_id
# New name doesn't exist. Simply change the old name # New name doesn't exist. Simply change the old name
self.conn.execute('UPDATE publishers SET name=? WHERE id=?', \ self.conn.execute('UPDATE publishers SET name=? WHERE id=?',
(new_name, old_id)) (new_name, old_id))
else: else:
# Change the link table to point at the new one # Change the link table to point at the new one
@ -2860,7 +2859,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
self.conn.commit() self.conn.commit()
def set_sort_field_for_author(self, old_id, new_sort, commit=True, notify=False): def set_sort_field_for_author(self, old_id, new_sort, commit=True, notify=False):
self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \ self.conn.execute('UPDATE authors SET sort=? WHERE id=?',
(new_sort.strip(), old_id)) (new_sort.strip(), old_id))
if commit: if commit:
self.conn.commit() self.conn.commit()
@ -2959,7 +2958,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
@classmethod @classmethod
def cleanup_tags(cls, tags): def cleanup_tags(cls, tags):
tags = [x.strip().replace(',', ';') for x in tags if x.strip()] tags = [x.strip().replace(',', ';') for x in tags if x.strip()]
tags = [x.decode(preferred_encoding, 'replace') \ tags = [x.decode(preferred_encoding, 'replace')
if isbytestring(x) else x for x in tags] if isbytestring(x) else x for x in tags]
tags = [u' '.join(x.split()) for x in tags] tags = [u' '.join(x.split()) for x in tags]
ans, seen = [], set([]) ans, seen = [], set([])
@ -3360,10 +3359,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
self.add_format(db_id, format, stream, index_is_id=True) self.add_format(db_id, format, stream, index_is_id=True)
self.conn.commit() self.conn.commit()
self.data.refresh_ids(self, [db_id]) # Needed to update format list and size self.data.refresh_ids(self, [db_id]) # Needed to update format list and size
return db_id return db_id
def add_news(self, path, arg): def add_news(self, path, arg):
from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.meta import get_metadata
@ -3399,7 +3397,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if not hasattr(path, 'read'): if not hasattr(path, 'read'):
stream.close() stream.close()
self.conn.commit() self.conn.commit()
self.data.refresh_ids(self, [id]) # Needed to update format list and size self.data.refresh_ids(self, [id]) # Needed to update format list and size
return id return id
def run_import_plugins(self, path_or_stream, format): def run_import_plugins(self, path_or_stream, format):
@ -3463,7 +3461,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
traceback.print_exc() traceback.print_exc()
return id return id
def add_books(self, paths, formats, metadata, add_duplicates=True, def add_books(self, paths, formats, metadata, add_duplicates=True,
return_ids=False): return_ids=False):
''' '''
@ -3507,7 +3504,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
stream.close() stream.close()
postimport.append((id, format)) postimport.append((id, format))
self.conn.commit() self.conn.commit()
self.data.refresh_ids(self, ids) # Needed to update format list and size self.data.refresh_ids(self, ids) # Needed to update format list and size
for book_id, fmt in postimport: for book_id, fmt in postimport:
run_plugins_on_postimport(self, book_id, fmt) run_plugins_on_postimport(self, book_id, fmt)
if duplicates: if duplicates:
@ -3557,7 +3554,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# set_metadata, but probably isn't good enough # set_metadata, but probably isn't good enough
self.dirtied([id], commit=False) self.dirtied([id], commit=False)
self.conn.commit() self.conn.commit()
self.data.refresh_ids(self, [id]) # Needed to update format list and size self.data.refresh_ids(self, [id]) # Needed to update format list and size
if notify: if notify:
self.notify('add', [id]) self.notify('add', [id])
return id return id
@ -3651,7 +3648,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
FIELDS.add('%d_index'%x) FIELDS.add('%d_index'%x)
data = [] data = []
for record in self.data: for record in self.data:
if record is None: continue if record is None:
continue
db_id = record[self.FIELD_MAP['id']] db_id = record[self.FIELD_MAP['id']]
if ids is not None and db_id not in ids: if ids is not None and db_id not in ids:
continue continue
@ -3694,8 +3692,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
progress.setValue(0) progress.setValue(0)
progress.setLabelText(header) progress.setLabelText(header)
QCoreApplication.processEvents() QCoreApplication.processEvents()
db.conn.row_factory = lambda cursor, row : tuple(row) db.conn.row_factory = lambda cursor, row: tuple(row)
db.conn.text_factory = lambda x : unicode(x, 'utf-8', 'replace') db.conn.text_factory = lambda x: unicode(x, 'utf-8', 'replace')
books = db.conn.get('SELECT id, title, sort, timestamp, series_index, author_sort, isbn FROM books ORDER BY id ASC') books = db.conn.get('SELECT id, title, sort, timestamp, series_index, author_sort, isbn FROM books ORDER BY id ASC')
progress.setAutoReset(False) progress.setAutoReset(False)
progress.setRange(0, len(books)) progress.setRange(0, len(books))
@ -3771,7 +3769,7 @@ books_series_link feeds
continue continue
key = os.path.splitext(path)[0] key = os.path.splitext(path)[0]
if not books.has_key(key): if key not in books:
books[key] = [] books[key] = []
books[key].append(path) books[key].append(path)