mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix pep8 compliance and add replace parameter to add_formats_with_hooks
This commit is contained in:
parent
a7274a2c80
commit
d9acb3091b
@ -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
|
||||||
|
@ -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
|
||||||
@ -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:
|
||||||
@ -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
|
||||||
@ -2007,7 +2008,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
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'] = []
|
||||||
@ -2036,7 +2037,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
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())
|
||||||
@ -2335,7 +2336,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
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 \
|
||||||
@ -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([])
|
||||||
@ -3363,7 +3362,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
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
|
||||||
|
|
||||||
@ -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):
|
||||||
'''
|
'''
|
||||||
@ -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)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user