From 8b9b64a8e6bdab03f62c98a4f2c35ec73957cca7 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 24 Sep 2010 11:34:52 +0100 Subject: [PATCH] 1) add two tweaks controlling what custom fields the content server displays 2) add & cleanup some field_metadata methods --- resources/default_tweaks.py | 18 ++++++++++++++++++ src/calibre/gui2/library/models.py | 2 +- src/calibre/library/database2.py | 6 ++++++ src/calibre/library/field_metadata.py | 2 +- src/calibre/library/server/__init__.py | 12 +++++++++++- src/calibre/library/server/mobile.py | 3 ++- src/calibre/library/server/opds.py | 3 ++- src/calibre/library/server/xml.py | 3 ++- 8 files changed, 43 insertions(+), 6 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 04b861605e..095eba0c3d 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -145,6 +145,24 @@ add_new_book_tags_when_importing_books = False # Set the maximum number of tags to show per book in the content server max_content_server_tags_shown=5 +# Set custom metadata fields that the content server will or will not display. +# content_server_will_display is a list of custom fields to be displayed. +# content_server_wont_display is a list of custom fields not to be displayed. +# wont_display has priority over will_display. +# The special value '*' means all custom fields. +# Defaults: +# content_server_will_display = ['*'] +# content_server_wont_display = [''] +# Examples: +# To display only the custom fields #mytags and #genre: +# content_server_will_display = ['#mytags', '#genre'] +# content_server_wont_display = [''] +# To display all fields except #mycomments: +# content_server_will_display = ['*'] +# content_server_wont_display['#mycomments'] +content_server_will_display = ['*'] +content_server_wont_display = [''] + # Set the maximum number of sort 'levels' that calibre will use to resort the # library after certain operations such as searches or device insertion. Each diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 640a588d29..af1b42bf33 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -132,7 +132,7 @@ class BooksModel(QAbstractTableModel): # {{{ def set_database(self, db): self.db = db - self.custom_columns = self.db.field_metadata.get_custom_field_metadata() + self.custom_columns = self.db.field_metadata.custom_field_metadata() self.column_map = list(self.orig_headers.keys()) + \ list(self.custom_columns) def col_idx(name): diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 773a4bdc9f..c7c4926b14 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -554,6 +554,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): def search_term_to_field_key(self, term): return self.field_metadata.search_term_to_key(term) + def custom_field_metadata(self): + return self.field_metadata.custom_field_metadata() + + def all_metadata(self): + return self.field_metadata.all_metadata() + def metadata_for_field(self, key): return self.field_metadata[key] diff --git a/src/calibre/library/field_metadata.py b/src/calibre/library/field_metadata.py index bac423f46d..d608dca49d 100644 --- a/src/calibre/library/field_metadata.py +++ b/src/calibre/library/field_metadata.py @@ -411,7 +411,7 @@ class FieldMetadata(dict): l[k] = self._tb_cats[k] return l - def get_custom_field_metadata(self): + def custom_field_metadata(self): l = {} for k in self._tb_cats: if self._tb_cats[k]['is_custom']: diff --git a/src/calibre/library/server/__init__.py b/src/calibre/library/server/__init__.py index 5050dfaa99..7cdea9f602 100644 --- a/src/calibre/library/server/__init__.py +++ b/src/calibre/library/server/__init__.py @@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en' import os -from calibre.utils.config import Config, StringConfig, config_dir +from calibre.utils.config import Config, StringConfig, config_dir, tweaks listen_on = '0.0.0.0' @@ -46,6 +46,16 @@ def server_config(defaults=None): 'to disable grouping.')) return c +def custom_fields_to_display(db): + ckeys = db.custom_field_keys() + yes_fields = set(tweaks['content_server_will_display']) + no_fields = set(tweaks['content_server_wont_display']) + if '*' in yes_fields: + yes_fields = set(ckeys) + if '*' in no_fields: + no_fields = set(ckeys) + return frozenset(yes_fields - no_fields) + def main(): from calibre.library.server.main import main return main() diff --git a/src/calibre/library/server/mobile.py b/src/calibre/library/server/mobile.py index c0a3c122cd..071c7b1077 100644 --- a/src/calibre/library/server/mobile.py +++ b/src/calibre/library/server/mobile.py @@ -13,6 +13,7 @@ from lxml import html from lxml.html.builder import HTML, HEAD, TITLE, LINK, DIV, IMG, BODY, \ OPTION, SELECT, INPUT, FORM, SPAN, TABLE, TR, TD, A, HR +from calibre.library.server import custom_fields_to_display from calibre.library.server.utils import strftime, format_tag_string from calibre.ebooks.metadata import fmt_sidx from calibre.constants import __appname__ @@ -197,7 +198,7 @@ class MobileServer(object): self.sort(items, sort, (order.lower().strip() == 'ascending')) CFM = self.db.field_metadata - CKEYS = [key for key in sorted(CFM.get_custom_fields(), + CKEYS = [key for key in sorted(custom_fields_to_display(self.db), cmp=lambda x,y: cmp(CFM[x]['name'].lower(), CFM[y]['name'].lower()))] # This method uses its own book dict, not the Metadata dict. The loop diff --git a/src/calibre/library/server/opds.py b/src/calibre/library/server/opds.py index d495f58fa1..0e6917c504 100644 --- a/src/calibre/library/server/opds.py +++ b/src/calibre/library/server/opds.py @@ -17,6 +17,7 @@ import routes from calibre.constants import __appname__ from calibre.ebooks.metadata import fmt_sidx from calibre.library.comments import comments_to_html +from calibre.library.server import custom_fields_to_display from calibre.library.server.utils import format_tag_string from calibre import guess_type from calibre.utils.ordered_dict import OrderedDict @@ -277,7 +278,7 @@ class AcquisitionFeed(NavFeed): 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(), + CKEYS = [key for key in sorted(custom_fields_to_display(db), cmp=lambda x,y: cmp(CFM[x]['name'].lower(), CFM[y]['name'].lower()))] for item in items: diff --git a/src/calibre/library/server/xml.py b/src/calibre/library/server/xml.py index 45ffdc2737..12fcc217f0 100644 --- a/src/calibre/library/server/xml.py +++ b/src/calibre/library/server/xml.py @@ -11,6 +11,7 @@ import cherrypy from lxml.builder import ElementMaker from lxml import etree +from calibre.library.server import custom_fields_to_display from calibre.library.server.utils import strftime, format_tag_string from calibre.ebooks.metadata import fmt_sidx from calibre.constants import preferred_encoding @@ -94,7 +95,7 @@ class XMLServer(object): c = kwargs.pop('comments') CFM = self.db.field_metadata - CKEYS = [key for key in sorted(CFM.get_custom_fields(), + CKEYS = [key for key in sorted(custom_fields_to_display(self.db), cmp=lambda x,y: cmp(CFM[x]['name'].lower(), CFM[y]['name'].lower()))] custcols = []