Refactor to use new field formatting infrastructure of Metadata class

This commit is contained in:
Kovid Goyal 2010-09-18 20:37:59 -06:00
parent db2f4dca7f
commit 3f763407a0
7 changed files with 51 additions and 77 deletions

View File

@ -137,7 +137,6 @@ class CollectionsBookList(BookList):
# For existing books, modify the collections only if the user # For existing books, modify the collections only if the user
# specified 'on_connect' # specified 'on_connect'
attrs = collection_attributes attrs = collection_attributes
meta_vals = book.get_all_non_none_attributes()
for attr in attrs: for attr in attrs:
attr = attr.strip() attr = attr.strip()
ign, val, orig_val, fm = book.format_field_extended(attr) ign, val, orig_val, fm = book.format_field_extended(attr)
@ -166,7 +165,7 @@ class CollectionsBookList(BookList):
continue continue
if attr == 'series' or \ if attr == 'series' or \
('series' in collection_attributes and ('series' in collection_attributes and
meta_vals.get('series', None) == category): book.get('series', None) == category):
is_series = True is_series = True
cat_name = self.compute_category_name(attr, category, fm) cat_name = self.compute_category_name(attr, category, fm)
if cat_name not in collections: if cat_name not in collections:
@ -177,10 +176,10 @@ class CollectionsBookList(BookList):
collections_lpaths[cat_name].add(lpath) collections_lpaths[cat_name].add(lpath)
if is_series: if is_series:
collections[cat_name].append( collections[cat_name].append(
(book, meta_vals.get(attr+'_index', sys.maxint))) (book, book.get(attr+'_index', sys.maxint)))
else: else:
collections[cat_name].append( collections[cat_name].append(
(book, meta_vals.get('title_sort', 'zzzz'))) (book, book.get('title_sort', 'zzzz')))
# Sort collections # Sort collections
result = {} result = {}
for category, books in collections.items(): for category, books in collections.items():

View File

@ -81,9 +81,8 @@ DEVICE_METADATA_FIELDS = frozenset([
CALIBRE_METADATA_FIELDS = frozenset([ CALIBRE_METADATA_FIELDS = frozenset([
'application_id', # An application id, currently set to the db_id. '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. '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( PUBLICATION_METADATA_FIELDS).union(
CALIBRE_METADATA_FIELDS).union( CALIBRE_METADATA_FIELDS).union(
DEVICE_METADATA_FIELDS) - \ DEVICE_METADATA_FIELDS) - \
frozenset(['device_collections']) frozenset(['device_collections', 'formats'])
# device_collections is rebuilt when needed # these are rebuilt when needed

View File

@ -343,26 +343,26 @@ class Metadata(object):
def format_rating(self): def format_rating(self):
return unicode(self.rating) return unicode(self.rating)
def format_field(self, key): def format_field(self, key, series_with_index=True):
name, val, ign, ign = self.format_field_extended(key) name, val, ign, ign = self.format_field_extended(key, series_with_index)
return (name, val) 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 from calibre.ebooks.metadata import authors_to_string
''' '''
returns the tuple (field_name, formatted_value) returns the tuple (field_name, formatted_value)
''' '''
if key in self.user_metadata_keys: if key in self.user_metadata_keys:
res = self.get(key, None) res = self.get(key, None)
cmeta = self.get_user_metadata(key, make_copy=False)
if res is None or res == '': if res is None or res == '':
return (None, None, None, None) return (None, None, None, None)
orig_res = res orig_res = res
cmeta = self.get_user_metadata(key, make_copy=False)
name = unicode(cmeta['name']) name = unicode(cmeta['name'])
datatype = cmeta['datatype'] datatype = cmeta['datatype']
if datatype == 'text' and cmeta['is_multiple']: if datatype == 'text' and cmeta['is_multiple']:
res = u', '.join(res) res = u', '.join(res)
elif datatype == 'series': elif datatype == 'series' and series_with_index:
res = res + \ res = res + \
' [%s]'%self.format_series_index(val=self.get_extra(key)) ' [%s]'%self.format_series_index(val=self.get_extra(key))
elif datatype == 'datetime': elif datatype == 'datetime':
@ -383,7 +383,7 @@ class Metadata(object):
res = authors_to_string(res) res = authors_to_string(res)
elif datatype == 'text' and fmeta['is_multiple']: elif datatype == 'text' and fmeta['is_multiple']:
res = u', '.join(res) res = u', '.join(res)
elif datatype == 'series': elif datatype == 'series' and series_with_index:
res = res + ' [%s]'%self.format_series_index() res = res + ' [%s]'%self.format_series_index()
elif datatype == 'datetime': elif datatype == 'datetime':
res = format_date(res, fmeta['display'].get('date_format','dd MMM yyyy')) res = format_date(res, fmeta['display'].get('date_format','dd MMM yyyy'))

View File

@ -538,6 +538,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
mi.pubdate = self.pubdate(idx, index_is_id=index_is_id) mi.pubdate = self.pubdate(idx, index_is_id=index_is_id)
mi.uuid = self.uuid(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.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) tags = self.tags(idx, index_is_id=index_is_id)
if tags: if tags:
mi.tags = [i.strip() for i in tags.split(',')] mi.tags = [i.strip() for i in tags.split(',')]

View File

@ -228,29 +228,19 @@ class MobileServer(object):
for key in CKEYS: for key in CKEYS:
def concat(name, val): def concat(name, val):
return '%s:#:%s'%(name, unicode(val)) return '%s:#:%s'%(name, unicode(val))
val = record[CFM[key]['rec_index']] mi = self.db.get_metadata(record[CFM['id']['rec_index']], index_is_id=True)
if val: name, val = mi.format_field(key)
datatype = CFM[key]['datatype'] if val is None:
if datatype in ['comments']: continue
continue datatype = CFM[key]['datatype']
name = CFM[key]['name'] if datatype in ['comments']:
if datatype == 'text' and CFM[key]['is_multiple']: continue
book[key] = concat(name, if datatype == 'text' and CFM[key]['is_multiple']:
format_tag_string(val, '|', book[key] = concat(name,
no_tag_count=True)) format_tag_string(val, ',',
elif datatype == 'series': no_tag_count=True))
book[key] = concat(name, '%s [%s]'%(val, else:
fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) book[key] = concat(name, val)
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)
updated = self.db.last_modified() updated = self.db.last_modified()

View File

@ -20,7 +20,6 @@ from calibre.library.comments import comments_to_html
from calibre.library.server.utils import format_tag_string from calibre.library.server.utils import format_tag_string
from calibre import guess_type from calibre import guess_type
from calibre.utils.ordered_dict import OrderedDict from calibre.utils.ordered_dict import OrderedDict
from calibre.utils.date import format_date
BASE_HREFS = { BASE_HREFS = {
0 : '/stanza', 0 : '/stanza',
@ -132,7 +131,8 @@ def CATALOG_GROUP_ENTRY(item, category, base_href, version, updated):
link 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']] title = item[FM['title']]
if not title: if not title:
title = _('Unknown') title = _('Unknown')
@ -157,22 +157,16 @@ def ACQUISITION_ENTRY(item, version, FM, updated, CFM, CKEYS):
(series, (series,
fmt_sidx(float(item[FM['series_index']])))) fmt_sidx(float(item[FM['series_index']]))))
for key in CKEYS: 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: if val is not None:
name = CFM[key]['name']
datatype = CFM[key]['datatype'] datatype = CFM[key]['datatype']
if datatype == 'text' and CFM[key]['is_multiple']: if datatype == 'text' and CFM[key]['is_multiple']:
extra.append('%s: %s<br />'%(name, format_tag_string(val, '|', extra.append('%s: %s<br />'%(name, format_tag_string(val, ',',
ignore_max=True, ignore_max=True,
no_tag_count=True))) no_tag_count=True)))
elif datatype == 'series':
extra.append('%s: %s [%s]<br />'%(name, val,
fmt_sidx(item[CFM.cc_series_index_column_for(key)])))
elif datatype == 'datetime':
extra.append('%s: %s<br />'%(name,
format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy'))))
else: else:
extra.append('%s: %s <br />' % (CFM[key]['name'], val)) extra.append('%s: %s<br />'%(name, val))
comments = item[FM['comments']] comments = item[FM['comments']]
if comments: if comments:
comments = comments_to_html(comments) comments = comments_to_html(comments)
@ -280,13 +274,14 @@ class NavFeed(Feed):
class AcquisitionFeed(NavFeed): class AcquisitionFeed(NavFeed):
def __init__(self, updated, id_, items, offsets, page_url, up_url, version, 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) 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(), CKEYS = [key for key in sorted(CFM.get_custom_fields(),
cmp=lambda x,y: cmp(CFM[x]['name'].lower(), cmp=lambda x,y: cmp(CFM[x]['name'].lower(),
CFM[y]['name'].lower()))] CFM[y]['name'].lower()))]
for item in items: for item in items:
self.root.append(ACQUISITION_ENTRY(item, version, FM, updated, self.root.append(ACQUISITION_ENTRY(item, version, db, updated,
CFM, CKEYS)) CFM, CKEYS))
class CategoryFeed(NavFeed): class CategoryFeed(NavFeed):
@ -384,7 +379,7 @@ class OPDSServer(object):
cherrypy.response.headers['Last-Modified'] = self.last_modified(updated) cherrypy.response.headers['Last-Modified'] = self.last_modified(updated)
cherrypy.response.headers['Content-Type'] = 'application/atom+xml;profile=opds-catalog' cherrypy.response.headers['Content-Type'] = 'application/atom+xml;profile=opds-catalog'
return str(AcquisitionFeed(updated, id_, items, offsets, 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): def opds_search(self, query=None, version=0, offset=0):
try: try:

View File

@ -102,31 +102,21 @@ class XMLServer(object):
for key in CKEYS: for key in CKEYS:
def concat(name, val): def concat(name, val):
return '%s:#:%s'%(name, unicode(val)) return '%s:#:%s'%(name, unicode(val))
val = record[CFM[key]['rec_index']] mi = self.db.get_metadata(record[CFM['id']['rec_index']], index_is_id=True)
if val: name, val = mi.format_field(key)
datatype = CFM[key]['datatype'] if not val:
if datatype in ['comments']: continue
continue datatype = CFM[key]['datatype']
k = str('CF_'+key[1:]) if datatype in ['comments']:
name = CFM[key]['name'] continue
custcols.append(k) k = str('CF_'+key[1:])
if datatype == 'text' and CFM[key]['is_multiple']: name = CFM[key]['name']
kwargs[k] = concat('#T#'+name, custcols.append(k)
format_tag_string(val,'|', if datatype == 'text' and CFM[key]['is_multiple']:
ignore_max=True)) kwargs[k] = concat('#T#'+name, format_tag_string(val,',',
elif datatype == 'series': ignore_max=True))
kwargs[k] = concat(name, '%s [%s]'%(val, else:
fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) kwargs[k] = concat(name, val)
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)
kwargs['custcols'] = ','.join(custcols) kwargs['custcols'] = ','.join(custcols)
books.append(E.book(c, **kwargs)) books.append(E.book(c, **kwargs))