From 3f763407a02f5c00599bdbe43f053a821fb0a3e3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 18 Sep 2010 20:37:59 -0600 Subject: [PATCH] Refactor to use new field formatting infrastructure of Metadata class --- src/calibre/devices/usbms/books.py | 7 ++-- src/calibre/ebooks/metadata/book/__init__.py | 7 ++-- src/calibre/ebooks/metadata/book/base.py | 12 +++--- src/calibre/library/database2.py | 1 + src/calibre/library/server/mobile.py | 36 +++++++----------- src/calibre/library/server/opds.py | 25 +++++------- src/calibre/library/server/xml.py | 40 ++++++++------------ 7 files changed, 51 insertions(+), 77 deletions(-) diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index eab625f7be..13fcb90b49 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -137,7 +137,6 @@ class CollectionsBookList(BookList): # For existing books, modify the collections only if the user # specified 'on_connect' attrs = collection_attributes - meta_vals = book.get_all_non_none_attributes() for attr in attrs: attr = attr.strip() ign, val, orig_val, fm = book.format_field_extended(attr) @@ -166,7 +165,7 @@ class CollectionsBookList(BookList): continue if attr == 'series' or \ ('series' in collection_attributes and - meta_vals.get('series', None) == category): + book.get('series', None) == category): is_series = True cat_name = self.compute_category_name(attr, category, fm) if cat_name not in collections: @@ -177,10 +176,10 @@ class CollectionsBookList(BookList): collections_lpaths[cat_name].add(lpath) if is_series: collections[cat_name].append( - (book, meta_vals.get(attr+'_index', sys.maxint))) + (book, book.get(attr+'_index', sys.maxint))) else: collections[cat_name].append( - (book, meta_vals.get('title_sort', 'zzzz'))) + (book, book.get('title_sort', 'zzzz'))) # Sort collections result = {} for category, books in collections.items(): diff --git a/src/calibre/ebooks/metadata/book/__init__.py b/src/calibre/ebooks/metadata/book/__init__.py index e087f8072d..e6dff9110b 100644 --- a/src/calibre/ebooks/metadata/book/__init__.py +++ b/src/calibre/ebooks/metadata/book/__init__.py @@ -81,9 +81,8 @@ DEVICE_METADATA_FIELDS = frozenset([ CALIBRE_METADATA_FIELDS = frozenset([ 'application_id', # An application id, currently set to the db_id. - # the calibre primary key of the item. 'db_id', # the calibre primary key of the item. - # TODO: NEWMETA: May want to remove once Sony's no longer use it + 'formats', # list of formats (extensions) for this book ] ) @@ -124,5 +123,5 @@ SERIALIZABLE_FIELDS = SOCIAL_METADATA_FIELDS.union( PUBLICATION_METADATA_FIELDS).union( CALIBRE_METADATA_FIELDS).union( DEVICE_METADATA_FIELDS) - \ - frozenset(['device_collections']) - # device_collections is rebuilt when needed + frozenset(['device_collections', 'formats']) + # these are rebuilt when needed diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index b252f518da..8868709db2 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -343,26 +343,26 @@ class Metadata(object): def format_rating(self): return unicode(self.rating) - def format_field(self, key): - name, val, ign, ign = self.format_field_extended(key) + def format_field(self, key, series_with_index=True): + name, val, ign, ign = self.format_field_extended(key, series_with_index) return (name, val) - def format_field_extended(self, key): + def format_field_extended(self, key, series_with_index=True): from calibre.ebooks.metadata import authors_to_string ''' returns the tuple (field_name, formatted_value) ''' if key in self.user_metadata_keys: res = self.get(key, None) + cmeta = self.get_user_metadata(key, make_copy=False) if res is None or res == '': return (None, None, None, None) orig_res = res - cmeta = self.get_user_metadata(key, make_copy=False) name = unicode(cmeta['name']) datatype = cmeta['datatype'] if datatype == 'text' and cmeta['is_multiple']: res = u', '.join(res) - elif datatype == 'series': + elif datatype == 'series' and series_with_index: res = res + \ ' [%s]'%self.format_series_index(val=self.get_extra(key)) elif datatype == 'datetime': @@ -383,7 +383,7 @@ class Metadata(object): res = authors_to_string(res) elif datatype == 'text' and fmeta['is_multiple']: res = u', '.join(res) - elif datatype == 'series': + elif datatype == 'series' and series_with_index: res = res + ' [%s]'%self.format_series_index() elif datatype == 'datetime': res = format_date(res, fmeta['display'].get('date_format','dd MMM yyyy')) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 9e9e75a26e..d06d217b76 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -538,6 +538,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): mi.pubdate = self.pubdate(idx, index_is_id=index_is_id) mi.uuid = self.uuid(idx, index_is_id=index_is_id) mi.title_sort = self.title_sort(idx, index_is_id=index_is_id) + mi.formats = self.formats(idx, index_is_id=index_is_id).split(',') tags = self.tags(idx, index_is_id=index_is_id) if tags: mi.tags = [i.strip() for i in tags.split(',')] diff --git a/src/calibre/library/server/mobile.py b/src/calibre/library/server/mobile.py index ab5b39eed8..8e7c75b0ac 100644 --- a/src/calibre/library/server/mobile.py +++ b/src/calibre/library/server/mobile.py @@ -228,29 +228,19 @@ class MobileServer(object): for key in CKEYS: def concat(name, val): return '%s:#:%s'%(name, unicode(val)) - val = record[CFM[key]['rec_index']] - if val: - datatype = CFM[key]['datatype'] - if datatype in ['comments']: - continue - name = CFM[key]['name'] - if datatype == 'text' and CFM[key]['is_multiple']: - book[key] = concat(name, - format_tag_string(val, '|', - no_tag_count=True)) - elif datatype == 'series': - book[key] = concat(name, '%s [%s]'%(val, - fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) - elif datatype == 'datetime': - book[key] = concat(name, - format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy'))) - elif datatype == 'bool': - if val: - book[key] = concat(name, __builtin__._('Yes')) - else: - book[key] = concat(name, __builtin__._('No')) - else: - book[key] = concat(name, val) + mi = self.db.get_metadata(record[CFM['id']['rec_index']], index_is_id=True) + name, val = mi.format_field(key) + if val is None: + continue + datatype = CFM[key]['datatype'] + if datatype in ['comments']: + continue + if datatype == 'text' and CFM[key]['is_multiple']: + book[key] = concat(name, + format_tag_string(val, ',', + no_tag_count=True)) + else: + book[key] = concat(name, val) updated = self.db.last_modified() diff --git a/src/calibre/library/server/opds.py b/src/calibre/library/server/opds.py index e495598a2f..d495f58fa1 100644 --- a/src/calibre/library/server/opds.py +++ b/src/calibre/library/server/opds.py @@ -20,7 +20,6 @@ from calibre.library.comments import comments_to_html from calibre.library.server.utils import format_tag_string from calibre import guess_type from calibre.utils.ordered_dict import OrderedDict -from calibre.utils.date import format_date BASE_HREFS = { 0 : '/stanza', @@ -132,7 +131,8 @@ def CATALOG_GROUP_ENTRY(item, category, base_href, version, updated): link ) -def ACQUISITION_ENTRY(item, version, FM, updated, CFM, CKEYS): +def ACQUISITION_ENTRY(item, version, db, updated, CFM, CKEYS): + FM = db.FIELD_MAP title = item[FM['title']] if not title: title = _('Unknown') @@ -157,22 +157,16 @@ def ACQUISITION_ENTRY(item, version, FM, updated, CFM, CKEYS): (series, fmt_sidx(float(item[FM['series_index']])))) for key in CKEYS: - val = item[CFM[key]['rec_index']] + mi = db.get_metadata(item[CFM['id']['rec_index']], index_is_id=True) + name, val = mi.format_field(key) if val is not None: - name = CFM[key]['name'] datatype = CFM[key]['datatype'] if datatype == 'text' and CFM[key]['is_multiple']: - extra.append('%s: %s
'%(name, format_tag_string(val, '|', + extra.append('%s: %s
'%(name, format_tag_string(val, ',', ignore_max=True, no_tag_count=True))) - elif datatype == 'series': - extra.append('%s: %s [%s]
'%(name, val, - fmt_sidx(item[CFM.cc_series_index_column_for(key)]))) - elif datatype == 'datetime': - extra.append('%s: %s
'%(name, - format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy')))) else: - extra.append('%s: %s
' % (CFM[key]['name'], val)) + extra.append('%s: %s
'%(name, val)) comments = item[FM['comments']] if comments: comments = comments_to_html(comments) @@ -280,13 +274,14 @@ class NavFeed(Feed): class AcquisitionFeed(NavFeed): def __init__(self, updated, id_, items, offsets, page_url, up_url, version, - FM, CFM): + db): NavFeed.__init__(self, id_, updated, version, offsets, page_url, up_url) + CFM = db.field_metadata CKEYS = [key for key in sorted(CFM.get_custom_fields(), cmp=lambda x,y: cmp(CFM[x]['name'].lower(), CFM[y]['name'].lower()))] for item in items: - self.root.append(ACQUISITION_ENTRY(item, version, FM, updated, + self.root.append(ACQUISITION_ENTRY(item, version, db, updated, CFM, CKEYS)) class CategoryFeed(NavFeed): @@ -384,7 +379,7 @@ class OPDSServer(object): cherrypy.response.headers['Last-Modified'] = self.last_modified(updated) cherrypy.response.headers['Content-Type'] = 'application/atom+xml;profile=opds-catalog' return str(AcquisitionFeed(updated, id_, items, offsets, - page_url, up_url, version, self.db.FIELD_MAP, self.db.field_metadata)) + page_url, up_url, version, self.db)) def opds_search(self, query=None, version=0, offset=0): try: diff --git a/src/calibre/library/server/xml.py b/src/calibre/library/server/xml.py index 8715dda7d0..7f5bc31e70 100644 --- a/src/calibre/library/server/xml.py +++ b/src/calibre/library/server/xml.py @@ -102,31 +102,21 @@ class XMLServer(object): for key in CKEYS: def concat(name, val): return '%s:#:%s'%(name, unicode(val)) - val = record[CFM[key]['rec_index']] - if val: - datatype = CFM[key]['datatype'] - if datatype in ['comments']: - continue - k = str('CF_'+key[1:]) - name = CFM[key]['name'] - custcols.append(k) - if datatype == 'text' and CFM[key]['is_multiple']: - kwargs[k] = concat('#T#'+name, - format_tag_string(val,'|', - ignore_max=True)) - elif datatype == 'series': - kwargs[k] = concat(name, '%s [%s]'%(val, - fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) - elif datatype == 'datetime': - kwargs[k] = concat(name, - format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy'))) - elif datatype == 'bool': - if val: - kwargs[k] = concat(name, __builtin__._('Yes')) - else: - kwargs[k] = concat(name, __builtin__._('No')) - else: - kwargs[k] = concat(name, val) + mi = self.db.get_metadata(record[CFM['id']['rec_index']], index_is_id=True) + name, val = mi.format_field(key) + if not val: + continue + datatype = CFM[key]['datatype'] + if datatype in ['comments']: + continue + k = str('CF_'+key[1:]) + name = CFM[key]['name'] + custcols.append(k) + if datatype == 'text' and CFM[key]['is_multiple']: + kwargs[k] = concat('#T#'+name, format_tag_string(val,',', + ignore_max=True)) + else: + kwargs[k] = concat(name, val) kwargs['custcols'] = ','.join(custcols) books.append(E.book(c, **kwargs))