pep8 compliance

This commit is contained in:
Kovid Goyal 2013-04-09 14:03:02 +05:30
parent 6bfcd2536a
commit adb0859108
10 changed files with 98 additions and 94 deletions

View File

@ -41,7 +41,6 @@ Differences in semantics from pysqlite:
''' '''
class DynamicFilter(object): # {{{ class DynamicFilter(object): # {{{
'No longer used, present for legacy compatibility' 'No longer used, present for legacy compatibility'
@ -114,9 +113,10 @@ class DBPrefs(dict): # {{{
return default return default
def set_namespaced(self, namespace, key, val): def set_namespaced(self, namespace, key, val):
if u':' in key: raise KeyError('Colons are not allowed in keys') if u':' in key:
if u':' in namespace: raise KeyError('Colons are not allowed in' raise KeyError('Colons are not allowed in keys')
' the namespace') if u':' in namespace:
raise KeyError('Colons are not allowed in the namespace')
key = u'namespaced:%s:%s'%(namespace, key) key = u'namespaced:%s:%s'%(namespace, key)
self[key] = val self[key] = val
@ -170,7 +170,8 @@ def pynocase(one, two, encoding='utf-8'):
return cmp(one.lower(), two.lower()) return cmp(one.lower(), two.lower())
def _author_to_author_sort(x): def _author_to_author_sort(x):
if not x: return '' if not x:
return ''
return author_to_author_sort(x.replace('|', ',')) return author_to_author_sort(x.replace('|', ','))
def icu_collator(s1, s2): def icu_collator(s1, s2):
@ -257,7 +258,7 @@ class Connection(apsw.Connection): # {{{
self.createscalarfunction('title_sort', title_sort, 1) self.createscalarfunction('title_sort', title_sort, 1)
self.createscalarfunction('author_to_author_sort', self.createscalarfunction('author_to_author_sort',
_author_to_author_sort, 1) _author_to_author_sort, 1)
self.createscalarfunction('uuid4', lambda : str(uuid.uuid4()), self.createscalarfunction('uuid4', lambda: str(uuid.uuid4()),
0) 0)
# Dummy functions for dynamically created filters # Dummy functions for dynamically created filters
@ -635,10 +636,10 @@ class DB(object):
self.custom_data_adapters = { self.custom_data_adapters = {
'float': adapt_number, 'float': adapt_number,
'int': adapt_number, 'int': adapt_number,
'rating':lambda x,d : x if x is None else min(10., max(0., float(x))), 'rating':lambda x,d: x if x is None else min(10., max(0., float(x))),
'bool': adapt_bool, 'bool': adapt_bool,
'comments': lambda x,d: adapt_text(x, {'is_multiple':False}), 'comments': lambda x,d: adapt_text(x, {'is_multiple':False}),
'datetime' : adapt_datetime, 'datetime': adapt_datetime,
'text':adapt_text, 'text':adapt_text,
'series':adapt_text, 'series':adapt_text,
'enumeration': adapt_enum 'enumeration': adapt_enum

View File

@ -403,16 +403,19 @@ class Cache(object):
''' '''
if as_file: if as_file:
ret = SpooledTemporaryFile(SPOOL_SIZE) ret = SpooledTemporaryFile(SPOOL_SIZE)
if not self.copy_cover_to(book_id, ret): return if not self.copy_cover_to(book_id, ret):
return
ret.seek(0) ret.seek(0)
elif as_path: elif as_path:
pt = PersistentTemporaryFile('_dbcover.jpg') pt = PersistentTemporaryFile('_dbcover.jpg')
with pt: with pt:
if not self.copy_cover_to(book_id, pt): return if not self.copy_cover_to(book_id, pt):
return
ret = pt.name ret = pt.name
else: else:
buf = BytesIO() buf = BytesIO()
if not self.copy_cover_to(book_id, buf): return if not self.copy_cover_to(book_id, buf):
return
ret = buf.getvalue() ret = buf.getvalue()
if as_image: if as_image:
from PyQt4.Qt import QImage from PyQt4.Qt import QImage

View File

@ -31,7 +31,7 @@ class Field(object):
self.table_type = self.table.table_type self.table_type = self.table.table_type
self._sort_key = (sort_key if dt in ('text', 'series', 'enumeration') else lambda x: x) self._sort_key = (sort_key if dt in ('text', 'series', 'enumeration') else lambda x: x)
self._default_sort_key = '' self._default_sort_key = ''
if dt in { 'int', 'float', 'rating' }: if dt in {'int', 'float', 'rating'}:
self._default_sort_key = 0 self._default_sort_key = 0
elif dt == 'bool': elif dt == 'bool':
self._default_sort_key = None self._default_sort_key = None
@ -138,7 +138,7 @@ class OneToOneField(Field):
return self.table.book_col_map.iterkeys() return self.table.book_col_map.iterkeys()
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
return {id_ : self._sort_key(self.table.book_col_map.get(id_, return {id_: self._sort_key(self.table.book_col_map.get(id_,
self._default_sort_key)) for id_ in all_book_ids} self._default_sort_key)) for id_ in all_book_ids}
def iter_searchable_values(self, get_metadata, candidates, default_value=None): def iter_searchable_values(self, get_metadata, candidates, default_value=None):
@ -183,7 +183,7 @@ class CompositeField(OneToOneField):
return ans return ans
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
return {id_ : sort_key(self.get_value_with_cache(id_, get_metadata)) for id_ in return {id_: sort_key(self.get_value_with_cache(id_, get_metadata)) for id_ in
all_book_ids} all_book_ids}
def iter_searchable_values(self, get_metadata, candidates, default_value=None): def iter_searchable_values(self, get_metadata, candidates, default_value=None):
@ -245,7 +245,7 @@ class OnDeviceField(OneToOneField):
return iter(()) return iter(())
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
return {id_ : self.for_book(id_) for id_ in return {id_: self.for_book(id_) for id_ in
all_book_ids} all_book_ids}
def iter_searchable_values(self, get_metadata, candidates, default_value=None): def iter_searchable_values(self, get_metadata, candidates, default_value=None):
@ -280,12 +280,12 @@ class ManyToOneField(Field):
return self.table.id_map.iterkeys() return self.table.id_map.iterkeys()
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
ans = {id_ : self.table.book_col_map.get(id_, None) ans = {id_: self.table.book_col_map.get(id_, None)
for id_ in all_book_ids} for id_ in all_book_ids}
sk_map = {cid : (self._default_sort_key if cid is None else sk_map = {cid: (self._default_sort_key if cid is None else
self._sort_key(self.table.id_map[cid])) self._sort_key(self.table.id_map[cid]))
for cid in ans.itervalues()} for cid in ans.itervalues()}
return {id_ : sk_map[cid] for id_, cid in ans.iteritems()} return {id_: sk_map[cid] for id_, cid in ans.iteritems()}
def iter_searchable_values(self, get_metadata, candidates, default_value=None): def iter_searchable_values(self, get_metadata, candidates, default_value=None):
cbm = self.table.col_book_map cbm = self.table.col_book_map
@ -327,14 +327,14 @@ class ManyToManyField(Field):
return self.table.id_map.iterkeys() return self.table.id_map.iterkeys()
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
ans = {id_ : self.table.book_col_map.get(id_, ()) ans = {id_: self.table.book_col_map.get(id_, ())
for id_ in all_book_ids} for id_ in all_book_ids}
all_cids = set() all_cids = set()
for cids in ans.itervalues(): for cids in ans.itervalues():
all_cids = all_cids.union(set(cids)) all_cids = all_cids.union(set(cids))
sk_map = {cid : self._sort_key(self.table.id_map[cid]) sk_map = {cid: self._sort_key(self.table.id_map[cid])
for cid in all_cids} for cid in all_cids}
return {id_ : (tuple(sk_map[cid] for cid in cids) if cids else return {id_: (tuple(sk_map[cid] for cid in cids) if cids else
(self._default_sort_key,)) (self._default_sort_key,))
for id_, cids in ans.iteritems()} for id_, cids in ans.iteritems()}
@ -369,9 +369,9 @@ class IdentifiersField(ManyToManyField):
def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids): def sort_keys_for_books(self, get_metadata, lang_map, all_book_ids):
'Sort by identifier keys' 'Sort by identifier keys'
ans = {id_ : self.table.book_col_map.get(id_, ()) ans = {id_: self.table.book_col_map.get(id_, ())
for id_ in all_book_ids} for id_ in all_book_ids}
return {id_ : (tuple(sorted(cids.iterkeys())) if cids else return {id_: (tuple(sorted(cids.iterkeys())) if cids else
(self._default_sort_key,)) (self._default_sort_key,))
for id_, cids in ans.iteritems()} for id_, cids in ans.iteritems()}
@ -397,9 +397,9 @@ class AuthorsField(ManyToManyField):
def author_data(self, author_id): def author_data(self, author_id):
return { return {
'name' : self.table.id_map[author_id], 'name': self.table.id_map[author_id],
'sort' : self.table.asort_map[author_id], 'sort': self.table.asort_map[author_id],
'link' : self.table.alink_map[author_id], 'link': self.table.alink_map[author_id],
} }
def category_sort_value(self, item_id, book_ids, lang_map): def category_sort_value(self, item_id, book_ids, lang_map):
@ -505,9 +505,9 @@ class TagsField(ManyToManyField):
def create_field(name, table): def create_field(name, table):
cls = { cls = {
ONE_ONE : OneToOneField, ONE_ONE: OneToOneField,
MANY_ONE : ManyToOneField, MANY_ONE: ManyToOneField,
MANY_MANY : ManyToManyField, MANY_MANY: ManyToManyField,
}[table.table_type] }[table.table_type]
if name == 'authors': if name == 'authors':
cls = AuthorsField cls = AuthorsField

View File

@ -191,7 +191,7 @@ class SHLock(object): # {{{
try: try:
return self._free_waiters.pop() return self._free_waiters.pop()
except IndexError: except IndexError:
return Condition(self._lock)#, verbose=True) return Condition(self._lock)
def _return_waiter(self, waiter): def _return_waiter(self, waiter):
self._free_waiters.append(waiter) self._free_waiters.append(waiter)

View File

@ -172,7 +172,6 @@ class SchemaUpgrade(object):
''' '''
) )
def upgrade_version_6(self): def upgrade_version_6(self):
'Show authors in order' 'Show authors in order'
self.conn.execute(''' self.conn.execute('''
@ -337,7 +336,7 @@ class SchemaUpgrade(object):
FROM {tn}; FROM {tn};
'''.format(tn=table_name, cn=column_name, '''.format(tn=table_name, cn=column_name,
vcn=view_column_name, scn= sort_column_name)) vcn=view_column_name, scn=sort_column_name))
self.conn.execute(script) self.conn.execute(script)
def create_cust_tag_browser_view(table_name, link_table_name): def create_cust_tag_browser_view(table_name, link_table_name):

View File

@ -64,7 +64,7 @@ def _match(query, value, matchkind, use_primary_find_in_search=True):
else: else:
internal_match_ok = False internal_match_ok = False
for t in value: for t in value:
try: ### ignore regexp exceptions, required because search-ahead tries before typing is finished try: # ignore regexp exceptions, required because search-ahead tries before typing is finished
t = icu_lower(t) t = icu_lower(t)
if (matchkind == EQUALS_MATCH): if (matchkind == EQUALS_MATCH):
if internal_match_ok: if internal_match_ok:
@ -99,16 +99,16 @@ class DateSearch(object): # {{{
def __init__(self): def __init__(self):
self.operators = { self.operators = {
'=' : (1, self.eq), '=': (1, self.eq),
'!=' : (2, self.ne), '!=': (2, self.ne),
'>' : (1, self.gt), '>': (1, self.gt),
'>=' : (2, self.ge), '>=': (2, self.ge),
'<' : (1, self.lt), '<': (1, self.lt),
'<=' : (2, self.le), '<=': (2, self.le),
} }
self.local_today = { '_today', 'today', icu_lower(_('today')) } self.local_today = {'_today', 'today', icu_lower(_('today'))}
self.local_yesterday = { '_yesterday', 'yesterday', icu_lower(_('yesterday')) } self.local_yesterday = {'_yesterday', 'yesterday', icu_lower(_('yesterday'))}
self.local_thismonth = { '_thismonth', 'thismonth', icu_lower(_('thismonth')) } self.local_thismonth = {'_thismonth', 'thismonth', icu_lower(_('thismonth'))}
self.daysago_pat = re.compile(r'(%s|daysago|_daysago)$'%_('daysago')) self.daysago_pat = re.compile(r'(%s|daysago|_daysago)$'%_('daysago'))
def eq(self, dbdate, query, field_count): def eq(self, dbdate, query, field_count):
@ -220,12 +220,12 @@ class NumericSearch(object): # {{{
def __init__(self): def __init__(self):
self.operators = { self.operators = {
'=':( 1, lambda r, q: r == q ), '=':(1, lambda r, q: r == q),
'>':( 1, lambda r, q: r is not None and r > q ), '>':(1, lambda r, q: r is not None and r > q),
'<':( 1, lambda r, q: r is not None and r < q ), '<':(1, lambda r, q: r is not None and r < q),
'!=':( 2, lambda r, q: r != q ), '!=':(2, lambda r, q: r != q),
'>=':( 2, lambda r, q: r is not None and r >= q ), '>=':(2, lambda r, q: r is not None and r >= q),
'<=':( 2, lambda r, q: r is not None and r <= q ) '<=':(2, lambda r, q: r is not None and r <= q)
} }
def __call__(self, query, field_iter, location, datatype, candidates, is_many=False): def __call__(self, query, field_iter, location, datatype, candidates, is_many=False):
@ -325,20 +325,20 @@ class BooleanSearch(object): # {{{
val = force_to_bool(val) val = force_to_bool(val)
if not bools_are_tristate: if not bools_are_tristate:
if val is None or not val: # item is None or set to false if val is None or not val: # item is None or set to false
if query in { self.local_no, self.local_unchecked, 'no', '_no', 'false' }: if query in {self.local_no, self.local_unchecked, 'no', '_no', 'false'}:
matches |= book_ids matches |= book_ids
else: # item is explicitly set to true else: # item is explicitly set to true
if query in { self.local_yes, self.local_checked, 'yes', '_yes', 'true' }: if query in {self.local_yes, self.local_checked, 'yes', '_yes', 'true'}:
matches |= book_ids matches |= book_ids
else: else:
if val is None: if val is None:
if query in { self.local_empty, self.local_blank, 'empty', '_empty', 'false' }: if query in {self.local_empty, self.local_blank, 'empty', '_empty', 'false'}:
matches |= book_ids matches |= book_ids
elif not val: # is not None and false elif not val: # is not None and false
if query in { self.local_no, self.local_unchecked, 'no', '_no', 'true' }: if query in {self.local_no, self.local_unchecked, 'no', '_no', 'true'}:
matches |= book_ids matches |= book_ids
else: # item is not None and true else: # item is not None and true
if query in { self.local_yes, self.local_checked, 'yes', '_yes', 'true' }: if query in {self.local_yes, self.local_checked, 'yes', '_yes', 'true'}:
matches |= book_ids matches |= book_ids
return matches return matches
@ -547,11 +547,12 @@ class Parser(SearchQueryParser):
field_metadata = {} field_metadata = {}
for x, fm in self.field_metadata.iteritems(): for x, fm in self.field_metadata.iteritems():
if x.startswith('@'): continue if x.startswith('@'):
continue
if fm['search_terms'] and x != 'series_sort': if fm['search_terms'] and x != 'series_sort':
all_locs.add(x) all_locs.add(x)
field_metadata[x] = fm field_metadata[x] = fm
if fm['datatype'] in { 'composite', 'text', 'comments', 'series', 'enumeration' }: if fm['datatype'] in {'composite', 'text', 'comments', 'series', 'enumeration'}:
text_fields.add(x) text_fields.add(x)
locations = all_locs if location == 'all' else {location} locations = all_locs if location == 'all' else {location}
@ -687,8 +688,8 @@ class Search(object):
dbcache, all_book_ids, dbcache.pref('grouped_search_terms'), dbcache, all_book_ids, dbcache.pref('grouped_search_terms'),
self.date_search, self.num_search, self.bool_search, self.date_search, self.num_search, self.bool_search,
self.keypair_search, self.keypair_search,
prefs[ 'limit_search_columns' ], prefs['limit_search_columns'],
prefs[ 'limit_search_columns_to' ], self.all_search_locations, prefs['limit_search_columns_to'], self.all_search_locations,
virtual_fields) virtual_fields)
try: try:

View File

@ -60,10 +60,10 @@ class View(object):
else: else:
try: try:
self._field_getters[idx] = { self._field_getters[idx] = {
'id' : self._get_id, 'id': self._get_id,
'au_map' : self.get_author_data, 'au_map': self.get_author_data,
'ondevice': self.get_ondevice, 'ondevice': self.get_ondevice,
'marked' : self.get_marked, 'marked': self.get_marked,
}[col] }[col]
except KeyError: except KeyError:
self._field_getters[idx] = partial(self.get, col) self._field_getters[idx] = partial(self.get, col)