Add API docs for the Metadata object to the template tutorial

This commit is contained in:
Kovid Goyal 2011-04-27 15:28:48 -06:00
parent fed64f9a17
commit 0a31b52a42
2 changed files with 49 additions and 3 deletions

View File

@ -68,7 +68,19 @@ composite_formatter = SafeFormat()
class Metadata(object): class Metadata(object):
''' '''
A class representing all the metadata for a book. A class representing all the metadata for a book. The various standard metadata
fields are available as attributes of this object. You can also stick
arbitrary attributes onto this object.
Metadata from custom columns should be accessed via the get() method,
passing in the lookup name for the column, for example: "#mytags".
Use the :meth:`is_null` method to test if a filed is null.
This object also has functions to format fields into strings.
The list of standard metadata fields grows with time is in
:data:`STANDARD_METADATA_FIELDS`.
Please keep the method based API of this class to a minimum. Every method Please keep the method based API of this class to a minimum. Every method
becomes a reserved field name. becomes a reserved field name.
@ -88,11 +100,19 @@ class Metadata(object):
if title: if title:
self.title = title self.title = title
if authors: if authors:
#: List of strings or [] # List of strings or []
self.author = list(authors) if authors else []# Needed for backward compatibility self.author = list(authors) if authors else []# Needed for backward compatibility
self.authors = list(authors) if authors else [] self.authors = list(authors) if authors else []
def is_null(self, field): def is_null(self, field):
'''
Return True if the value of filed is null in this object.
'null' means it is unknown or evaluates to False. So a title of
_('Unknown') is null or a language of 'und' is null.
Be careful with numeric fields since this will return True for zero as
well as None.
'''
null_val = NULL_VALUES.get(field, None) null_val = NULL_VALUES.get(field, None)
val = getattr(self, field, None) val = getattr(self, field, None)
return not val or val == null_val return not val or val == null_val
@ -547,13 +567,16 @@ class Metadata(object):
return unicode(self.rating) return unicode(self.rating)
def format_field(self, key, series_with_index=True): def format_field(self, key, series_with_index=True):
'''
Returns the tuple (display_name, formatted_value)
'''
name, val, ign, ign = self.format_field_extended(key, series_with_index) name, val, ign, ign = self.format_field_extended(key, series_with_index)
return (name, val) return (name, val)
def format_field_extended(self, key, series_with_index=True): 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, original_value, returns the tuple (display_name, formatted_value, original_value,
field_metadata) field_metadata)
''' '''
@ -637,6 +660,10 @@ class Metadata(object):
return (None, None, None, None) return (None, None, None, None)
def __unicode__(self): def __unicode__(self):
'''
A string representation of this object, suitable for printing to
console
'''
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
ans = [] ans = []
def fmt(x, y): def fmt(x, y):
@ -680,6 +707,9 @@ class Metadata(object):
return u'\n'.join(ans) return u'\n'.join(ans)
def to_html(self): def to_html(self):
'''
A HTML representation of this object.
'''
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
ans = [(_('Title'), unicode(self.title))] ans = [(_('Title'), unicode(self.title))]
ans += [(_('Author(s)'), (authors_to_string(self.authors) if self.authors else _('Unknown')))] ans += [(_('Author(s)'), (authors_to_string(self.authors) if self.authors else _('Unknown')))]

View File

@ -396,3 +396,19 @@ You might find the following tips useful.
* In a plugboard, you can set a field to empty (or whatever is equivalent to empty) by using the special template ``{null}``. This template will always evaluate to an empty string. * In a plugboard, you can set a field to empty (or whatever is equivalent to empty) by using the special template ``{null}``. This template will always evaluate to an empty string.
* The technique described above to show numbers even if they have a zero value works with the standard field series_index. * The technique described above to show numbers even if they have a zero value works with the standard field series_index.
API of the Metadata objects
----------------------------
.. module:: calibre.ebooks.metadata.book.base
.. autoclass:: Metadata
:members:
:member-order: bysource
.. data:: STANDARD_METADATA_FIELDS
The set of standard metadata fields.
.. literalinclude:: ../ebooks/metadata/book/__init__.py
:lines: 7-