diff --git a/.bzrignore b/.bzrignore index 005391bf46..d2a2d592dd 100644 --- a/.bzrignore +++ b/.bzrignore @@ -4,6 +4,7 @@ src/calibre/plugins resources/images.qrc src/calibre/manual/.build/ src/calibre/manual/cli/ +src/calibre/manual/template_ref.rst build dist docs @@ -31,4 +32,4 @@ nbproject/ .pydevproject .settings/ *.DS_Store -calibre_plugins/ \ No newline at end of file +calibre_plugins/ diff --git a/recipes/bbc_fast.recipe b/recipes/bbc_fast.recipe index 93ee11ce32..9bcbfb5f70 100644 --- a/recipes/bbc_fast.recipe +++ b/recipes/bbc_fast.recipe @@ -1,27 +1,30 @@ __license__ = 'GPL v3' -__copyright__ = '2010, Darko Miletic ' +__copyright__ = '2010 - 2011, Darko Miletic ' ''' news.bbc.co.uk ''' -import re from calibre.web.feeds.recipes import BasicNewsRecipe class BBC(BasicNewsRecipe): title = 'BBC News (fast)' __author__ = 'Darko Miletic, Starson17' - description = 'News from UK. A much faster version that does not download pictures' + description = 'Visit BBC News for up-to-the-minute news, breaking news, video, audio and feature stories. BBC News provides trusted World and UK news as well as local and regional perspectives. Also entertainment, business, science, technology and health news.' oldest_article = 2 max_articles_per_feed = 100 no_stylesheets = True - #delay = 1 use_embedded_content = False encoding = 'utf8' publisher = 'BBC' category = 'news, UK, world' language = 'en_GB' publication_type = 'newsportal' - extra_css = ' body{ font-family: Verdana,Helvetica,Arial,sans-serif } .introduction{font-weight: bold} .story-feature{display: block; padding: 0; border: 1px solid; width: 40%; font-size: small} .story-feature h2{text-align: center; text-transform: uppercase} ' - preprocess_regexps = [(re.compile(r'', re.DOTALL), lambda m: '')] + masthead_url = 'http://news.bbcimg.co.uk/img/1_0_1/cream/hi/news/news-blocks.gif' + extra_css = """ + body{ font-family: Verdana,Helvetica,Arial,sans-serif } + .introduction{font-weight: bold} + .story-feature{display: block; padding: 0; border: 1px solid; width: 40%; font-size: small} + .story-feature h2{text-align: center; text-transform: uppercase} + """ conversion_options = { 'comments' : description ,'tags' : category @@ -31,31 +34,54 @@ class BBC(BasicNewsRecipe): } keep_only_tags = [ - dict(name='div', attrs={'class':['layout-block-a layout-block']}) - ,dict(attrs={'class':['story-body','storybody']}) + dict(name='div', attrs={'class':['layout-block-a layout-block']}) + ,dict(attrs={'class':['story-body','storybody']}) + ,dict(attrs={'id':['meta-information','story-body']}) ] remove_tags = [ - dict(name='div', attrs={'class':['story-feature related narrow', 'share-help', 'embedded-hyper', \ - 'story-feature wide ', 'story-feature narrow']}) - , dict(name=['img']) - ] + dict(name='div', attrs={'class':['story-feature related narrow', \ + 'share-help', 'embedded-hyper', \ + 'story-feature wide ', \ + 'story-feature narrow', \ + 'hidden','story-actions', \ + 'embedded-hyper']}) + ,dict(name=['img','meta','link','object','embed','iframe','base']) + ,dict(attrs={'class':['hidden','videoInStoryC']}) + ,dict(attrs={'id':['bbccom_sponsor_section','toggle-controls', \ + 'toggle-images','toggle-title']}) + ] - remove_attributes = ['width','height'] + remove_attributes = ['width','height','xmlns:og','lang','clear'] feeds = [ - ('News Front Page', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml'), - ('Science/Nature', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/science/nature/rss.xml'), - ('Technology', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/technology/rss.xml'), - ('Entertainment', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/entertainment/rss.xml'), - ('Magazine', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/uk_news/magazine/rss.xml'), - ('Business', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/business/rss.xml'), - ('Health', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/health/rss.xml'), - ('Americas', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml'), - ('Europe', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/europe/rss.xml'), - ('South Asia', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/south_asia/rss.xml'), - ('UK', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/uk_news/rss.xml'), - ('Asia-Pacific', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/asia-pacific/rss.xml'), - ('Africa', 'http://newsrss.bbc.co.uk/rss/newsonline_world_edition/africa/rss.xml'), + ('Top Stories' , 'http://feeds.bbci.co.uk/news/rss.xml' ), + ('Science/Environment', 'http://feeds.bbci.co.uk/news/science_and_environment/rss.xml'), + ('Technology' , 'http://feeds.bbci.co.uk/news/technology/rss.xml' ), + ('Entertainment/Arts' , 'http://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml' ), + ('Magazine' , 'http://feeds.bbci.co.uk/news/magazine/rss.xml' ), + ('Business' , 'http://feeds.bbci.co.uk/news/business/rss.xml' ), + ('Politics' , 'http://feeds.bbci.co.uk/news/politics/rss.xml' ), + ('Health' , 'http://feeds.bbci.co.uk/news/health/rss.xml' ), + ('US&Canada' , 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml' ), + ('Latin America' , 'http://feeds.bbci.co.uk/news/world/latin_america/rss.xml' ), + ('Europe' , 'http://feeds.bbci.co.uk/news/world/europe/rss.xml' ), + ('South Asia' , 'http://feeds.bbci.co.uk/news/world/south_asia/rss.xml' ), + ('England' , 'http://feeds.bbci.co.uk/news/england/rss.xml' ), + ('Asia-Pacific' , 'http://feeds.bbci.co.uk/news/world/asia_pacific/rss.xml' ), + ('Africa' , 'http://feeds.bbci.co.uk/news/world/africa/rss.xml' ) ] + def preprocess_html(self, soup): + for item in soup.findAll(style=True): + del item['style'] + for item in soup.findAll('left'): + item.name='span' + for item in soup.findAll('a'): + if item.string is not None: + str = item.string + item.replaceWith(str) + else: + str = self.tag_to_string(item) + item.replaceWith(str) + return soup diff --git a/recipes/cnn.recipe b/recipes/cnn.recipe index 8c3cfa6de8..ccf47e26d8 100644 --- a/recipes/cnn.recipe +++ b/recipes/cnn.recipe @@ -3,71 +3,39 @@ __copyright__ = '2008, Kovid Goyal ' ''' Profile to download CNN ''' + +import re from calibre.web.feeds.news import BasicNewsRecipe -from calibre.ebooks.BeautifulSoup import BeautifulSoup class CNN(BasicNewsRecipe): title = 'CNN' description = 'Global news' timefmt = ' [%d %b %Y]' - __author__ = 'Krittika Goyal and Sujata Raman' + __author__ = 'Kovid Goyal' language = 'en' no_stylesheets = True use_embedded_content = False oldest_article = 15 - recursions = 1 - match_regexps = [r'http://sportsillustrated.cnn.com/.*/[1-9].html'] + #recursions = 1 + #match_regexps = [r'http://sportsillustrated.cnn.com/.*/[1-9].html'] max_articles_per_feed = 25 - extra_css = ''' - .cnn_strycntntlft{font-family :Arial,Helvetica,sans-serif;} - h2{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - .cnnTxtCmpnt{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - .cnnTMcontent{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757} - .storytext{font-family :Arial,Helvetica,sans-serif; font-size:small} - .storybyline{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757} - .credit{font-family :Arial,Helvetica,sans-serif; font-size:xx-small; color:#575757} - .storyBrandingBanner{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757} - .storytimestamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757} - .timestamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757} - .cnn_strytmstmp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .cnn_stryimg640caption{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .cnn_strylccimg300cntr{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .cnn_stryichgfcpt{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .cnnByline{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .cnn_bulletbin cnnStryHghLght{ font-size:xx-small;} - .subhead p{font-family :Arial,Helvetica,sans-serif; font-size:x-small;} - .cnnStoryContent{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - .cnnContentContainer{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - .col1{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;} - .col3{color:#333333; font-family :Arial,Helvetica,sans-serif; font-size:x-small;font-weight:bold;} - .cnnInlineT1Caption{font-family :Arial,Helvetica,sans-serif; font-size:x-small;font-weight:bold;} - .cnnInlineT1Credit{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#333333;} - .col10{color:#5A637E;} - .cnnInlineRailBulletList{color:black;} - .cnnLine0{font-family :Arial,Helvetica,sans-serif; color:#666666;font-weight:bold;} - .cnnTimeStamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#333333;} - .galleryhedDek{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757;} - .galleryWidgetHeader{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#004276;} - .article-content{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - .cnnRecapStory{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - h1{font-family :Arial,Helvetica,sans-serif; font-size:x-large} - .captionname{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757;} - inStoryIE{{font-family :Arial,Helvetica,sans-serif; font-size:x-small;} - ''' - - #remove_tags_before = dict(name='h1', attrs={'class':'heading'}) - #remove_tags_after = dict(name='td', attrs={'class':'newptool1'}) - remove_tags = [ - dict(name='iframe'), - dict(name='div', attrs={'class':['cnnEndOfStory', 'cnnShareThisItem', 'cnn_strylctcntr cnn_strylctcqrelt', 'cnnShareBoxContent', 'cnn_strybtmcntnt', 'cnn_strycntntrgt']}), - dict(name='div', attrs={'id':['IEContainer', 'clickIncludeBox']}), - #dict(name='ul', attrs={'class':'article-tools'}), - #dict(name='ul', attrs={'class':'articleTools'}), + preprocess_regexps = [ + (re.compile(r'', re.DOTALL), lambda m: ''), + (re.compile(r'', re.DOTALL), lambda m: ''), + (re.compile(r'', re.DOTALL), lambda m: ''), ] + keep_only_tags = [dict(id='cnnContentContainer')] + remove_tags = [ + {'class':['cnn_strybtntools', 'cnn_strylftcntnt', + 'cnn_strybtntools', 'cnn_strybtntoolsbttm', 'cnn_strybtmcntnt', + 'cnn_strycntntrgt']}, + ] + + feeds = [ ('Top News', 'http://rss.cnn.com/rss/cnn_topstories.rss'), ('World', 'http://rss.cnn.com/rss/cnn_world.rss'), @@ -84,15 +52,8 @@ class CNN(BasicNewsRecipe): ('Offbeat', 'http://rss.cnn.com/rss/cnn_offbeat.rss'), ('Most Popular', 'http://rss.cnn.com/rss/cnn_mostpopular.rss') ] - def preprocess_html(self, soup): - story = soup.find(name='div', attrs={'class':'cnnBody_Left'}) - if story is None: - story = soup.find(name='div', attrs={'id':'cnnContentContainer'}) - soup = BeautifulSoup('t') - body = soup.find(name='body') - body.insert(0, story) - else: - soup = BeautifulSoup('t') - body = soup.find(name='body') - body.insert(0, story) - return soup + + def get_article_url(self, article): + ans = BasicNewsRecipe.get_article_url(self, article) + return ans.partition('?')[0] + diff --git a/recipes/runa.recipe b/recipes/runa.recipe deleted file mode 100644 index fe30041581..0000000000 --- a/recipes/runa.recipe +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -__license__ = 'GPL v3' -__author__ = 'Mori' -__version__ = 'v. 0.1' -''' -www.runa.pl/blog -''' - -from calibre.web.feeds.news import BasicNewsRecipe -import re - -class FantazmatyRecipe(BasicNewsRecipe): - __author__ = 'Mori' - language = 'pl' - - title = u'Fantazmaty' - publisher = u'Agencja Wydawnicza Runa' - description = u'Blog Agencji Wydawniczej Runa' - - no_stylesheets = True - remove_javascript = True - encoding = 'utf-8' - - oldest_article = 100 - max_articles_per_feed = 100 - - extra_css = ''' - img{float: left; padding-right: 10px; padding-bottom: 5px;} - ''' - - feeds = [ - (u'Fantazmaty', u'http://www.runa.pl/blog/rss.xml') - ] - - remove_tags = [ - dict(name = 'div', attrs = {'class' : 'path'}), - dict(name = 'div', attrs = {'class' : 'drdot'}), - dict(name = 'div', attrs = {'class' : 'picture'}) - ] - - remove_tags_after = [ - dict(name = 'div', attrs = {'class' : 'content'}) - ] - - preprocess_regexps = [ - (re.compile(i[0], re.IGNORECASE | re.DOTALL), i[1]) for i in - [ - (r'.*?
', lambda match: '') - ] - ] \ No newline at end of file diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 5cde30f72e..9ebec5e7e8 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1227,6 +1227,15 @@ class StoreEHarlequinStore(StoreBase): formats = ['EPUB', 'PDF'] affiliate = True +class StoreEpubBudStore(StoreBase): + name = 'ePub Bud' + description = 'Well, it\'s pretty much just "YouTube for Children\'s eBooks. A not-for-profit organization devoted to brining self published childrens books to the world.' + actual_plugin = 'calibre.gui2.store.epubbud_plugin:EpubBudStore' + + drm_free_only = True + headquarters = 'US' + formats = ['EPUB'] + class StoreFeedbooksStore(StoreBase): name = 'Feedbooks' description = u'Feedbooks is a cloud publishing and distribution service, connected to a large ecosystem of reading systems and social networks. Provides a variety of genres from independent and classic books.' @@ -1422,6 +1431,7 @@ plugins += [ StoreEBookShoppeUKStore, StoreEPubBuyDEStore, StoreEHarlequinStore, + StoreEpubBudStore, StoreFeedbooksStore, StoreFoylesUKStore, StoreGandalfStore, diff --git a/src/calibre/devices/android/driver.py b/src/calibre/devices/android/driver.py index 1cdf394c24..b557ac3526 100644 --- a/src/calibre/devices/android/driver.py +++ b/src/calibre/devices/android/driver.py @@ -52,6 +52,7 @@ class ANDROID(USBMS): 0x04e8 : { 0x681d : [0x0222, 0x0223, 0x0224, 0x0400], 0x681c : [0x0222, 0x0224, 0x0400], 0x6640 : [0x0100], + 0x685e : [0x0400], 0x6877 : [0x0400], }, @@ -109,10 +110,12 @@ class ANDROID(USBMS): 'SGH-T849', '_MB300', 'A70S', 'S_ANDROID', 'A101IT', 'A70H', 'IDEOS_TABLET', 'MYTOUCH_4G', 'UMS_COMPOSITE', 'SCH-I800_CARD', '7', 'A956', 'A955', 'A43', 'ANDROID_PLATFORM', 'TEGRA_2', - 'MB860', 'MULTI-CARD', 'MID7015A', 'INCREDIBLE', 'A7EB', 'STREAK'] + 'MB860', 'MULTI-CARD', 'MID7015A', 'INCREDIBLE', 'A7EB', 'STREAK', + 'MB525'] WINDOWS_CARD_A_MEM = ['ANDROID_PHONE', 'GT-I9000_CARD', 'SGH-I897', 'FILE-STOR_GADGET', 'SGH-T959', 'SAMSUNG_ANDROID', 'GT-P1000_CARD', - 'A70S', 'A101IT', '7', 'INCREDIBLE', 'A7EB', 'SGH-T849_CARD'] + 'A70S', 'A101IT', '7', 'INCREDIBLE', 'A7EB', 'SGH-T849_CARD', + '__UMS_COMPOSITE'] OSX_MAIN_MEM = 'Android Device Main Memory' diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index 42949215b2..d15f4070a7 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -2743,7 +2743,6 @@ class ITUNES(DriverBase): # Update metadata from plugboard # If self.plugboard is None (no transforms), original metadata is returned intact metadata_x = self._xform_metadata_via_plugboard(metadata, this_book.format) - self.log("metadata.title_sort: %s metadata_x.title_sort: %s" % (metadata.title_sort, metadata_x.title_sort)) if isosx: if lb_added: lb_added.name.set(metadata_x.title) @@ -3024,6 +3023,8 @@ class ITUNES_ASYNC(ITUNES): pythoncom.CoInitialize() self._launch_iTunes() except: + import traceback + traceback.print_exc() raise UserFeedback('unable to launch iTunes', details=None, level=UserFeedback.WARN) finally: pythoncom.CoUninitialize() diff --git a/src/calibre/devices/eb600/driver.py b/src/calibre/devices/eb600/driver.py index dbccd72ee9..ca7e0ce373 100644 --- a/src/calibre/devices/eb600/driver.py +++ b/src/calibre/devices/eb600/driver.py @@ -35,8 +35,8 @@ class EB600(USBMS): PRODUCT_ID = [0x1688] BCD = [0x110] - VENDOR_NAME = 'NETRONIX' - WINDOWS_MAIN_MEM = 'EBOOK' + VENDOR_NAME = ['NETRONIX', 'WOLDER'] + WINDOWS_MAIN_MEM = ['EBOOK', 'MIBUK_GAMMA_6.2'] WINDOWS_CARD_A_MEM = 'EBOOK' OSX_MAIN_MEM = 'EB600 Internal Storage Media' diff --git a/src/calibre/devices/nook/driver.py b/src/calibre/devices/nook/driver.py index 3c30b88568..aaa68891ba 100644 --- a/src/calibre/devices/nook/driver.py +++ b/src/calibre/devices/nook/driver.py @@ -115,5 +115,6 @@ class NOOK_TSR(NOOK): BCD = [0x216] EBOOK_DIR_MAIN = EBOOK_DIR_CARD_A = 'My Files/Books' + WINDOWS_MAIN_MEM = WINDOWS_CARD_A_MEM = 'EBOOK_DISK' diff --git a/src/calibre/ebooks/epub/fix/container.py b/src/calibre/ebooks/epub/fix/container.py index 1669290a7b..691bf7132a 100644 --- a/src/calibre/ebooks/epub/fix/container.py +++ b/src/calibre/ebooks/epub/fix/container.py @@ -101,7 +101,7 @@ class Container(object): return None return existing[0] - def add_name_to_manifest(self, name): + def add_name_to_manifest(self, name, mt=None): item = self.manifest_item_for_name(name) if item is not None: return @@ -109,11 +109,27 @@ class Container(object): item = manifest.makeelement('{%s}item'%OPF_NS, nsmap={'opf':OPF_NS}, href=self.name_to_href(name, posixpath.dirname(self.opf_name)), id=self.generate_manifest_id()) - mt = guess_type(posixpath.basename(name))[0] + if not mt: + mt = guess_type(posixpath.basename(name))[0] if not mt: mt = 'application/octest-stream' item.set('media-type', mt) manifest.append(item) + self.fix_tail(item) + + def fix_tail(self, item): + ''' + Designed only to work with self closing elements after item has + just been inserted/appended + ''' + parent = item.getparent() + idx = parent.index(item) + if idx == 0: + item.tail = parent.text + else: + item.tail = parent[idx-1].tail + if idx == len(parent)-1: + parent[idx-1].tail = parent.text def generate_manifest_id(self): items = self.opf.xpath('//opf:manifest/opf:item[@id]', diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 69407dcb2e..378d4ab5f0 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -44,12 +44,20 @@ class SafeFormat(TemplateFormatter): def get_value(self, orig_key, args, kwargs): if not orig_key: return '' - key = orig_key.lower() + orig_key = orig_key.lower() + key = orig_key if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS: key = field_metadata.search_term_to_field_key(key) - if key is None or (self.book and key not in self.book.all_field_keys()): - raise ValueError(_('Value: unknown field ') + orig_key) - b = self.book.get_user_metadata(key, False) + if key is None or (self.book and + key not in self.book.all_field_keys()): + if hasattr(self.book, orig_key): + key = orig_key + else: + raise ValueError(_('Value: unknown field ') + orig_key) + try: + b = self.book.get_user_metadata(key, False) + except: + b = None if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0: v = '' elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0: diff --git a/src/calibre/ebooks/metadata/sources/douban.py b/src/calibre/ebooks/metadata/sources/douban.py index 70bf01a00e..8f8f5b80c4 100644 --- a/src/calibre/ebooks/metadata/sources/douban.py +++ b/src/calibre/ebooks/metadata/sources/douban.py @@ -46,6 +46,8 @@ cover_url = XPath("descendant::atom:link[@rel='image']/attribute::href") def get_details(browser, url, timeout): # {{{ try: + if Douban.DOUBAN_API_KEY and Douban.DOUBAN_API_KEY != '': + url = url + "?apikey=" + Douban.DOUBAN_API_KEY raw = browser.open_novisit(url, timeout=timeout).read() except Exception as e: gc = getattr(e, 'getcode', lambda : -1) diff --git a/src/calibre/ebooks/metadata/sources/identify.py b/src/calibre/ebooks/metadata/sources/identify.py index 0cc070c3c6..303bb2db6e 100644 --- a/src/calibre/ebooks/metadata/sources/identify.py +++ b/src/calibre/ebooks/metadata/sources/identify.py @@ -408,6 +408,8 @@ def identify(log, abort, # {{{ for f in plugin.prefs['ignore_fields']: if ':' not in f: setattr(result, f, getattr(dummy, f)) + if f == 'series': + result.series_index = dummy.series_index result.relevance_in_source = i result.has_cached_cover_url = (plugin.cached_cover_url_is_reliable and plugin.get_cached_cover_url(result.identifiers) is not diff --git a/src/calibre/ebooks/oeb/iterator.py b/src/calibre/ebooks/oeb/iterator.py index 299c77af10..92a4febb6c 100644 --- a/src/calibre/ebooks/oeb/iterator.py +++ b/src/calibre/ebooks/oeb/iterator.py @@ -18,7 +18,7 @@ from calibre.ebooks.chardet import xml_to_unicode from calibre.utils.zipfile import safe_replace from calibre.utils.config import DynamicConfig from calibre.utils.logging import Log -from calibre import guess_type, prints +from calibre import guess_type, prints, prepare_string_for_xml from calibre.ebooks.oeb.transforms.cover import CoverManager TITLEPAGE = CoverManager.SVG_TEMPLATE.decode('utf-8').replace(\ @@ -229,8 +229,8 @@ class EbookIterator(object): cover = self.opf.cover if self.ebook_ext in ('lit', 'mobi', 'prc', 'opf', 'fb2') and cover: cfile = os.path.join(self.base, 'calibre_iterator_cover.html') - chtml = (TITLEPAGE%os.path.relpath(cover, self.base).replace(os.sep, - '/')).encode('utf-8') + rcpath = os.path.relpath(cover, self.base).replace(os.sep, '/') + chtml = (TITLEPAGE%prepare_string_for_xml(rcpath, True)).encode('utf-8') open(cfile, 'wb').write(chtml) self.spine[0:0] = [SpineItem(cfile, mime_type='application/xhtml+xml')] diff --git a/src/calibre/ebooks/oeb/transforms/split.py b/src/calibre/ebooks/oeb/transforms/split.py index 69de740ddc..0c9b492855 100644 --- a/src/calibre/ebooks/oeb/transforms/split.py +++ b/src/calibre/ebooks/oeb/transforms/split.py @@ -120,7 +120,19 @@ class Split(object): for i, x in enumerate(page_breaks): x.set('id', x.get('id', 'calibre_pb_%d'%i)) id = x.get('id') - page_breaks_.append((XPath('//*[@id=%r]'%id), + try: + xp = XPath('//*[@id="%s"]'%id) + except: + try: + xp = XPath("//*[@id='%s']"%id) + except: + # The id has both a quote and an apostrophe or some other + # Just replace it since I doubt its going to work anywhere else + # either + id = 'calibre_pb_%d'%i + x.set('id', id) + xp = XPath('//*[@id=%r]'%id) + page_breaks_.append((xp, x.get('pb_before', False))) page_break_ids.append(id) diff --git a/src/calibre/gui2/actions/convert.py b/src/calibre/gui2/actions/convert.py index ed0a064e88..17fa0ad622 100644 --- a/src/calibre/gui2/actions/convert.py +++ b/src/calibre/gui2/actions/convert.py @@ -171,10 +171,9 @@ class ConvertAction(InterfaceAction): raise Exception(_('Empty output file, ' 'probably the conversion process crashed')) - data = open(temp_files[-1].name, 'rb') - self.gui.library_view.model().db.add_format(book_id, \ + with open(temp_files[-1].name, 'rb') as data: + self.gui.library_view.model().db.add_format(book_id, \ fmt, data, index_is_id=True) - data.close() self.gui.status_bar.show_message(job.description + \ (' completed'), 2000) finally: diff --git a/src/calibre/gui2/actions/delete.py b/src/calibre/gui2/actions/delete.py index 718f0737b3..43465512e0 100644 --- a/src/calibre/gui2/actions/delete.py +++ b/src/calibre/gui2/actions/delete.py @@ -152,7 +152,8 @@ class DeleteAction(InterfaceAction): if not ids: return fmts = self._get_selected_formats( - '

'+_('Choose formats not to be deleted'), ids) + '

'+_('Choose formats not to be deleted.

Note that ' + 'this will never remove all formats from a book.'), ids) if fmts is None: return for id in ids: @@ -161,9 +162,12 @@ class DeleteAction(InterfaceAction): continue bfmts = set([x.lower() for x in bfmts.split(',')]) rfmts = bfmts - set(fmts) - for fmt in rfmts: - self.gui.library_view.model().db.remove_format(id, fmt, - index_is_id=True, notify=False) + if bfmts - rfmts: + # Do not delete if it will leave the book with no + # formats + for fmt in rfmts: + self.gui.library_view.model().db.remove_format(id, fmt, + index_is_id=True, notify=False) self.gui.library_view.model().refresh_ids(ids) self.gui.library_view.model().current_changed(self.gui.library_view.currentIndex(), self.gui.library_view.currentIndex()) diff --git a/src/calibre/gui2/actions/edit_metadata.py b/src/calibre/gui2/actions/edit_metadata.py index ac475cb027..384bf4c9be 100644 --- a/src/calibre/gui2/actions/edit_metadata.py +++ b/src/calibre/gui2/actions/edit_metadata.py @@ -482,6 +482,8 @@ class EditMetadataAction(InterfaceAction): if mi.identifiers: idents.update(mi.identifiers) mi.identifiers = idents + if mi.is_null('series'): + mi.series_index = None db.set_metadata(i, mi, commit=False, set_title=set_title, set_authors=set_authors, notify=False) self.applied_ids.append(i) diff --git a/src/calibre/gui2/dialogs/select_formats.py b/src/calibre/gui2/dialogs/select_formats.py index 5934c8c0f9..aea56ad196 100644 --- a/src/calibre/gui2/dialogs/select_formats.py +++ b/src/calibre/gui2/dialogs/select_formats.py @@ -44,7 +44,7 @@ class SelectFormats(QDialog): self.setLayout(self._l) self.setWindowTitle(_('Choose formats')) self._m = QLabel(msg) - self._m.setWordWrap = True + self._m.setWordWrap(True) self._l.addWidget(self._m) self.formats = Formats(fmt_list) self.fview = QListView(self) diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py index 083dacbf00..852bbcc221 100644 --- a/src/calibre/gui2/dialogs/template_dialog.py +++ b/src/calibre/gui2/dialogs/template_dialog.py @@ -5,13 +5,15 @@ __license__ = 'GPL v3' import json -from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, - QRegExp, QApplication, - QTextCharFormat, QFont, QColor, QCursor) +from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, QFont, + QRegExp, QApplication, QTextCharFormat, QColor, QCursor) +from calibre.gui2 import error_dialog from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog from calibre.utils.formatter_functions import formatter_functions -from calibre.ebooks.metadata.book.base import composite_formatter +from calibre.ebooks.metadata.book.base import composite_formatter, Metadata +from calibre.library.coloring import (displayable_columns) + class ParenPosition: @@ -195,12 +197,24 @@ class TemplateHighlighter(QSyntaxHighlighter): class TemplateDialog(QDialog, Ui_TemplateDialog): - def __init__(self, parent, text, mi): + def __init__(self, parent, text, mi=None, fm=None, color_field=None): QDialog.__init__(self, parent) Ui_TemplateDialog.__init__(self) self.setupUi(self) - self.mi = mi + self.coloring = color_field is not None + if self.coloring: + cols = sorted([k for k in displayable_columns(fm)]) + self.colored_field.addItems(cols) + self.colored_field.setCurrentIndex(self.colored_field.findText(color_field)) + else: + self.colored_field.setVisible(False) + self.colored_field_label.setVisible(False) + + if mi: + self.mi = mi + else: + self.mi = Metadata(None, None) # Remove help icon on title bar icon = self.windowIcon() @@ -238,24 +252,26 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.function.setCurrentIndex(0) self.function.currentIndexChanged[str].connect(self.function_changed) self.textbox_changed() + self.rule = (None, '') def textbox_changed(self): cur_text = unicode(self.textbox.toPlainText()) if self.last_text != cur_text: self.last_text = cur_text self.highlighter.regenerate_paren_positions() + self.text_cursor_changed() self.template_value.setText( composite_formatter.safe_format(cur_text, self.mi, _('EXCEPTION: '), self.mi)) def text_cursor_changed(self): cursor = self.textbox.textCursor() - block_number = cursor.blockNumber() - pos_in_block = cursor.positionInBlock() position = cursor.position() t = unicode(self.textbox.toPlainText()) - if position < len(t): - self.highlighter.check_cursor_pos(t[position], block_number, + if position > 0 and position <= len(t): + block_number = cursor.blockNumber() + pos_in_block = cursor.positionInBlock() - 1 + self.highlighter.check_cursor_pos(t[position-1], block_number, pos_in_block) def function_changed(self, toWhat): @@ -270,3 +286,17 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): else: self.source_code.setPlainText(self.funcs[name].program_text) + def accept(self): + txt = unicode(self.textbox.toPlainText()).rstrip() + if self.coloring: + if self.colored_field.currentIndex() == -1: + error_dialog(self, _('No column chosen'), + _('You must specify a column to be colored'), show=True) + return + if not txt: + error_dialog(self, _('No template provided'), + _('The template box cannot be empty'), show=True) + return + + self.rule = (unicode(self.colored_field.currentText()), txt) + QDialog.accept(self) \ No newline at end of file diff --git a/src/calibre/gui2/dialogs/template_dialog.ui b/src/calibre/gui2/dialogs/template_dialog.ui index d36cbbd3d4..13586e7049 100644 --- a/src/calibre/gui2/dialogs/template_dialog.ui +++ b/src/calibre/gui2/dialogs/template_dialog.ui @@ -20,12 +20,30 @@ Edit Comments + + + + + + Set the color of the column: + + + colored_field + + + + + + + + + - + Template value: @@ -38,14 +56,14 @@ - + true - + Qt::Horizontal @@ -55,7 +73,7 @@ - + Function &name: @@ -65,10 +83,10 @@ - + - + &Documentation: @@ -81,7 +99,7 @@ - + Python &code: @@ -94,7 +112,7 @@ - + @@ -104,7 +122,7 @@ - + diff --git a/src/calibre/gui2/dialogs/template_line_editor.py b/src/calibre/gui2/dialogs/template_line_editor.py index b24328f0ad..02293e4df7 100644 --- a/src/calibre/gui2/dialogs/template_line_editor.py +++ b/src/calibre/gui2/dialogs/template_line_editor.py @@ -5,17 +5,10 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -from functools import partial -from collections import defaultdict -from PyQt4.Qt import (Qt, QLineEdit, QDialog, QGridLayout, QLabel, QCheckBox, - QIcon, QDialogButtonBox, QColor, QComboBox, QPushButton) +from PyQt4.Qt import QLineEdit -from calibre.ebooks.metadata.book.base import composite_formatter from calibre.gui2.dialogs.template_dialog import TemplateDialog -from calibre.gui2.complete import MultiCompleteLineEdit -from calibre.gui2 import error_dialog -from calibre.utils.icu import sort_key class TemplateLineEditor(QLineEdit): @@ -25,16 +18,11 @@ class TemplateLineEditor(QLineEdit): def __init__(self, parent): QLineEdit.__init__(self, parent) - self.tags = None self.mi = None - self.txt = None def set_mi(self, mi): self.mi = mi - def set_db(self, db): - self.db = db - def contextMenuEvent(self, event): menu = self.createStandardContextMenu() menu.addSeparator() @@ -47,398 +35,9 @@ class TemplateLineEditor(QLineEdit): def clear_field(self): self.setText('') - self.txt = None - self.setReadOnly(False) - self.setStyleSheet('TemplateLineEditor { color: black }') def open_editor(self): - if self.txt: - t = TemplateDialog(self, self.txt, self.mi) - else: - t = TemplateDialog(self, self.text(), self.mi) + t = TemplateDialog(self, self.text(), mi=self.mi) t.setWindowTitle(_('Edit template')) if t.exec_(): - self.txt = None - self.setText(t.textbox.toPlainText()) - - def enable_wizard_button(self, txt): - if not txt or txt.startswith('program:\n#tag wizard'): - return True - return False - - def setText(self, txt): - txt = unicode(txt) - if txt and txt.startswith('program:\n#tag wizard'): - self.txt = txt - self.setReadOnly(True) - QLineEdit.setText(self, '') - QLineEdit.setText(self, _('Template generated by the wizard')) - self.setStyleSheet('TemplateLineEditor { color: gray }') - else: - QLineEdit.setText(self, txt) - - def tag_wizard(self): - txt = unicode(self.text()) - if txt and not self.txt: - error_dialog(self, _('Invalid text'), - _('The text in the box was not generated by this wizard'), - show=True, show_copy_button=False) - return - d = TagWizard(self, self.db, unicode(self.txt), self.mi) - if d.exec_(): - self.setText(d.template) - - def text(self): - if self.txt: - return self.txt - return QLineEdit.text(self) - -class TagWizard(QDialog): - - def __init__(self, parent, db, txt, mi): - QDialog.__init__(self, parent) - self.setWindowTitle(_('Coloring Wizard')) - self.setWindowIcon(QIcon(I('wizard.png'))) - - self.mi = mi - - self.columns = [] - self.completion_values = defaultdict(dict) - for k in db.all_field_keys(): - m = db.metadata_for_field(k) - if m['datatype'] in ('text', 'enumeration', 'series') and \ - m['is_category'] and k not in ('identifiers'): - self.columns.append(k) - if m['is_custom']: - self.completion_values[k]['v'] = db.all_custom(m['label']) - elif k == 'tags': - self.completion_values[k]['v'] = db.all_tags() - elif k == 'formats': - self.completion_values[k]['v'] = db.all_formats() - else: - if k in ('publisher'): - ck = k + 's' - else: - ck = k - f = getattr(db, 'all_' + ck, None) - if f: - if k == 'authors': - self.completion_values[k]['v'] = [v[1].\ - replace('|', ',') for v in f()] - else: - self.completion_values[k]['v'] = [v[1] for v in f()] - - if k in self.completion_values: - if k == 'authors': - self.completion_values[k]['m'] = None - else: - self.completion_values[k]['m'] = m['is_multiple'] - - self.columns.sort(key=sort_key) - self.columns.insert(0, '') - - l = QGridLayout() - self.setLayout(l) - l.setColumnStretch(2, 10) - l.setColumnMinimumWidth(3, 300) - - h = QLabel(_('And')) - h.setToolTip('

' + - _('Set this box to indicate that the two conditions must both ' - 'be true to return the "color if value found". For example, you ' - 'can check if two tags are present, if the book has a tag ' - 'and a #read custom column is checked, or if a book has ' - 'some tag and has a particular format.')) - l.addWidget(h, 0, 0, 1, 1) - - h = QLabel(_('Column')) - h.setAlignment(Qt.AlignCenter) - l.addWidget(h, 0, 1, 1, 1) - - h = QLabel(_('Not')) - h.setToolTip('

' + - _('Set this box to indicate that the value must not match ' - 'to return the "color if value found". For example, you ' - 'can check if a tag does not exist by entering that tag ' - 'and checking this box. You can check if tags are empty by ' - 'checking this box, entering .* (period asterisk) for the text, ' - 'then checking the RE box. The .* regular expression matches ' - 'anything, so if this box is checked, it matches nothing. ' - 'This box is particularly useful when using the AND box.')) - h.setAlignment(Qt.AlignCenter) - l.addWidget(h, 0, 2, 1, 1) - - h = QLabel(_('Values (see the popup help for more information)')) - h.setAlignment(Qt.AlignCenter) - h.setToolTip('

' + - _('You can enter more than one value per box, separated by commas. ' - 'The comparison ignores letter case. Special note: you can ' - 'enter at most one author.
' - 'A value can be a regular expression. Check the box to turn ' - 'them on. When using regular expressions, note that the wizard ' - 'puts anchors (^ and $) around the expression, so you ' - 'must ensure your expression matches from the beginning ' - 'to the end of the column you are checking.
' - 'Regular expression examples:') + '

    ' + - _('
  • .* matches anything in the column. No ' - 'empty values are checked, so you don\'t need to worry about ' - 'empty strings
  • ' - '
  • A.* matches anything beginning with A
  • ' - '
  • .*mystery.* matches anything containing ' - 'the word "mystery"
  • ') + '

') - l.addWidget(h , 0, 3, 1, 1) - - c = QLabel(_('is RE')) - c.setToolTip('

' + - _('Check this box if the values box contains regular expressions') + '

') - l.addWidget(c, 0, 4, 1, 1) - - c = QLabel(_('Color if value found')) - c.setToolTip('

' + - _('At least one of the two color boxes must have a value. Leave ' - 'one color box empty if you want the template to use the next ' - 'line in this wizard. If both boxes are filled in, the rest of ' - 'the lines in this wizard will be ignored.') + '

') - l.addWidget(c, 0, 5, 1, 1) - c = QLabel(_('Color if value not found')) - c.setToolTip('

' + - _('This box is usually filled in only on the last test. If it is ' - 'filled in before the last test, then the color for value found box ' - 'must be empty or all the rest of the tests will be ignored.') + '

') - l.addWidget(c, 0, 6, 1, 1) - - self.andboxes = [] - self.notboxes = [] - self.tagboxes = [] - self.colorboxes = [] - self.nfcolorboxes = [] - self.reboxes = [] - self.colboxes = [] - self.colors = [unicode(s) for s in list(QColor.colorNames())] - self.colors.insert(0, '') - - maxlines = 10 - for i in range(1, maxlines+1): - ab = QCheckBox(self) - self.andboxes.append(ab) - if i != maxlines: - # let the last box float in space - l.addWidget(ab, i, 0, 2, 1) - ab.stateChanged.connect(partial(self.and_box_changed, line=i-1)) - else: - ab.setVisible(False) - - w = QComboBox(self) - w.addItems(self.columns) - l.addWidget(w, i, 1, 1, 1) - self.colboxes.append(w) - - nb = QCheckBox(self) - self.notboxes.append(nb) - l.addWidget(nb, i, 2, 1, 1) - - tb = MultiCompleteLineEdit(self) - tb.set_separator(', ') - self.tagboxes.append(tb) - l.addWidget(tb, i, 3, 1, 1) - w.currentIndexChanged[str].connect(partial(self.column_changed, valbox=tb)) - - w = QCheckBox(self) - self.reboxes.append(w) - l.addWidget(w, i, 4, 1, 1) - - w = QComboBox(self) - w.addItems(self.colors) - self.colorboxes.append(w) - l.addWidget(w, i, 5, 1, 1) - - w = QComboBox(self) - w.addItems(self.colors) - self.nfcolorboxes.append(w) - l.addWidget(w, i, 6, 1, 1) - - if txt: - lines = txt.split('\n')[3:] - i = 0 - for line in lines: - if line.startswith('#'): - vals = line[1:].split(':|:') - if len(vals) == 2: - t, c = vals - nc = '' - re = False - f = 'tags' - a = False - n = False - else: - t,c,f,nc,re,a,n = vals - try: - self.colboxes[i].setCurrentIndex(self.colboxes[i].findText(f)) - self.colorboxes[i].setCurrentIndex( - self.colorboxes[i].findText(c)) - self.nfcolorboxes[i].setCurrentIndex( - self.nfcolorboxes[i].findText(nc)) - self.tagboxes[i].setText(t) - self.reboxes[i].setChecked(re == '2') - self.andboxes[i].setChecked(a == '2') - self.notboxes[i].setChecked(n == '2') - i += 1 - except: - pass - - w = QLabel(_('Preview')) - l.addWidget(w, 99, 1, 1, 1) - w = self.test_box = QLineEdit(self) - w.setReadOnly(True) - l.addWidget(w, 99, 3, 1, 1) - w = QPushButton(_('Test')) - l.addWidget(w, 99, 5, 1, 1) - w.clicked.connect(self.preview) - - bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self) - l.addWidget(bb, 100, 3, 1, 2) - bb.accepted.connect(self.accepted) - bb.rejected.connect(self.reject) - self.template = '' - - def preview(self): - if not self.generate_program(): - return - t = composite_formatter.safe_format(self.template, self.mi, - _('EXCEPTION'), self.mi) - self.test_box.setText(t) - - def column_changed(self, s, valbox=None): - k = unicode(s) - if k in self.completion_values: - valbox.update_items_cache(self.completion_values[k]['v']) - if self.completion_values[k]['m']: - valbox.set_separator(', ') - else: - valbox.set_separator(None) - else: - valbox.update_items_cache([]) - valbox.set_separator(None) - - def generate_program(self): - res = ("program:\n#tag wizard -- do not directly edit\n" - " first_non_empty(\n") - lines = [] - was_and = False - had_line = False - - line = 0 - for tb, cb, fb, nfcb, reb, ab, nb in zip( - self.tagboxes, self.colorboxes, self.colboxes, - self.nfcolorboxes, self.reboxes, self.andboxes, self.notboxes): - f = unicode(fb.currentText()) - if not f: - continue - m = self.completion_values[f]['m'] - c = unicode(cb.currentText()).strip() - nfc = unicode(nfcb.currentText()).strip() - re = reb.checkState() - a = ab.checkState() - n = nb.checkState() - line += 1 - - if n == 2: - tval = '' - fval = '1' - else: - tval = '1' - fval = '' - - if m: - tags = [t.strip() for t in unicode(tb.text()).split(',') if t.strip()] - if re == 2: - tags = '$|^'.join(tags) - else: - tags = ','.join(tags) - else: - tags = unicode(tb.text()).strip() - if f == 'authors': - tags.replace(',', '|') - - if (tags or f) and not (tags and f and (a == 2 or c)): - error_dialog(self, _('Invalid line'), - _('Line number {0} is not valid').format(line), - show=True, show_copy_button=False) - return False - - if c not in self.colors: - error_dialog(self, _('Invalid color'), - _('The color {0} is not valid').format(c), - show=True, show_copy_button=False) - return False - if nfc not in self.colors: - error_dialog(self, _('Invalid color'), - _('The color {0} is not valid').format(nfc), - show=True, show_copy_button=False) - return False - - if not was_and: - if had_line: - lines[-1] += ',' - had_line = True - lines.append(" test(and(") - else: - lines[-1] += ',' - - if re == 2: - if m: - lines.append(" in_list(field('{1}'), ',', '^{0}$', '{2}', '{3}')".\ - format(tags, f, tval, fval)) - else: - lines.append(" contains(field('{1}'), '{0}', '{2}', '{3}')".\ - format(tags, f, tval, fval)) - else: - if m: - lines.append(" str_in_list(field('{1}'), ',', '{0}', '{2}', '{3}')".\ - format(tags, f, tval, fval)) - else: - lines.append(" strcmp(field('{1}'), '{0}', '{3}', '{2}', '{3}')".\ - format(tags, f, tval, fval)) - if a == 2: - was_and = True - else: - was_and = False - lines.append(" ), '{0}', '{1}')".format(c, nfc)) - - res += '\n'.join(lines) - res += ')\n' - self.template = res - res = '' - for tb, cb, fb, nfcb, reb, ab, nb in zip( - self.tagboxes, self.colorboxes, self.colboxes, - self.nfcolorboxes, self.reboxes, self.andboxes, self.notboxes): - t = unicode(tb.text()).strip() - if t.endswith(','): - t = t[:-1] - c = unicode(cb.currentText()).strip() - f = unicode(fb.currentText()) - nfc = unicode(nfcb.currentText()).strip() - re = unicode(reb.checkState()) - a = unicode(ab.checkState()) - n = unicode(nb.checkState()) - if f and t and (a == '2' or c): - res += '#' + t + ':|:' + c + ':|:' + f + ':|:' + \ - nfc + ':|:' + re + ':|:' + a + ':|:' + n + '\n' - self.template += res - return True - - def and_box_changed(self, state, line=None): - if state == 2: - self.colorboxes[line].setCurrentIndex(0) - self.colorboxes[line].setEnabled(False) - self.nfcolorboxes[line].setCurrentIndex(0) - self.nfcolorboxes[line].setEnabled(False) - else: - self.colorboxes[line].setEnabled(True) - self.nfcolorboxes[line].setEnabled(True) - - def accepted(self): - if self.generate_program(): - self.accept() - else: - self.template = '' + self.setText(t.rule[1]) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 94c3deb403..b97cb3074a 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -426,7 +426,7 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{ editor.textbox.setTabStopWidth(20) d = editor.exec_() if d: - m.setData(index, QVariant(editor.textbox.toPlainText()), Qt.EditRole) + m.setData(index, QVariant(editor.rule[1]), Qt.EditRole) return None def setModelData(self, editor, model, index): diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 5e703a524f..72c8e0629f 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -99,8 +99,7 @@ class BooksModel(QAbstractTableModel): # {{{ self.ids_to_highlight_set = set() self.current_highlighted_idx = None self.highlight_only = False - self.column_color_map = {} - self.colors = [unicode(c) for c in QColor.colorNames()] + self.colors = frozenset([unicode(c) for c in QColor.colorNames()]) self.read_config() def change_alignment(self, colname, alignment): @@ -156,7 +155,6 @@ class BooksModel(QAbstractTableModel): # {{{ self.headers[col] = self.custom_columns[col]['name'] self.build_data_convertors() - self.set_color_templates(reset=False) self.reset() self.database_changed.emit(db) self.stop_metadata_backup() @@ -545,16 +543,6 @@ class BooksModel(QAbstractTableModel): # {{{ img = self.default_image return img - def set_color_templates(self, reset=True): - self.column_color_map = {} - for i in range(1,self.db.column_color_count+1): - name = self.db.prefs.get('column_color_name_'+str(i)) - if name: - self.column_color_map[name] = \ - self.db.prefs.get('column_color_template_'+str(i)) - if reset: - self.reset() - def build_data_convertors(self): def authors(r, idx=-1): au = self.db.data[r][idx] @@ -726,13 +714,16 @@ class BooksModel(QAbstractTableModel): # {{{ return QVariant(QColor('lightgreen')) elif role == Qt.ForegroundRole: key = self.column_map[col] - if key in self.column_color_map: + mi = None + for k, fmt in self.db.prefs['column_color_rules']: + if k != key: + continue id_ = self.id(index) if id_ in self.color_cache: if key in self.color_cache[id_]: return self.color_cache[id_][key] - mi = self.db.get_metadata(self.id(index), index_is_id=True) - fmt = self.column_color_map[key] + if mi is None: + mi = self.db.get_metadata(id_, index_is_id=True) try: color = composite_formatter.safe_format(fmt, mi, '', mi) if color in self.colors: @@ -742,8 +733,8 @@ class BooksModel(QAbstractTableModel): # {{{ self.color_cache[id_][key] = color return color except: - return NONE - elif self.is_custom_column(key) and \ + continue + if self.is_custom_column(key) and \ self.custom_columns[key]['datatype'] == 'enumeration': cc = self.custom_columns[self.column_map[col]]['display'] colors = cc.get('enum_colors', []) diff --git a/src/calibre/gui2/metadata/single_download.py b/src/calibre/gui2/metadata/single_download.py index cc89ef2259..013ab42684 100644 --- a/src/calibre/gui2/metadata/single_download.py +++ b/src/calibre/gui2/metadata/single_download.py @@ -35,8 +35,9 @@ from calibre import force_unicode class RichTextDelegate(QStyledItemDelegate): # {{{ - def __init__(self, parent=None): + def __init__(self, parent=None, max_width=160): QStyledItemDelegate.__init__(self, parent) + self.max_width = max_width def to_doc(self, index): doc = QTextDocument() @@ -46,8 +47,8 @@ class RichTextDelegate(QStyledItemDelegate): # {{{ def sizeHint(self, option, index): doc = self.to_doc(index) ans = doc.size().toSize() - if ans.width() > 150: - ans.setWidth(160) + if ans.width() > self.max_width - 10: + ans.setWidth(self.max_width) ans.setHeight(ans.height()+10) return ans diff --git a/src/calibre/gui2/preferences/coloring.py b/src/calibre/gui2/preferences/coloring.py new file mode 100644 index 0000000000..695adabed8 --- /dev/null +++ b/src/calibre/gui2/preferences/coloring.py @@ -0,0 +1,614 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2011, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +from PyQt4.Qt import (QWidget, QDialog, QLabel, QGridLayout, QComboBox, QSize, + QLineEdit, QIntValidator, QDoubleValidator, QFrame, QColor, Qt, QIcon, + QScrollArea, QPushButton, QVBoxLayout, QDialogButtonBox, QToolButton, + QListView, QAbstractListModel, pyqtSignal, QSizePolicy, QSpacerItem) + +from calibre.utils.icu import sort_key +from calibre.gui2 import error_dialog +from calibre.gui2.dialogs.template_dialog import TemplateDialog +from calibre.gui2.metadata.single_download import RichTextDelegate +from calibre.library.coloring import (Rule, conditionable_columns, + displayable_columns, rule_from_template) + +class ConditionEditor(QWidget): # {{{ + + def __init__(self, fm, parent=None): + QWidget.__init__(self, parent) + self.fm = fm + + self.action_map = { + 'bool' : ( + (_('is true'), 'is true',), + (_('is false'), 'is false'), + (_('is undefined'), 'is undefined') + ), + 'ondevice' : ( + (_('is true'), 'is set',), + (_('is false'), 'is not set'), + ), + 'identifiers' : ( + (_('has id'), 'has id'), + (_('does not have id'), 'does not have id'), + ), + 'int' : ( + (_('is equal to'), 'eq'), + (_('is less than'), 'lt'), + (_('is greater than'), 'gt') + ), + 'multiple' : ( + (_('has'), 'has'), + (_('does not have'), 'does not have'), + (_('has pattern'), 'has pattern'), + (_('does not have pattern'), 'does not have pattern'), + (_('is set'), 'is set'), + (_('is not set'), 'is not set'), + ), + 'single' : ( + (_('is'), 'is'), + (_('is not'), 'is not'), + (_('matches pattern'), 'matches pattern'), + (_('does not match pattern'), 'does not match pattern'), + (_('is set'), 'is set'), + (_('is not set'), 'is not set'), + ), + } + + for x in ('float', 'rating', 'datetime'): + self.action_map[x] = self.action_map['int'] + + self.l = l = QGridLayout(self) + self.setLayout(l) + + self.l1 = l1 = QLabel(_('If the ')) + l.addWidget(l1, 0, 0) + + self.column_box = QComboBox(self) + l.addWidget(self.column_box, 0, 1) + + self.l2 = l2 = QLabel(_(' column ')) + l.addWidget(l2, 0, 2) + + self.action_box = QComboBox(self) + l.addWidget(self.action_box, 0, 3) + + self.l3 = l3 = QLabel(_(' value ')) + l.addWidget(l3, 0, 4) + + self.value_box = QLineEdit(self) + l.addWidget(self.value_box, 0, 5) + + self.column_box.addItem('', '') + for key in sorted( + conditionable_columns(fm), + key=sort_key): + self.column_box.addItem(key, key) + self.column_box.setCurrentIndex(0) + + self.column_box.currentIndexChanged.connect(self.init_action_box) + self.action_box.currentIndexChanged.connect(self.init_value_box) + + for b in (self.column_box, self.action_box): + b.setSizeAdjustPolicy(b.AdjustToMinimumContentsLengthWithIcon) + b.setMinimumContentsLength(15) + + @dynamic_property + def current_col(self): + def fget(self): + idx = self.column_box.currentIndex() + return unicode(self.column_box.itemData(idx).toString()) + def fset(self, val): + for idx in range(self.column_box.count()): + c = unicode(self.column_box.itemData(idx).toString()) + if c == val: + self.column_box.setCurrentIndex(idx) + return + raise ValueError('Column %r not found'%val) + return property(fget=fget, fset=fset) + + @dynamic_property + def current_action(self): + def fget(self): + idx = self.action_box.currentIndex() + return unicode(self.action_box.itemData(idx).toString()) + def fset(self, val): + for idx in range(self.action_box.count()): + c = unicode(self.action_box.itemData(idx).toString()) + if c == val: + self.action_box.setCurrentIndex(idx) + return + raise ValueError('Action %r not valid for current column'%val) + return property(fget=fget, fset=fset) + + @property + def current_val(self): + return unicode(self.value_box.text()).strip() + + @dynamic_property + def condition(self): + + def fget(self): + c, a, v = (self.current_col, self.current_action, + self.current_val) + if not c or not a: + return None + return (c, a, v) + + def fset(self, condition): + c, a, v = condition + if not v: + v = '' + v = v.strip() + self.current_col = c + self.current_action = a + self.value_box.setText(v) + + return property(fget=fget, fset=fset) + + def init_action_box(self): + self.action_box.blockSignals(True) + self.action_box.clear() + self.action_box.addItem('', '') + col = self.current_col + m = self.fm[col] + dt = m['datatype'] + if dt in self.action_map: + actions = self.action_map[dt] + else: + if col == 'ondevice': + k = 'ondevice' + elif col == 'identifiers': + k = 'identifiers' + else: + k = 'multiple' if m['is_multiple'] else 'single' + actions = self.action_map[k] + + for text, key in actions: + self.action_box.addItem(text, key) + self.action_box.setCurrentIndex(0) + self.action_box.blockSignals(False) + self.init_value_box() + + def init_value_box(self): + self.value_box.setEnabled(True) + self.value_box.setText('') + self.value_box.setInputMask('') + self.value_box.setValidator(None) + col = self.current_col + m = self.fm[col] + dt = m['datatype'] + action = self.current_action + if not col or not action: + return + tt = '' + if col == 'identifiers': + tt = _('Enter either an identifier type or an ' + 'identifier type and value of the form identifier:value') + elif dt in ('int', 'float', 'rating'): + tt = _('Enter a number') + v = QIntValidator if dt == 'int' else QDoubleValidator + self.value_box.setValidator(v(self.value_box)) + elif dt == 'datetime': + self.value_box.setInputMask('9999-99-99') + tt = _('Enter a date in the format YYYY-MM-DD') + else: + tt = _('Enter a string.') + if 'pattern' in action: + tt = _('Enter a regular expression') + elif m.get('is_multiple', False): + tt += '\n' + _('You can match multiple values by separating' + ' them with %s')%m['is_multiple'] + self.value_box.setToolTip(tt) + if action in ('is set', 'is not set', 'is true', 'is false', + 'is undefined'): + self.value_box.setEnabled(False) +# }}} + +class RuleEditor(QDialog): # {{{ + + def __init__(self, fm, parent=None): + QDialog.__init__(self, parent) + self.fm = fm + + self.setWindowIcon(QIcon(I('format-fill-color.png'))) + self.setWindowTitle(_('Create/edit a column coloring rule')) + + self.l = l = QGridLayout(self) + self.setLayout(l) + + self.l1 = l1 = QLabel(_('Create a coloring rule by' + ' filling in the boxes below')) + l.addWidget(l1, 0, 0, 1, 5) + + self.f1 = QFrame(self) + self.f1.setFrameShape(QFrame.HLine) + l.addWidget(self.f1, 1, 0, 1, 5) + + self.l2 = l2 = QLabel(_('Set the color of the column:')) + l.addWidget(l2, 2, 0) + + self.column_box = QComboBox(self) + l.addWidget(self.column_box, 2, 1) + + self.l3 = l3 = QLabel(_('to')) + l.addWidget(l3, 2, 2) + + self.color_box = QComboBox(self) + l.addWidget(self.color_box, 2, 3) + l.addItem(QSpacerItem(10, 10, QSizePolicy.Expanding), 2, 4) + + self.l4 = l4 = QLabel( + _('Only if the following conditions are all satisfied:')) + l.addWidget(l4, 3, 0, 1, 5) + + self.scroll_area = sa = QScrollArea(self) + sa.setMinimumHeight(300) + sa.setMinimumWidth(950) + sa.setWidgetResizable(True) + l.addWidget(sa, 4, 0, 1, 5) + + self.add_button = b = QPushButton(QIcon(I('plus.png')), + _('Add another condition')) + l.addWidget(b, 5, 0, 1, 5) + b.clicked.connect(self.add_blank_condition) + + self.l5 = l5 = QLabel(_('You can disable a condition by' + ' blanking all of its boxes')) + l.addWidget(l5, 6, 0, 1, 5) + + self.bb = bb = QDialogButtonBox( + QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + bb.accepted.connect(self.accept) + bb.rejected.connect(self.reject) + l.addWidget(bb, 7, 0, 1, 5) + + self.conditions_widget = QWidget(self) + sa.setWidget(self.conditions_widget) + self.conditions_widget.setLayout(QVBoxLayout()) + self.conditions_widget.layout().setAlignment(Qt.AlignTop) + self.conditions = [] + + for b in (self.column_box, self.color_box): + b.setSizeAdjustPolicy(b.AdjustToMinimumContentsLengthWithIcon) + b.setMinimumContentsLength(15) + + for key in sorted( + displayable_columns(fm), + key=sort_key): + name = fm[key]['name'] + if name: + self.column_box.addItem(key, key) + self.column_box.setCurrentIndex(0) + + self.color_box.addItems(QColor.colorNames()) + self.color_box.setCurrentIndex(0) + + self.resize(self.sizeHint()) + + def add_blank_condition(self): + c = ConditionEditor(self.fm, parent=self.conditions_widget) + self.conditions.append(c) + self.conditions_widget.layout().addWidget(c) + + def apply_rule(self, col, rule): + for i in range(self.column_box.count()): + c = unicode(self.column_box.itemData(i).toString()) + if col == c: + self.column_box.setCurrentIndex(i) + break + if rule.color: + idx = self.color_box.findText(rule.color) + if idx >= 0: + self.color_box.setCurrentIndex(idx) + for c in rule.conditions: + ce = ConditionEditor(self.fm, parent=self.conditions_widget) + self.conditions.append(ce) + self.conditions_widget.layout().addWidget(ce) + try: + ce.condition = c + except: + import traceback + traceback.print_exc() + + + def accept(self): + if self.validate(): + QDialog.accept(self) + + def validate(self): + r = Rule(self.fm) + for c in self.conditions: + condition = c.condition + if condition is not None: + try: + r.add_condition(*condition) + except Exception as e: + import traceback + error_dialog(self, _('Invalid condition'), + _('One of the conditions for this rule is' + ' invalid: %s')%e, + det_msg=traceback.format_exc(), show=True) + return False + if len(r.conditions) < 1: + error_dialog(self, _('No conditions'), + _('You must specify at least one non-empty condition' + ' for this rule'), show=True) + return False + return True + + @property + def rule(self): + r = Rule(self.fm) + r.color = unicode(self.color_box.currentText()) + idx = self.column_box.currentIndex() + col = unicode(self.column_box.itemData(idx).toString()) + for c in self.conditions: + condition = c.condition + if condition is not None: + r.add_condition(*condition) + + return col, r +# }}} + +class RulesModel(QAbstractListModel): # {{{ + + def __init__(self, prefs, fm, parent=None): + QAbstractListModel.__init__(self, parent) + + self.fm = fm + rules = list(prefs['column_color_rules']) + self.rules = [] + for col, template in rules: + try: + rule = rule_from_template(self.fm, template) + except: + rule = template + self.rules.append((col, rule)) + + def rowCount(self, *args): + return len(self.rules) + + def data(self, index, role): + row = index.row() + try: + col, rule = self.rules[row] + except: + return None + + if role == Qt.DisplayRole: + return self.rule_to_html(col, rule) + if role == Qt.UserRole: + return (col, rule) + + def add_rule(self, col, rule): + self.rules.append((col, rule)) + self.reset() + return self.index(len(self.rules)-1) + + def replace_rule(self, index, col, r): + self.rules[index.row()] = (col, r) + self.dataChanged.emit(index, index) + + def remove_rule(self, index): + self.rules.remove(self.rules[index.row()]) + self.reset() + + def commit(self, prefs): + rules = [] + for col, r in self.rules: + if isinstance(r, Rule): + r = r.template + if r is not None: + rules.append((col, r)) + prefs['column_color_rules'] = rules + + def move(self, idx, delta): + row = idx.row() + delta + if row >= 0 and row < len(self.rules): + t = self.rules[row] + self.rules[row] = self.rules[row-delta] + self.rules[row-delta] = t + self.dataChanged.emit(idx, idx) + idx = self.index(row) + self.dataChanged.emit(idx, idx) + return idx + + def clear(self): + self.rules = [] + self.reset() + + def rule_to_html(self, col, rule): + if not isinstance(rule, Rule): + return _(''' +

Advanced Rule for column %s: +

%s
+ ''')%(col, rule) + conditions = [self.condition_to_html(c) for c in rule.conditions] + return _('''\ +

Set the color of %s to %s if the following + conditions are met:

+
    %s
+ ''') % (col, rule.color, ''.join(conditions)) + + def condition_to_html(self, condition): + return ( + _('
  • If the %s column %s value: %s') % + tuple(condition)) + +# }}} + +class EditRules(QWidget): # {{{ + + changed = pyqtSignal() + + def __init__(self, parent=None): + QWidget.__init__(self, parent) + + self.l = l = QGridLayout(self) + self.setLayout(l) + + self.l1 = l1 = QLabel(_( + 'You can control the color of columns in the' + ' book list by creating "rules" that tell calibre' + ' what color to use. Click the Add Rule button below' + ' to get started. You can change an existing rule by double' + ' clicking it.')) + l1.setWordWrap(True) + l.addWidget(l1, 0, 0, 1, 2) + + self.add_button = QPushButton(QIcon(I('plus.png')), _('Add Rule'), + self) + self.remove_button = QPushButton(QIcon(I('minus.png')), + _('Remove Rule'), self) + self.add_button.clicked.connect(self.add_rule) + self.remove_button.clicked.connect(self.remove_rule) + l.addWidget(self.add_button, 1, 0) + l.addWidget(self.remove_button, 1, 1) + + self.g = g = QGridLayout() + self.rules_view = QListView(self) + self.rules_view.doubleClicked.connect(self.edit_rule) + self.rules_view.setSelectionMode(self.rules_view.SingleSelection) + self.rules_view.setAlternatingRowColors(True) + self.rtfd = RichTextDelegate(parent=self.rules_view, max_width=400) + self.rules_view.setItemDelegate(self.rtfd) + g.addWidget(self.rules_view, 0, 0, 2, 1) + + self.up_button = b = QToolButton(self) + b.setIcon(QIcon(I('arrow-up.png'))) + b.setToolTip(_('Move the selected rule up')) + b.clicked.connect(self.move_up) + g.addWidget(b, 0, 1, 1, 1, Qt.AlignTop) + self.down_button = b = QToolButton(self) + b.setIcon(QIcon(I('arrow-down.png'))) + b.setToolTip(_('Move the selected rule down')) + b.clicked.connect(self.move_down) + g.addWidget(b, 1, 1, 1, 1, Qt.AlignBottom) + + l.addLayout(g, 2, 0, 1, 2) + l.setRowStretch(2, 10) + + self.add_advanced_button = b = QPushButton(QIcon(I('plus.png')), + _('Add Advanced Rule'), self) + b.clicked.connect(self.add_advanced) + l.addWidget(b, 3, 0, 1, 2) + + def initialize(self, fm, prefs, mi): + self.model = RulesModel(prefs, fm) + self.rules_view.setModel(self.model) + self.fm = fm + self.mi = mi + + def _add_rule(self, dlg): + if dlg.exec_() == dlg.Accepted: + col, r = dlg.rule + if r and col: + idx = self.model.add_rule(col, r) + self.rules_view.scrollTo(idx) + self.changed.emit() + + def add_rule(self): + d = RuleEditor(self.model.fm) + d.add_blank_condition() + self._add_rule(d) + + def add_advanced(self): + td = TemplateDialog(self, '', mi=self.mi, fm=self.fm, color_field='') + self._add_rule(td) + + def edit_rule(self, index): + try: + col, rule = self.model.data(index, Qt.UserRole) + except: + return + if isinstance(rule, Rule): + d = RuleEditor(self.model.fm) + d.apply_rule(col, rule) + else: + d = TemplateDialog(self, rule, mi=self.mi, fm=self.fm, color_field=col) + if d.exec_() == d.Accepted: + col, r = d.rule + if r is not None and col: + self.model.replace_rule(index, col, r) + self.rules_view.scrollTo(index) + self.changed.emit() + + def get_selected_row(self, txt): + sm = self.rules_view.selectionModel() + rows = list(sm.selectedRows()) + if not rows: + error_dialog(self, _('No rule selected'), + _('No rule selected for %s.')%txt, show=True) + return None + return rows[0] + + def remove_rule(self): + row = self.get_selected_row(_('removal')) + if row is not None: + self.model.remove_rule(row) + self.changed.emit() + + def move_up(self): + idx = self.rules_view.currentIndex() + if idx.isValid(): + idx = self.model.move(idx, -1) + if idx is not None: + sm = self.rules_view.selectionModel() + sm.select(idx, sm.ClearAndSelect) + self.rules_view.setCurrentIndex(idx) + self.changed.emit() + + def move_down(self): + idx = self.rules_view.currentIndex() + if idx.isValid(): + idx = self.model.move(idx, 1) + if idx is not None: + sm = self.rules_view.selectionModel() + sm.select(idx, sm.ClearAndSelect) + self.rules_view.setCurrentIndex(idx) + self.changed.emit() + + def clear(self): + self.model.clear() + self.changed.emit() + + def commit(self, prefs): + self.model.commit(prefs) + +# }}} + +if __name__ == '__main__': + from PyQt4.Qt import QApplication + app = QApplication([]) + + from calibre.library import db + + db = db() + + if False: + d = RuleEditor(db.field_metadata) + d.add_blank_condition() + d.exec_() + + col, r = d.rule + + print ('Column to be colored:', col) + print ('Template:') + print (r.template) + else: + d = EditRules() + d.resize(QSize(800, 600)) + d.initialize(db.field_metadata, db.prefs, None) + d.show() + app.exec_() + d.commit(db.prefs) + + diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index 7a8c1fb69c..a2850679f1 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -5,21 +5,19 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -from functools import partial - from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog, - QAbstractListModel, Qt, QColor, QIcon, QToolButton, QComboBox) + QAbstractListModel, Qt, QIcon) from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList from calibre.gui2.preferences.look_feel_ui import Ui_Form from calibre.gui2 import config, gprefs, qt_app -from calibre.gui2.dialogs.template_line_editor import TemplateLineEditor from calibre.utils.localization import (available_translations, get_language, get_lang) from calibre.utils.config import prefs from calibre.utils.icu import sort_key from calibre.gui2 import NONE from calibre.gui2.book_details import get_field_list +from calibre.gui2.preferences.coloring import EditRules class DisplayedFields(QAbstractListModel): # {{{ @@ -162,117 +160,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.df_up_button.clicked.connect(self.move_df_up) self.df_down_button.clicked.connect(self.move_df_down) - self.color_help_text.setText('

    ' + - _('Here you can specify coloring rules for columns shown in the ' - 'library view. Choose the column you wish to color, then ' - 'supply a template that specifies the color to use based on ' - 'the values in the column. There is a ' - '' - 'tutorial on using templates.') + - '

    ' + - _('If you want to color a field based on contents of columns, ' - 'then click the button next to an empty line to open the wizard. ' - 'It will build a template for you. You can later edit that ' - 'template with the same wizard. This is by far the easiest ' - 'way to specify a template.') + - '

    ' + - _('If you manually construct a template, then the template must ' - 'evaluate to a valid color name shown in the color names box.' - 'You can use any legal template expression. ' - 'For example, you can set the title to always display in ' - 'green using the template "green" (without the quotes). ' - 'To show the title in the color named in the custom column ' - '#column, use "{#column}". To show the title in blue if the ' - 'custom column #column contains the value "foo", in red if the ' - 'column contains the value "bar", otherwise in black, use ' - '

    {#column:switch(foo,blue,bar,red,black)}
    ' - 'To show the title in blue if the book has the exact tag ' - '"Science Fiction", red if the book has the exact tag ' - '"Mystery", or black if the book has neither tag, use' - "
    program: \n"
    -                  "    t = field('tags'); \n"
    -                  "    first_non_empty(\n"
    -                  "        in_list(t, ',', '^Science Fiction$', 'blue', ''), \n"
    -                  "        in_list(t, ',', '^Mystery$', 'red', 'black'))
    " - 'To show the title in green if it has one format, blue if it ' - 'two formats, and red if more, use' - "
    program:cmp(count(field('formats'),','), 2, 'green', 'blue', 'red')
    ") + - '

    ' + - _('You can access a multi-line template editor from the ' - 'context menu (right-click).') + '

    ' + - _('Note: if you want to color a "custom column with a fixed set ' - 'of values", it is often easier to specify the ' - 'colors in the column definition dialog. There you can ' - 'provide a color for each value without using a template.')+ '

    ') - self.color_help_scrollArea.setVisible(False) - self.color_help_button.clicked.connect(self.change_help_text) - self.colors_scrollArea.setVisible(False) - self.colors_label.setVisible(False) - self.colors_button.clicked.connect(self.change_colors_text) - - choices = db.field_metadata.displayable_field_keys() - choices.sort(key=sort_key) - choices.insert(0, '') - self.column_color_count = db.column_color_count+1 - - mi=None - try: - idx = gui.library_view.currentIndex().row() - mi = db.get_metadata(idx, index_is_id=False) - except: - pass - - l = self.column_color_layout - for i in range(1, self.column_color_count): - ccn = QComboBox(parent=self) - setattr(self, 'opt_column_color_name_'+str(i), ccn) - l.addWidget(ccn, i, 0, 1, 1) - - wtb = QToolButton(parent=self) - setattr(self, 'opt_column_color_wizard_'+str(i), wtb) - wtb.setIcon(QIcon(I('wizard.png'))) - l.addWidget(wtb, i, 1, 1, 1) - - ttb = QToolButton(parent=self) - setattr(self, 'opt_column_color_tpledit_'+str(i), ttb) - ttb.setIcon(QIcon(I('edit_input.png'))) - l.addWidget(ttb, i, 2, 1, 1) - - tpl = TemplateLineEditor(parent=self) - setattr(self, 'opt_column_color_template_'+str(i), tpl) - tpl.textChanged.connect(partial(self.tpl_edit_text_changed, ctrl=i)) - tpl.set_db(db) - tpl.set_mi(mi) - l.addWidget(tpl, i, 3, 1, 1) - - wtb.clicked.connect(tpl.tag_wizard) - ttb.clicked.connect(tpl.open_editor) - - r('column_color_name_'+str(i), db.prefs, choices=choices) - r('column_color_template_'+str(i), db.prefs) - txt = db.prefs.get('column_color_template_'+str(i), None) - - wtb.setEnabled(tpl.enable_wizard_button(txt)) - ttb.setEnabled(not tpl.enable_wizard_button(txt) or not txt) - - all_colors = [unicode(s) for s in list(QColor.colorNames())] - self.colors_box.setText(', '.join(all_colors)) - - def change_help_text(self): - self.color_help_scrollArea.setVisible(not self.color_help_scrollArea.isVisible()) - - def change_colors_text(self): - self.colors_scrollArea.setVisible(not self.colors_scrollArea.isVisible()) - self.colors_label.setVisible(not self.colors_label.isVisible()) - - def tpl_edit_text_changed(self, ign, ctrl=None): - tpl = getattr(self, 'opt_column_color_template_'+str(ctrl)) - txt = unicode(tpl.text()) - wtb = getattr(self, 'opt_column_color_wizard_'+str(ctrl)) - ttb = getattr(self, 'opt_column_color_tpledit_'+str(ctrl)) - wtb.setEnabled(tpl.enable_wizard_button(txt)) - ttb.setEnabled(not tpl.enable_wizard_button(txt) or not txt) - tpl.setFocus() + self.edit_rules = EditRules(self.tabWidget) + self.edit_rules.changed.connect(self.changed_signal) + self.tabWidget.addTab(self.edit_rules, + QIcon(I('format-fill-color.png')), _('Column coloring')) + self.tabWidget.setCurrentIndex(0) def initialize(self): ConfigWidgetBase.initialize(self) @@ -283,6 +175,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.current_font = self.initial_font = font self.update_font_display() self.display_model.initialize() + db = self.gui.current_db + try: + idx = self.gui.library_view.currentIndex().row() + mi = db.get_metadata(idx, index_is_id=False) + except: + mi=None + self.edit_rules.initialize(db.field_metadata, db.prefs, mi) def restore_defaults(self): ConfigWidgetBase.restore_defaults(self) @@ -292,6 +191,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.changed_signal.emit() self.update_font_display() self.display_model.restore_defaults() + self.edit_rules.clear() self.changed_signal.emit() def build_font_obj(self): @@ -341,12 +241,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.changed_signal.emit() def commit(self, *args): - for i in range(1, self.column_color_count): - col = getattr(self, 'opt_column_color_name_'+str(i)) - tpl = getattr(self, 'opt_column_color_template_'+str(i)) - if not col.currentIndex() or not unicode(tpl.text()).strip(): - col.setCurrentIndex(0) - tpl.setText('') rr = ConfigWidgetBase.commit(self, *args) if self.current_font != self.initial_font: gprefs['font'] = (self.current_font[:4] if self.current_font else @@ -356,10 +250,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): QApplication.setFont(self.font_display.font()) rr = True self.display_model.commit() + self.edit_rules.commit(self.gui.current_db.prefs) return rr def refresh_gui(self, gui): - gui.library_view.model().set_color_templates() + gui.library_view.model().reset() self.update_font_display() gui.tags_view.reread_collapse_parameters() gui.library_view.refresh_book_details() diff --git a/src/calibre/gui2/preferences/look_feel.ui b/src/calibre/gui2/preferences/look_feel.ui index def1bdd41c..cc9133a36f 100644 --- a/src/calibre/gui2/preferences/look_feel.ui +++ b/src/calibre/gui2/preferences/look_feel.ui @@ -6,7 +6,7 @@ 0 0 - 717 + 820 519 @@ -407,161 +407,6 @@ then the tags will be displayed each on their own line. - - - - :/images/format-fill-color.png:/images/format-fill-color.png - - - Column Coloring - - - - - - Column to color - - - - - - - - - Color selection template - - - - 10 - 0 - - - - - - - - The template wizard is easiest to use - - - - - - - Show/hide help text - - - - - - - Show/hide colors - - - - - - - - - Color names - - - - - - - - 16777215 - 300 - - - - true - - - - - 0 - 0 - 687 - 61 - - - - - - - true - - - Qt::AlignLeft|Qt::AlignTop - - - - - - - - - - - - 0 - 200 - - - - true - - - Qt::AlignLeft|Qt::AlignTop - - - - - 0 - 0 - 687 - 194 - - - - - - - true - - - true - - - - - - - - - - - - 0 - 10 - - - - Qt::Vertical - - - - 0 - 0 - - - - - - @@ -572,11 +417,6 @@ then the tags will be displayed each on their own line. QLineEdit
    calibre/gui2/complete.h
    - - TemplateLineEditor - QLineEdit -
    calibre/gui2/dialogs/template_line_editor.h
    -
    diff --git a/src/calibre/gui2/preferences/metadata_sources.py b/src/calibre/gui2/preferences/metadata_sources.py index f7465fb0ee..961d0dd0a4 100644 --- a/src/calibre/gui2/preferences/metadata_sources.py +++ b/src/calibre/gui2/preferences/metadata_sources.py @@ -283,7 +283,9 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.fields_model.dataChanged.connect(self.changed_signal) self.select_all_button.clicked.connect(self.fields_model.select_all) + self.select_all_button.clicked.connect(self.changed_signal) self.clear_all_button.clicked.connect(self.fields_model.clear_all) + self.clear_all_button.clicked.connect(self.changed_signal) def configure_plugin(self): for index in self.sources_view.selectionModel().selectedRows(): diff --git a/src/calibre/gui2/preferences/template_functions.py b/src/calibre/gui2/preferences/template_functions.py index fcb4c87372..7f310dace0 100644 --- a/src/calibre/gui2/preferences/template_functions.py +++ b/src/calibre/gui2/preferences/template_functions.py @@ -7,7 +7,9 @@ __docformat__ = 'restructuredtext en' import json, traceback -from calibre.gui2 import error_dialog +from PyQt4.Qt import QDialogButtonBox + +from calibre.gui2 import error_dialog, warning_dialog from calibre.gui2.preferences import ConfigWidgetBase, test_widget from calibre.gui2.preferences.template_functions_ui import Ui_Form from calibre.gui2.widgets import PythonHighlighter @@ -152,10 +154,15 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): _('Name already used'), show=True) return if self.argument_count.value() == 0: - error_dialog(self.gui, _('Template functions'), - _('Argument count must be -1 or greater than zero'), - show=True) - return + box = warning_dialog(self.gui, _('Template functions'), + _('Argument count should be -1 or greater than zero.' + 'Setting it to zero means that this function cannot ' + 'be used in single function mode.'), det_msg = '', + show=False) + box.bb.setStandardButtons(box.bb.standardButtons() | QDialogButtonBox.Cancel) + box.det_msg_toggle.setVisible(False) + if not box.exec_(): + return try: prog = unicode(self.program.toPlainText()) cls = compile_user_function(name, unicode(self.documentation.toPlainText()), diff --git a/src/calibre/gui2/store/epubbud_plugin.py b/src/calibre/gui2/store/epubbud_plugin.py new file mode 100644 index 0000000000..d6193f6ae0 --- /dev/null +++ b/src/calibre/gui2/store/epubbud_plugin.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +from __future__ import (unicode_literals, division, absolute_import, print_function) + +__license__ = 'GPL 3' +__copyright__ = '2011, John Schember ' +__docformat__ = 'restructuredtext en' + +import urllib +from contextlib import closing + +from lxml import html + +from PyQt4.Qt import QUrl + +from calibre import browser, url_slash_cleaner +from calibre.gui2 import open_url +from calibre.gui2.store import StorePlugin +from calibre.gui2.store.basic_config import BasicStoreConfig +from calibre.gui2.store.search_result import SearchResult +from calibre.gui2.store.web_store_dialog import WebStoreDialog + +class EpubBudStore(BasicStoreConfig, StorePlugin): + + def open(self, parent=None, detail_item=None, external=False): + url = 'http://epubbud.com/' + + if external or self.config.get('open_external', False): + open_url(QUrl(url_slash_cleaner(detail_item if detail_item else url))) + else: + d = WebStoreDialog(self.gui, url, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(self.config.get('tags', '')) + d.exec_() + + def search(self, query, max_results=10, timeout=60): + ''' + OPDS based search. + + We really should get the catelog from http://pragprog.com/catalog.opds + and look for the application/opensearchdescription+xml entry. + Then get the opensearch description to get the search url and + format. However, we are going to be lazy and hard code it. + ''' + url = 'http://www.epubbud.com/search.php?format=atom&q=' + urllib.quote_plus(query) + + br = browser() + + counter = max_results + with closing(br.open(url, timeout=timeout)) as f: + # Use html instead of etree as html allows us + # to ignore the namespace easily. + doc = html.fromstring(f.read()) + for data in doc.xpath('//entry'): + if counter <= 0: + break + + id = ''.join(data.xpath('.//id/text()')) + if not id: + continue + + cover_url = ''.join(data.xpath('.//link[@rel="http://opds-spec.org/thumbnail"]/@href')) + + title = u''.join(data.xpath('.//title/text()')) + author = u''.join(data.xpath('.//author/name/text()')) + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url + s.title = title.strip() + s.author = author.strip() + s.price = '$0.00' + s.detail_item = id.strip() + s.drm = SearchResult.DRM_UNLOCKED + s.formats = 'EPUB' + + yield s diff --git a/src/calibre/library/coloring.py b/src/calibre/library/coloring.py new file mode 100644 index 0000000000..c8cafcf9eb --- /dev/null +++ b/src/calibre/library/coloring.py @@ -0,0 +1,209 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import (unicode_literals, division, absolute_import, + print_function) +from future_builtins import map + +__license__ = 'GPL v3' +__copyright__ = '2011, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +import binascii, re, json +from textwrap import dedent + +class Rule(object): # {{{ + + SIGNATURE = '# BasicColorRule():' + + def __init__(self, fm, color=None): + self.color = color + self.fm = fm + self.conditions = [] + + def add_condition(self, col, action, val): + if col not in self.fm: + raise ValueError('%r is not a valid column name'%col) + v = self.validate_condition(col, action, val) + if v: + raise ValueError(v) + self.conditions.append((col, action, val)) + + def validate_condition(self, col, action, val): + m = self.fm[col] + dt = m['datatype'] + if (dt in ('int', 'float', 'rating') and action in ('lt', 'eq', 'gt')): + try: + int(val) if dt == 'int' else float(val) + except: + return '%r is not a valid numerical value'%val + + if (dt in ('comments', 'series', 'text', 'enumeration') and 'pattern' + in action): + try: + re.compile(val) + except: + return '%r is not a valid regular expression'%val + + @property + def signature(self): + args = (self.color, self.conditions) + sig = json.dumps(args, ensure_ascii=False) + return self.SIGNATURE + binascii.hexlify(sig.encode('utf-8')) + + @property + def template(self): + if not self.color or not self.conditions: + return None + conditions = map(self.apply_condition, self.conditions) + conditions = (',\n' + ' '*9).join(conditions) + return dedent('''\ + program: + {sig} + test(and( + {conditions} + ), '{color}', ''); + ''').format(sig=self.signature, conditions=conditions, + color=self.color) + + def apply_condition(self, condition): + col, action, val = condition + m = self.fm[col] + dt = m['datatype'] + + if col == 'ondevice': + return self.ondevice_condition(col, action, val) + + if col == 'identifiers': + return self.identifiers_condition(col, action, val) + + if dt == 'bool': + return self.bool_condition(col, action, val) + + if dt in ('int', 'float', 'rating'): + return self.number_condition(col, action, val) + + if dt == 'datetime': + return self.date_condition(col, action, val) + + if dt in ('comments', 'series', 'text', 'enumeration'): + ism = m.get('is_multiple', False) + if ism: + return self.multiple_condition(col, action, val, ism) + return self.text_condition(col, action, val) + + def identifiers_condition(self, col, action, val): + if action == 'has id': + return "identifier_in_list(field('identifiers'), '%s', '1', '')"%val + return "identifier_in_list(field('identifiers'), '%s', '', '1')"%val + + def ondevice_condition(self, col, action, val): + if action == 'is set': + return "test(ondevice(), '1', '')" + if action == 'is not set': + return "test(ondevice(), '', '1')" + + def bool_condition(self, col, action, val): + test = {'is true': 'True', + 'is false': 'False', + 'is undefined': 'None'}[action] + return "strcmp('%s', raw_field('%s'), '', '1', '')"%(test, col) + + def number_condition(self, col, action, val): + lt, eq, gt = { + 'eq': ('', '1', ''), + 'lt': ('1', '', ''), + 'gt': ('', '', '1') + }[action] + lt, eq, gt = '', '1', '' + return "cmp(raw_field('%s'), %s, '%s', '%s', '%s')" % (col, val, lt, eq, gt) + + def date_condition(self, col, action, val): + lt, eq, gt = { + 'eq': ('', '1', ''), + 'lt': ('1', '', ''), + 'gt': ('', '', '1') + }[action] + return "cmp(format_date(raw_field('%s'), 'yyyy-MM-dd'), %s, '%s', '%s', '%s')" % (col, + val, lt, eq, gt) + + def multiple_condition(self, col, action, val, sep): + if action == 'is set': + return "test('%s', '1', '')"%col + if action == 'is not set': + return "test('%s', '', '1')"%col + if action == 'has': + return "str_in_list(field('%s'), '%s', \"%s\", '1', '')"%(col, sep, val) + if action == 'does not have': + return "str_in_list(field('%s'), '%s', \"%s\", '', '1')"%(col, sep, val) + if action == 'has pattern': + return "in_list(field('%s'), '%s', \"%s\", '1', '')"%(col, sep, val) + if action == 'does not have pattern': + return "in_list(field('%s'), '%s', \"%s\", '', '1')"%(col, sep, val) + + def text_condition(self, col, action, val): + if action == 'is set': + return "test('%s', '1', '')"%col + if action == 'is not set': + return "test('%s', '', '1')"%col + if action == 'is': + return "strcmp(field('%s'), \"%s\", '', '1', '')"%(col, val) + if action == 'is not': + return "strcmp(field('%s'), \"%s\", '1', '', '1')"%(col, val) + if action == 'matches pattern': + return "contains(field('%s'), \"%s\", '1', '')"%(col, val) + if action == 'does not match pattern': + return "contains(field('%s'), \"%s\", '', '1')"%(col, val) + +# }}} + +def rule_from_template(fm, template): + ok_lines = [] + for line in template.splitlines(): + if line.startswith(Rule.SIGNATURE): + raw = line[len(Rule.SIGNATURE):].strip() + try: + color, conditions = json.loads(binascii.unhexlify(raw).decode('utf-8')) + except: + continue + r = Rule(fm) + r.color = color + for c in conditions: + try: + r.add_condition(*c) + except: + continue + if r.color and r.conditions: + return r + else: + ok_lines.append(line) + return '\n'.join(ok_lines) + +def conditionable_columns(fm): + for key in fm: + m = fm[key] + dt = m['datatype'] + if m.get('name', False) and dt in ('bool', 'int', 'float', 'rating', 'series', + 'comments', 'text', 'enumeration', 'datetime'): + if key == 'sort': + yield 'title_sort' + else: + yield key + +def displayable_columns(fm): + for key in fm.displayable_field_keys(): + if key not in ('sort', 'author_sort', 'comments', 'formats', + 'identifiers', 'path'): + yield key + +def migrate_old_rule(fm, template): + if template.startswith('program:\n#tag wizard'): + rules = [] + for line in template.splitlines(): + if line.startswith('#') and ':|:' in line: + value, color = line[1:].split(':|:') + r = Rule(fm, color=color) + r.add_condition('tags', 'has', value) + rules.append(r.template) + return rules + return [template] + diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index df465c919e..2106fcad8f 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en' The database used to store ebook metadata ''' import os, sys, shutil, cStringIO, glob, time, functools, traceback, re, \ - json, uuid + json, uuid, tempfile import threading, random from itertools import repeat from math import ceil @@ -211,10 +211,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): defs = self.prefs.defaults defs['gui_restriction'] = defs['cs_restriction'] = '' defs['categories_using_hierarchy'] = [] - self.column_color_count = 5 - for i in range(1,self.column_color_count+1): - defs['column_color_name_'+str(i)] = '' - defs['column_color_template_'+str(i)] = '' + defs['column_color_rules'] = [] # Migrate the bool tristate tweak defs['bools_are_tristate'] = \ @@ -222,6 +219,24 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if self.prefs.get('bools_are_tristate') is None: self.prefs.set('bools_are_tristate', defs['bools_are_tristate']) + # Migrate column coloring rules + if self.prefs.get('column_color_name_1', None) is not None: + from calibre.library.coloring import migrate_old_rule + old_rules = [] + for i in range(1, 6): + col = self.prefs.get('column_color_name_'+str(i), None) + templ = self.prefs.get('column_color_template_'+str(i), None) + if col and templ: + try: + del self.prefs['column_color_name_'+str(i)] + rules = migrate_old_rule(self.field_metadata, templ) + for templ in rules: + old_rules.append((col, templ)) + except: + pass + if old_rules: + self.prefs['column_color_rules'] += old_rules + # Migrate saved search and user categories to db preference scheme def migrate_preference(key, default): oldval = prefs[key] @@ -576,11 +591,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): f.write(cdata) for format in formats: # Get data as string (can't use file as source and target files may be the same) - f = self.format(id, format, index_is_id=True, as_file=False) - if not f: + f = self.format(id, format, index_is_id=True, as_file=True) + if f is None: continue - stream = cStringIO.StringIO(f) - self.add_format(id, format, stream, index_is_id=True, + with tempfile.SpooledTemporaryFile(max_size=100*(1024**2)) as stream: + shutil.copyfileobj(f, stream) + stream.seek(0) + self.add_format(id, format, stream, index_is_id=True, path=tpath, notify=False) self.conn.execute('UPDATE books SET path=? WHERE id=?', (path, id)) self.dirtied([id], commit=False) @@ -1556,13 +1573,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if ids is not None: count = self.conn.get('''SELECT COUNT(id) FROM data - WHERE format="%s" AND - books_list_filter(book)'''%fmt, + WHERE format=? AND + books_list_filter(book)''', (fmt,), all=False) else: count = self.conn.get('''SELECT COUNT(id) FROM data - WHERE format="%s"'''%fmt, + WHERE format=?''', (fmt,), all=False) if count > 0: categories['formats'].append(Tag(fmt, count=count, icon=icon, @@ -1584,13 +1601,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if ids is not None: count = self.conn.get('''SELECT COUNT(book) FROM identifiers - WHERE type="%s" AND - books_list_filter(book)'''%ident, + WHERE type=? AND + books_list_filter(book)''', (ident,), all=False) else: count = self.conn.get('''SELECT COUNT(id) FROM identifiers - WHERE type="%s"'''%ident, + WHERE type=?''', (ident,), all=False) if count > 0: categories['identifiers'].append(Tag(ident, count=count, icon=icon, diff --git a/src/calibre/library/field_metadata.py b/src/calibre/library/field_metadata.py index 979e98a819..c884542241 100644 --- a/src/calibre/library/field_metadata.py +++ b/src/calibre/library/field_metadata.py @@ -374,6 +374,8 @@ class FieldMetadata(dict): self.get = self._tb_cats.get def __getitem__(self, key): + if key == 'title_sort': + return self._tb_cats['sort'] return self._tb_cats[key] def __setitem__(self, key, val): @@ -390,6 +392,8 @@ class FieldMetadata(dict): return self.has_key(key) def has_key(self, key): + if key == 'title_sort': + return True return key in self._tb_cats def keys(self): diff --git a/src/calibre/manual/custom.py b/src/calibre/manual/custom.py index f5db6dd0c2..4788889972 100644 --- a/src/calibre/manual/custom.py +++ b/src/calibre/manual/custom.py @@ -240,11 +240,21 @@ def cli_docs(app): raw += '\n'+'\n'.join(lines) update_cli_doc(os.path.join('cli', cmd+'.rst'), raw, info) +def generate_docs(app): + cli_docs(app) + template_docs(app) + +def template_docs(app): + from template_ref_generate import generate_template_language_help + info = app.builder.info + raw = generate_template_language_help() + update_cli_doc('template_ref.rst', raw, info) + def setup(app): app.add_config_value('epub_cover', None, False) app.add_builder(EPUBHelpBuilder) app.connect('doctree-read', substitute) - app.connect('builder-inited', cli_docs) + app.connect('builder-inited', generate_docs) app.connect('build-finished', finished) def finished(app, exception): diff --git a/src/calibre/manual/portable.rst b/src/calibre/manual/portable.rst index 76776e3603..a9c9679512 100644 --- a/src/calibre/manual/portable.rst +++ b/src/calibre/manual/portable.rst @@ -11,6 +11,7 @@ You can "install" calibre onto a USB stick that you can take with you and use on * Run a Mobile Calibre installation with both the Calibre binaries and your ebook library resident on a USB disk or other portable media. In particular it is not necessary to have Calibre installed on the Windows PC that is to run Calibre. This batch file also does not care what drive letter is assigned when you plug in the USB device. It also will not affect any settings on the host machine being a completely self-contained Calibre installation. * Run a networked Calibre installation optimised for performance when the ebook files are located on a networked share. +If you find setting up the bat file too challenging, there is a third party portable calibre build available at `portableapps.com http://portableapps.com`_. This calibre-portable.bat file is intended for use on Windows based systems, but the principles are easily adapted for use on Linux or OS X based systems. Note that calibre requires the Microsoft Visual C++ 2008 runtimes to run. Most windows computers have them installed already, but it may be a good idea to have the installer for installing them on your USB stick. The installer is available from `Microsoft `_. @@ -73,4 +74,4 @@ Precautions Portable media can occasionally fail so you should make periodic backups of you Calibre library. This can be done by making a copy of the CalibreLibrary folder and all its contents. There are many freely available tools around that can optimise such back processes, well known ones being RoboCopy and RichCopy. However you can simply use a Windows copy facility if you cannot be bothered to use a specialised tools. -Using the environment variable CALIBRE_OVERRIDE_DATABASE_PATH disables multiple-library support in |app|. Avoid setting this variable in calibre-portable.bat unless you really need it. \ No newline at end of file +Using the environment variable CALIBRE_OVERRIDE_DATABASE_PATH disables multiple-library support in |app|. Avoid setting this variable in calibre-portable.bat unless you really need it. diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index ef44b0a5c9..079af59286 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -268,20 +268,14 @@ The following functions are available in addition to those described in single-f * ``subtract(x, y)`` -- returns x - y. Throws an exception if either x or y are not numbers. * ``template(x)`` -- evaluates x as a template. The evaluation is done in its own context, meaning that variables are not shared between the caller and the template evaluation. Because the `{` and `}` characters are special, you must use `[[` for the `{` character and `]]` for the '}' character; they are converted automatically. For example, ``template('[[title_sort]]') will evaluate the template ``{title_sort}`` and return its value. -Function classification summary: +Function classification +--------------------------- + +.. toctree:: + :maxdepth: 3 + + template_ref - * Get values from metadata: ``field``. ``raw_field``. In some situations, ``lookup`` can be used in place of ``field``. - * Arithmetic: ``add``, ``subtract``, ``multiply``, ``divide`` - * Boolean: ``and``, ``or``, ``not``. The function ``if_empty`` is similar to ``and`` called with one argument. - * If-then-else: ``contains``, ``test`` - * Iterating over values: ``first_non_empty``, ``lookup``, ``switch`` - * List lookup: ``in_list``, ``list_item``, ``select``, ``str_in_list`` - * List manipulation: ``count``, ``merge_lists``, ``sublist``, ``subitems`` - * Recursion: ``eval``, ``template`` - * Relational: ``cmp`` (for numbers), ``strcmp`` (for strings) - * String case changes: ``lowercase``, ``uppercase``, ``titlecase``, ``capitalize`` - * String manipulation: ``re``, ``shorten``, ``substr`` - * Other: ``assign``, ``booksize``, ``format_date``, ``ondevice`` ``print`` .. _general_mode: @@ -425,20 +419,9 @@ You might find the following tips useful. * Templates can use other templates by referencing a composite custom column. * 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. - -API of the Metadata objects ----------------------------- -.. module:: calibre.ebooks.metadata.book.base +.. toctree:: + :hidden: -.. autoclass:: Metadata - :members: - :member-order: bysource - -.. data:: STANDARD_METADATA_FIELDS - - The set of standard metadata fields. - -.. literalinclude:: ../ebooks/metadata/book/__init__.py - :lines: 7- + template_ref diff --git a/src/calibre/manual/template_ref_generate.py b/src/calibre/manual/template_ref_generate.py new file mode 100644 index 0000000000..742ab1fd54 --- /dev/null +++ b/src/calibre/manual/template_ref_generate.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +__license__ = 'GPL v3' +__copyright__ = '2008, Kovid Goyal ' + +from collections import defaultdict + +PREAMBLE = '''\ +.. include:: global.rst + +.. _templaterefcalibre: + +Reference for all builtin template language functions +======================================================== + +Here, we document all the builtin functions available in the |app| template language. Every function is implemented as a class in python and you can click the source links to see the source code, in case the documentation is insufficient. The functions are arranged in logical groups by type. + +.. contents:: + :depth: 2 + :local: + +.. module:: calibre.utils.formatter_functions + +''' + +CATEGORY_TEMPLATE = '''\ +{category} +{dashes} + +''' + +FUNCTION_TEMPLATE = '''\ +{fs} +{hats} + +.. autoclass:: {cn} + +''' + +POSTAMBLE = '''\ + +API of the Metadata objects +---------------------------- + +The python implementation of the template functions is passed in a Metadata object. Knowing it's API is useful if you want to define your own template functions. + +.. 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- +''' + + +def generate_template_language_help(): + from calibre.utils.formatter_functions import all_builtin_functions + + funcs = defaultdict(dict) + + for func in all_builtin_functions: + class_name = func.__class__.__name__ + func_sig = getattr(func, 'doc') + x = func_sig.find(' -- ') + if x < 0: + print 'No sig for ', class_name + continue + func_sig = func_sig[:x] + func_cat = getattr(func, 'category') + funcs[func_cat][func_sig] = class_name + + output = PREAMBLE + cats = sorted(funcs.keys()) + for cat in cats: + output += CATEGORY_TEMPLATE.format(category=cat, dashes='-'*len(cat)) + entries = [k for k in sorted(funcs[cat].keys())] + for entry in entries: + output += FUNCTION_TEMPLATE.format(fs = entry, cn=funcs[cat][entry], + hats='^'*len(entry)) + + output += POSTAMBLE + return output + +if __name__ == '__main__': + generate_template_language_help() diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index b66aec2cb9..1a8867b44e 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -57,6 +57,7 @@ class FormatterFunction(object): doc = _('No documentation provided') name = 'no name provided' + category = 'Unknown' arg_count = 0 def evaluate(self, formatter, kwargs, mi, locals, *args): @@ -87,7 +88,8 @@ class BuiltinFormatterFunction(FormatterFunction): class BuiltinStrcmp(BuiltinFormatterFunction): name = 'strcmp' arg_count = 5 - doc = _('strcmp(x, y, lt, eq, gt) -- does a case-insensitive comparison of x ' + category = 'Relational' + __doc__ = doc = _('strcmp(x, y, lt, eq, gt) -- does a case-insensitive comparison of x ' 'and y as strings. Returns lt if x < y. Returns eq if x == y. ' 'Otherwise returns gt.') @@ -101,13 +103,14 @@ class BuiltinStrcmp(BuiltinFormatterFunction): class BuiltinCmp(BuiltinFormatterFunction): name = 'cmp' + category = 'Relational' arg_count = 5 - doc = _('cmp(x, y, lt, eq, gt) -- compares x and y after converting both to ' + __doc__ = doc = _('cmp(x, y, lt, eq, gt) -- compares x and y after converting both to ' 'numbers. Returns lt if x < y. Returns eq if x == y. Otherwise returns gt.') def evaluate(self, formatter, kwargs, mi, locals, x, y, lt, eq, gt): - x = float(x if x else 0) - y = float(y if y else 0) + x = float(x if x and x != 'None' else 0) + y = float(y if y and y != 'None' else 0) if x < y: return lt if x == y: @@ -117,7 +120,8 @@ class BuiltinCmp(BuiltinFormatterFunction): class BuiltinStrcat(BuiltinFormatterFunction): name = 'strcat' arg_count = -1 - doc = _('strcat(a, b, ...) -- can take any number of arguments. Returns a ' + category = 'String Manipulation' + __doc__ = doc = _('strcat(a, b, ...) -- can take any number of arguments. Returns a ' 'string formed by concatenating all the arguments') def evaluate(self, formatter, kwargs, mi, locals, *args): @@ -130,7 +134,8 @@ class BuiltinStrcat(BuiltinFormatterFunction): class BuiltinAdd(BuiltinFormatterFunction): name = 'add' arg_count = 2 - doc = _('add(x, y) -- returns x + y. Throws an exception if either x or y are not numbers.') + category = 'Arithmetic' + __doc__ = doc = _('add(x, y) -- returns x + y. Throws an exception if either x or y are not numbers.') def evaluate(self, formatter, kwargs, mi, locals, x, y): x = float(x if x else 0) @@ -140,7 +145,8 @@ class BuiltinAdd(BuiltinFormatterFunction): class BuiltinSubtract(BuiltinFormatterFunction): name = 'subtract' arg_count = 2 - doc = _('subtract(x, y) -- returns x - y. Throws an exception if either x or y are not numbers.') + category = 'Arithmetic' + __doc__ = doc = _('subtract(x, y) -- returns x - y. Throws an exception if either x or y are not numbers.') def evaluate(self, formatter, kwargs, mi, locals, x, y): x = float(x if x else 0) @@ -150,7 +156,8 @@ class BuiltinSubtract(BuiltinFormatterFunction): class BuiltinMultiply(BuiltinFormatterFunction): name = 'multiply' arg_count = 2 - doc = _('multiply(x, y) -- returns x * y. Throws an exception if either x or y are not numbers.') + category = 'Arithmetic' + __doc__ = doc = _('multiply(x, y) -- returns x * y. Throws an exception if either x or y are not numbers.') def evaluate(self, formatter, kwargs, mi, locals, x, y): x = float(x if x else 0) @@ -160,7 +167,8 @@ class BuiltinMultiply(BuiltinFormatterFunction): class BuiltinDivide(BuiltinFormatterFunction): name = 'divide' arg_count = 2 - doc = _('divide(x, y) -- returns x / y. Throws an exception if either x or y are not numbers.') + category = 'Arithmetic' + __doc__ = doc = _('divide(x, y) -- returns x / y. Throws an exception if either x or y are not numbers.') def evaluate(self, formatter, kwargs, mi, locals, x, y): x = float(x if x else 0) @@ -170,7 +178,9 @@ class BuiltinDivide(BuiltinFormatterFunction): class BuiltinTemplate(BuiltinFormatterFunction): name = 'template' arg_count = 1 - doc = _('template(x) -- evaluates x as a template. The evaluation is done ' + category = 'Recursion' + + __doc__ = doc = _('template(x) -- evaluates x as a template. The evaluation is done ' 'in its own context, meaning that variables are not shared between ' 'the caller and the template evaluation. Because the { and } ' 'characters are special, you must use [[ for the { character and ' @@ -185,7 +195,8 @@ class BuiltinTemplate(BuiltinFormatterFunction): class BuiltinEval(BuiltinFormatterFunction): name = 'eval' arg_count = 1 - doc = _('eval(template) -- evaluates the template, passing the local ' + category = 'Recursion' + __doc__ = doc = _('eval(template) -- evaluates the template, passing the local ' 'variables (those \'assign\'ed to) instead of the book metadata. ' ' This permits using the template processor to construct complex ' 'results from local variables.') @@ -198,7 +209,8 @@ class BuiltinEval(BuiltinFormatterFunction): class BuiltinAssign(BuiltinFormatterFunction): name = 'assign' arg_count = 2 - doc = _('assign(id, val) -- assigns val to id, then returns val. ' + category = 'Other' + __doc__ = doc = _('assign(id, val) -- assigns val to id, then returns val. ' 'id must be an identifier, not an expression') def evaluate(self, formatter, kwargs, mi, locals, target, value): @@ -208,7 +220,8 @@ class BuiltinAssign(BuiltinFormatterFunction): class BuiltinPrint(BuiltinFormatterFunction): name = 'print' arg_count = -1 - doc = _('print(a, b, ...) -- prints the arguments to standard output. ' + category = 'Other' + __doc__ = doc = _('print(a, b, ...) -- prints the arguments to standard output. ' 'Unless you start calibre from the command line (calibre-debug -g), ' 'the output will go to a black hole.') @@ -219,15 +232,17 @@ class BuiltinPrint(BuiltinFormatterFunction): class BuiltinField(BuiltinFormatterFunction): name = 'field' arg_count = 1 - doc = _('field(name) -- returns the metadata field named by name') + category = 'Get values from metadata' + __doc__ = doc = _('field(name) -- returns the metadata field named by name') def evaluate(self, formatter, kwargs, mi, locals, name): return formatter.get_value(name, [], kwargs) -class BuiltinRaw_field(BuiltinFormatterFunction): +class BuiltinRawField(BuiltinFormatterFunction): name = 'raw_field' arg_count = 1 - doc = _('raw_field(name) -- returns the metadata field named by name ' + category = 'Get values from metadata' + __doc__ = doc = _('raw_field(name) -- returns the metadata field named by name ' 'without applying any formatting.') def evaluate(self, formatter, kwargs, mi, locals, name): @@ -236,7 +251,8 @@ class BuiltinRaw_field(BuiltinFormatterFunction): class BuiltinSubstr(BuiltinFormatterFunction): name = 'substr' arg_count = 3 - doc = _('substr(str, start, end) -- returns the start\'th through the end\'th ' + category = 'String Manipulation' + __doc__ = doc = _('substr(str, start, end) -- returns the start\'th through the end\'th ' 'characters of str. The first character in str is the zero\'th ' 'character. If end is negative, then it indicates that many ' 'characters counting from the right. If end is zero, then it ' @@ -249,7 +265,8 @@ class BuiltinSubstr(BuiltinFormatterFunction): class BuiltinLookup(BuiltinFormatterFunction): name = 'lookup' arg_count = -1 - doc = _('lookup(val, pattern, field, pattern, field, ..., else_field) -- ' + category = 'Iterating over values' + __doc__ = doc = _('lookup(val, pattern, field, pattern, field, ..., else_field) -- ' 'like switch, except the arguments are field (metadata) names, not ' 'text. The value of the appropriate field will be fetched and used. ' 'Note that because composite columns are fields, you can use this ' @@ -276,7 +293,8 @@ class BuiltinLookup(BuiltinFormatterFunction): class BuiltinTest(BuiltinFormatterFunction): name = 'test' arg_count = 3 - doc = _('test(val, text if not empty, text if empty) -- return `text if not ' + category = 'If-then-else' + __doc__ = doc = _('test(val, text if not empty, text if empty) -- return `text if not ' 'empty` if the field is not empty, otherwise return `text if empty`') def evaluate(self, formatter, kwargs, mi, locals, val, value_if_set, value_not_set): @@ -288,7 +306,8 @@ class BuiltinTest(BuiltinFormatterFunction): class BuiltinContains(BuiltinFormatterFunction): name = 'contains' arg_count = 4 - doc = _('contains(val, pattern, text if match, text if not match) -- checks ' + category = 'If-then-else' + __doc__ = doc = _('contains(val, pattern, text if match, text if not match) -- checks ' 'if field contains matches for the regular expression `pattern`. ' 'Returns `text if match` if matches are found, otherwise it returns ' '`text if no match`') @@ -303,7 +322,8 @@ class BuiltinContains(BuiltinFormatterFunction): class BuiltinSwitch(BuiltinFormatterFunction): name = 'switch' arg_count = -1 - doc = _('switch(val, pattern, value, pattern, value, ..., else_value) -- ' + category = 'Iterating over values' + __doc__ = doc = _('switch(val, pattern, value, pattern, value, ..., else_value) -- ' 'for each `pattern, value` pair, checks if the field matches ' 'the regular expression `pattern` and if so, returns that ' '`value`. If no pattern matches, then else_value is returned. ' @@ -323,7 +343,8 @@ class BuiltinSwitch(BuiltinFormatterFunction): class BuiltinInList(BuiltinFormatterFunction): name = 'in_list' arg_count = 5 - doc = _('in_list(val, separator, pattern, found_val, not_found_val) -- ' + category = 'List Lookup' + __doc__ = doc = _('in_list(val, separator, pattern, found_val, not_found_val) -- ' 'treat val as a list of items separated by separator, ' 'comparing the pattern against each value in the list. If the ' 'pattern matches a value, return found_val, otherwise return ' @@ -340,7 +361,8 @@ class BuiltinInList(BuiltinFormatterFunction): class BuiltinStrInList(BuiltinFormatterFunction): name = 'str_in_list' arg_count = 5 - doc = _('str_in_list(val, separator, string, found_val, not_found_val) -- ' + category = 'List lookup' + __doc__ = doc = _('str_in_list(val, separator, string, found_val, not_found_val) -- ' 'treat val as a list of items separated by separator, ' 'comparing the string against each value in the list. If the ' 'string matches a value, return found_val, otherwise return ' @@ -357,10 +379,37 @@ class BuiltinStrInList(BuiltinFormatterFunction): return fv return nfv +class BuiltinIdentifierInList(BuiltinFormatterFunction): + name = 'identifier_in_list' + arg_count = 4 + category = 'List lookup' + __doc__ = doc = _('identifier_in_list(val, id, found_val, not_found_val) -- ' + 'treat val as a list of identifiers separated by commas, ' + 'comparing the string against each value in the list. An identifier ' + 'has the format "identifier:value". The id parameter should be ' + 'either "id" or "id:regexp". The first case matches if there is any ' + 'identifier with that id. The second case matches if the regexp ' + 'matches the identifier\'s value. If there is a match, ' + 'return found_val, otherwise return not_found_val.') + + def evaluate(self, formatter, kwargs, mi, locals, val, ident, fv, nfv): + l = [v.strip() for v in val.split(',') if v.strip()] + (id, _, regexp) = ident.partition(':') + if not id: + return nfv + id += ':' + if l: + for v in l: + if v.startswith(id): + if not regexp or re.search(regexp, v[len(id):], flags=re.I): + return fv + return nfv + class BuiltinRe(BuiltinFormatterFunction): name = 're' arg_count = 3 - doc = _('re(val, pattern, replacement) -- return the field after applying ' + category = 'String Manipulation' + __doc__ = doc = _('re(val, pattern, replacement) -- return the field after applying ' 'the regular expression. All instances of `pattern` are replaced ' 'with `replacement`. As in all of calibre, these are ' 'python-compatible regular expressions') @@ -371,7 +420,8 @@ class BuiltinRe(BuiltinFormatterFunction): class BuiltinIfempty(BuiltinFormatterFunction): name = 'ifempty' arg_count = 2 - doc = _('ifempty(val, text if empty) -- return val if val is not empty, ' + category = 'If-then-else' + __doc__ = doc = _('ifempty(val, text if empty) -- return val if val is not empty, ' 'otherwise return `text if empty`') def evaluate(self, formatter, kwargs, mi, locals, val, value_if_empty): @@ -383,7 +433,8 @@ class BuiltinIfempty(BuiltinFormatterFunction): class BuiltinShorten(BuiltinFormatterFunction): name = 'shorten' arg_count = 4 - doc = _('shorten(val, left chars, middle text, right chars) -- Return a ' + category = 'String Manipulation' + __doc__ = doc = _('shorten(val, left chars, middle text, right chars) -- Return a ' 'shortened version of the field, consisting of `left chars` ' 'characters from the beginning of the field, followed by ' '`middle text`, followed by `right chars` characters from ' @@ -408,7 +459,8 @@ class BuiltinShorten(BuiltinFormatterFunction): class BuiltinCount(BuiltinFormatterFunction): name = 'count' arg_count = 2 - doc = _('count(val, separator) -- interprets the value as a list of items ' + category = 'List Manipulation' + __doc__ = doc = _('count(val, separator) -- interprets the value as a list of items ' 'separated by `separator`, returning the number of items in the ' 'list. Most lists use a comma as the separator, but authors ' 'uses an ampersand. Examples: {tags:count(,)}, {authors:count(&)}') @@ -419,7 +471,8 @@ class BuiltinCount(BuiltinFormatterFunction): class BuiltinListitem(BuiltinFormatterFunction): name = 'list_item' arg_count = 3 - doc = _('list_item(val, index, separator) -- interpret the value as a list of ' + category = 'List Lookup' + __doc__ = doc = _('list_item(val, index, separator) -- interpret the value as a list of ' 'items separated by `separator`, returning the `index`th item. ' 'The first item is number zero. The last item can be returned ' 'using `list_item(-1,separator)`. If the item is not in the list, ' @@ -439,7 +492,8 @@ class BuiltinListitem(BuiltinFormatterFunction): class BuiltinSelect(BuiltinFormatterFunction): name = 'select' arg_count = 2 - doc = _('select(val, key) -- interpret the value as a comma-separated list ' + category = 'List Lookup' + __doc__ = doc = _('select(val, key) -- interpret the value as a comma-separated list ' 'of items, with the items being "id:value". Find the pair with the' 'id equal to key, and return the corresponding value.' ) @@ -456,9 +510,10 @@ class BuiltinSelect(BuiltinFormatterFunction): class BuiltinSublist(BuiltinFormatterFunction): name = 'sublist' arg_count = 4 - doc = _('sublist(val, start_index, end_index, separator) -- interpret the ' + category = 'List Manipulation' + __doc__ = doc = _('sublist(val, start_index, end_index, separator) -- interpret the ' 'value as a list of items separated by `separator`, returning a ' - 'new list made from the `start_index`th to the `end_index`th item. ' + 'new list made from the `start_index` to the `end_index` item. ' 'The first item is number zero. If an index is negative, then it ' 'counts from the end of the list. As a special case, an end_index ' 'of zero is assumed to be the length of the list. Examples using ' @@ -466,7 +521,8 @@ class BuiltinSublist(BuiltinFormatterFunction): 'comma-separated) contains "A, B, C": ' '{tags:sublist(0,1,\,)} returns "A". ' '{tags:sublist(-1,0,\,)} returns "C". ' - '{tags:sublist(0,-1,\,)} returns "A, B".') + '{tags:sublist(0,-1,\,)} returns "A, B".' + ) def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep): if not val: @@ -485,12 +541,13 @@ class BuiltinSublist(BuiltinFormatterFunction): class BuiltinSubitems(BuiltinFormatterFunction): name = 'subitems' arg_count = 3 - doc = _('subitems(val, start_index, end_index) -- This function is used to ' + category = 'List Manipulation' + __doc__ = doc = _('subitems(val, start_index, end_index) -- This function is used to ' 'break apart lists of items such as genres. It interprets the value ' 'as a comma-separated list of items, where each item is a period-' 'separated list. Returns a new list made by first finding all the ' 'period-separated items, then for each such item extracting the ' - 'start_index`th to the `end_index`th components, then combining ' + 'start_index` to the `end_index` components, then combining ' 'the results back together. The first component in a period-' 'separated list has an index of zero. If an index is negative, ' 'then it counts from the end of the list. As a special case, an ' @@ -519,11 +576,12 @@ class BuiltinSubitems(BuiltinFormatterFunction): pass return ', '.join(sorted(rv, key=sort_key)) -class BuiltinFormat_date(BuiltinFormatterFunction): +class BuiltinFormatDate(BuiltinFormatterFunction): name = 'format_date' arg_count = 2 - doc = _('format_date(val, format_string) -- format the value, which must ' - 'be a date field, using the format_string, returning a string. ' + category = 'Get values from metadata' + __doc__ = doc = _('format_date(val, format_string) -- format the value, ' + 'which must be a date, using the format_string, returning a string. ' 'The formatting codes are: ' 'd : the day as number without a leading zero (1 to 31) ' 'dd : the day as number with a leading zero (01 to 31) ' @@ -538,7 +596,7 @@ class BuiltinFormat_date(BuiltinFormatterFunction): 'iso : the date with time and timezone. Must be the only format present') def evaluate(self, formatter, kwargs, mi, locals, val, format_string): - if not val: + if not val or val == 'None': return '' try: dt = parse_date(val) @@ -550,7 +608,8 @@ class BuiltinFormat_date(BuiltinFormatterFunction): class BuiltinUppercase(BuiltinFormatterFunction): name = 'uppercase' arg_count = 1 - doc = _('uppercase(val) -- return value of the field in upper case') + category = 'String case changes' + __doc__ = doc = _('uppercase(val) -- return value of the field in upper case') def evaluate(self, formatter, kwargs, mi, locals, val): return val.upper() @@ -558,7 +617,8 @@ class BuiltinUppercase(BuiltinFormatterFunction): class BuiltinLowercase(BuiltinFormatterFunction): name = 'lowercase' arg_count = 1 - doc = _('lowercase(val) -- return value of the field in lower case') + category = 'String case changes' + __doc__ = doc = _('lowercase(val) -- return value of the field in lower case') def evaluate(self, formatter, kwargs, mi, locals, val): return val.lower() @@ -566,7 +626,8 @@ class BuiltinLowercase(BuiltinFormatterFunction): class BuiltinTitlecase(BuiltinFormatterFunction): name = 'titlecase' arg_count = 1 - doc = _('titlecase(val) -- return value of the field in title case') + category = 'String case changes' + __doc__ = doc = _('titlecase(val) -- return value of the field in title case') def evaluate(self, formatter, kwargs, mi, locals, val): return titlecase(val) @@ -574,7 +635,8 @@ class BuiltinTitlecase(BuiltinFormatterFunction): class BuiltinCapitalize(BuiltinFormatterFunction): name = 'capitalize' arg_count = 1 - doc = _('capitalize(val) -- return value of the field capitalized') + category = 'String case changes' + __doc__ = doc = _('capitalize(val) -- return value of the field capitalized') def evaluate(self, formatter, kwargs, mi, locals, val): return capitalize(val) @@ -582,7 +644,8 @@ class BuiltinCapitalize(BuiltinFormatterFunction): class BuiltinBooksize(BuiltinFormatterFunction): name = 'booksize' arg_count = 0 - doc = _('booksize() -- return value of the size field') + category = 'Get values from metadata' + __doc__ = doc = _('booksize() -- return value of the size field') def evaluate(self, formatter, kwargs, mi, locals): if mi.book_size is not None: @@ -595,7 +658,8 @@ class BuiltinBooksize(BuiltinFormatterFunction): class BuiltinOndevice(BuiltinFormatterFunction): name = 'ondevice' arg_count = 0 - doc = _('ondevice() -- return Yes if ondevice is set, otherwise return ' + category = 'Get values from metadata' + __doc__ = doc = _('ondevice() -- return Yes if ondevice is set, otherwise return ' 'the empty string') def evaluate(self, formatter, kwargs, mi, locals): @@ -606,7 +670,8 @@ class BuiltinOndevice(BuiltinFormatterFunction): class BuiltinFirstNonEmpty(BuiltinFormatterFunction): name = 'first_non_empty' arg_count = -1 - doc = _('first_non_empty(value, value, ...) -- ' + category = 'Iterating over values' + __doc__ = doc = _('first_non_empty(value, value, ...) -- ' 'returns the first value that is not empty. If all values are ' 'empty, then the empty value is returned.' 'You can have as many values as you want.') @@ -622,7 +687,8 @@ class BuiltinFirstNonEmpty(BuiltinFormatterFunction): class BuiltinAnd(BuiltinFormatterFunction): name = 'and' arg_count = -1 - doc = _('and(value, value, ...) -- ' + category = 'Boolean' + __doc__ = doc = _('and(value, value, ...) -- ' 'returns the string "1" if all values are not empty, otherwise ' 'returns the empty string. This function works well with test or ' 'first_non_empty. You can have as many values as you want.') @@ -638,7 +704,8 @@ class BuiltinAnd(BuiltinFormatterFunction): class BuiltinOr(BuiltinFormatterFunction): name = 'or' arg_count = -1 - doc = _('or(value, value, ...) -- ' + category = 'Boolean' + __doc__ = doc = _('or(value, value, ...) -- ' 'returns the string "1" if any value is not empty, otherwise ' 'returns the empty string. This function works well with test or ' 'first_non_empty. You can have as many values as you want.') @@ -654,7 +721,8 @@ class BuiltinOr(BuiltinFormatterFunction): class BuiltinNot(BuiltinFormatterFunction): name = 'not' arg_count = 1 - doc = _('not(value) -- ' + category = 'Boolean' + __doc__ = doc = _('not(value) -- ' 'returns the string "1" if the value is empty, otherwise ' 'returns the empty string. This function works well with test or ' 'first_non_empty. You can have as many values as you want.') @@ -670,7 +738,8 @@ class BuiltinNot(BuiltinFormatterFunction): class BuiltinMergeLists(BuiltinFormatterFunction): name = 'merge_lists' arg_count = 3 - doc = _('merge_lists(list1, list2, separator) -- ' + category = 'List Manipulation' + __doc__ = doc = _('merge_lists(list1, list2, separator) -- ' 'return a list made by merging the items in list1 and list2, ' 'removing duplicate items using a case-insensitive compare. If ' 'items differ in case, the one in list1 is used. ' @@ -703,7 +772,8 @@ builtin_divide = BuiltinDivide() builtin_eval = BuiltinEval() builtin_first_non_empty = BuiltinFirstNonEmpty() builtin_field = BuiltinField() -builtin_format_date = BuiltinFormat_date() +builtin_format_date = BuiltinFormatDate() +builtin_identifier_in_list = BuiltinIdentifierInList() builtin_ifempty = BuiltinIfempty() builtin_in_list = BuiltinInList() builtin_list_item = BuiltinListitem() @@ -715,7 +785,7 @@ builtin_not = BuiltinNot() builtin_ondevice = BuiltinOndevice() builtin_or = BuiltinOr() builtin_print = BuiltinPrint() -builtin_raw_field = BuiltinRaw_field() +builtin_raw_field = BuiltinRawField() builtin_re = BuiltinRe() builtin_select = BuiltinSelect() builtin_shorten = BuiltinShorten()