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
# 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():

View File

@ -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

View File

@ -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'))

View File

@ -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(',')]

View File

@ -228,27 +228,17 @@ 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:
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
name = CFM[key]['name']
if datatype == 'text' and CFM[key]['is_multiple']:
book[key] = concat(name,
format_tag_string(val, '|',
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)

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 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<br />'%(name, format_tag_string(val, '|',
extra.append('%s: %s<br />'%(name, format_tag_string(val, ',',
ignore_max=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:
extra.append('%s: %s <br />' % (CFM[key]['name'], val))
extra.append('%s: %s<br />'%(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:

View File

@ -102,8 +102,10 @@ 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:
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
@ -111,20 +113,8 @@ class XMLServer(object):
name = CFM[key]['name']
custcols.append(k)
if datatype == 'text' and CFM[key]['is_multiple']:
kwargs[k] = concat('#T#'+name,
format_tag_string(val,'|',
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)
kwargs['custcols'] = ','.join(custcols)