From e445384d521eb7b64bc4a8e68a3d4881cf7e3dff Mon Sep 17 00:00:00 2001 From: Jim Miller Date: Fri, 20 Mar 2015 09:34:52 -0500 Subject: [PATCH] Better error handling of generate_cover_settings params. --- calibre-plugin/ffdl_plugin.py | 17 +++++++++-------- fanficdownloader/configurable.py | 29 +++++++++++++++++++++++++++++ fanficdownloader/exceptions.py | 15 +++++++++++---- fanficdownloader/story.py | 7 +------ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/calibre-plugin/ffdl_plugin.py b/calibre-plugin/ffdl_plugin.py index 4cf58995..fec4eec2 100644 --- a/calibre-plugin/ffdl_plugin.py +++ b/calibre-plugin/ffdl_plugin.py @@ -1777,14 +1777,15 @@ class FanFictionDownLoaderPlugin(InterfaceAction): # template => regexp to match => GC Setting to use. # generate_cover_settings: # ${category} => Buffy:? the Vampire Slayer => Buffy - for line in configuration.getConfig('generate_cover_settings').splitlines(): - if "=>" in line: - (template,regexp,setting) = map( lambda x: x.strip(), line.split("=>") ) - value = Template(template).safe_substitute(book['all_metadata']).encode('utf8') - # print("%s(%s) => %s => %s"%(template,value,regexp,setting)) - if re.search(regexp,value): - setting_name = setting - break + # for line in configuration.getConfig('generate_cover_settings').splitlines(): + # if "=>" in line: + # (template,regexp,setting) = map( lambda x: x.strip(), line.split("=>") ) + for (template,regexp,setting) in configuration.get_generate_cover_settings(): + value = Template(template).safe_substitute(book['all_metadata']).encode('utf8') + # print("%s(%s) => %s => %s"%(template,value,regexp,setting)) + if re.search(regexp,value): + setting_name = setting + break if setting_name: logger.debug("Generate Cover Setting from generate_cover_settings(%s)"%line) diff --git a/fanficdownloader/configurable.py b/fanficdownloader/configurable.py index f96fbdf3..dc30feae 100644 --- a/fanficdownloader/configurable.py +++ b/fanficdownloader/configurable.py @@ -36,6 +36,12 @@ from ConfigParser import DEFAULTSECT, MissingSectionHeaderError, ParsingError import adapters +def re_compile(regex,line): + try: + return re.compile(regex) + except Exception, e: + raise exceptions.RegularExpresssionFailed(e,regex,line) + formatsections = ['html','txt','epub','mobi'] othersections = ['defaults','overrides'] @@ -274,6 +280,21 @@ def get_valid_entry_keywords(): return list(['%s_label', '(default_value|include_in|join_string|keep_in_order)_%s',]) +# Moved here for test_config. +def make_generate_cover_settings(param): + vlist = [] + for line in param.splitlines(): + if "=>" in line: + try: + (template,regexp,setting) = map( lambda x: x.strip(), line.split("=>") ) + re_compile(regexp,line) + vlist.append((template,regexp,setting)) + except Exception, e: + raise exceptions.PersonalIniFailed(e,line,param) + + return vlist + + class Configuration(ConfigParser.SafeConfigParser): def __init__(self, site, fileform): @@ -371,6 +392,9 @@ class Configuration(ConfigParser.SafeConfigParser): def getConfigList(self, key): return self.get_config_list(self.sectionslist, key) + # Moved here for test_config. + def get_generate_cover_settings(self): + return make_generate_cover_settings(self.getConfig('generate_cover_settings')) def get_lineno(self,section,key=None): if key: @@ -480,6 +504,8 @@ class Configuration(ConfigParser.SafeConfigParser): from story import set_in_ex_clude, make_replacements custom_columns_settings_re = re.compile(r'(add_to_)?custom_columns_settings') + + generate_cover_settings_re = re.compile(r'(add_to_)?generate_cover_settings') valdict = get_valid_set_options() @@ -512,6 +538,9 @@ class Configuration(ConfigParser.SafeConfigParser): if replace_metadata_re.match(keyword): make_replacements(value) + if generate_cover_settings_re.match(keyword): + make_generate_cover_settings(value) + # if custom_columns_settings_re.match(keyword): #custom_columns_settings: # cliches=>#acolumn diff --git a/fanficdownloader/exceptions.py b/fanficdownloader/exceptions.py index 2960add9..d7ddd1f6 100644 --- a/fanficdownloader/exceptions.py +++ b/fanficdownloader/exceptions.py @@ -75,12 +75,19 @@ class FailedToWriteOutput(Exception): def __str__(self): return self.error -class RegularExpresssionFailed(Exception): - def __init__(self,error,regex,line): +class PersonalIniFailed(Exception): + def __init__(self,error,part,line): self.error=error - self.regex=regex + self.part=part self.line=line def __str__(self): - return "Regular Expression Error '%s' in regex '%s' in line '%s'"%(self.error,self.regex,self.line) + return "personal.ini Error '%s' in '%s' in line '%s'"%(self.error,self.part,self.line) + +class RegularExpresssionFailed(PersonalIniFailed): + def __init__(self,error,part,line): + PersonalIniFailed.__init__(self,error,part,line) + + def __str__(self): + return "Regular Expression Error '%s' in part '%s' in line '%s'"%(self.error,self.part,self.line) diff --git a/fanficdownloader/story.py b/fanficdownloader/story.py index e48fd132..a3cafeb0 100644 --- a/fanficdownloader/story.py +++ b/fanficdownloader/story.py @@ -26,7 +26,7 @@ import urlparse as up import exceptions from htmlcleanup import conditionalRemoveEntities, removeAllEntities -from configurable import Configurable +from configurable import Configurable, re_compile SPACE_REPLACE=u'\s' SPLIT_META=u'\,' @@ -279,11 +279,6 @@ langs = { u'中文':'zh', u'Bahasa Malaysia':'zsm', } -def re_compile(regex,line): - try: - return re.compile(regex) - except Exception, e: - raise exceptions.RegularExpresssionFailed(e,regex,line) class InExMatch: keys = []