mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #877366 (doesn't create folders by save to disk since 0.8.21) and other cleanups
This commit is contained in:
commit
c17fb8bd5e
@ -206,7 +206,7 @@ class Resources(Command):
|
||||
function_dict = {}
|
||||
import inspect
|
||||
from calibre.utils.formatter_functions import formatter_functions
|
||||
for obj in formatter_functions.get_builtins().values():
|
||||
for obj in formatter_functions().get_builtins().values():
|
||||
eval_func = inspect.getmembers(obj,
|
||||
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
||||
try:
|
||||
|
@ -45,7 +45,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
||||
"keyword"))
|
||||
TemplateHighlighter.Rules.append((QRegExp(
|
||||
"|".join([r"\b%s\b" % builtin for builtin in
|
||||
formatter_functions.get_builtins()])),
|
||||
formatter_functions().get_builtins()])),
|
||||
"builtin"))
|
||||
|
||||
TemplateHighlighter.Rules.append((QRegExp(
|
||||
@ -248,8 +248,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
||||
except:
|
||||
self.builtin_source_dict = {}
|
||||
|
||||
self.funcs = formatter_functions.get_functions()
|
||||
self.builtins = formatter_functions.get_builtins()
|
||||
self.funcs = formatter_functions().get_functions()
|
||||
self.builtins = formatter_functions().get_builtins()
|
||||
|
||||
func_names = sorted(self.funcs)
|
||||
self.function.clear()
|
||||
|
@ -82,8 +82,8 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
traceback.print_exc()
|
||||
self.builtin_source_dict = {}
|
||||
|
||||
self.funcs = formatter_functions.get_functions()
|
||||
self.builtins = formatter_functions.get_builtins_and_aliases()
|
||||
self.funcs = formatter_functions().get_functions()
|
||||
self.builtins = formatter_functions().get_builtins_and_aliases()
|
||||
|
||||
self.build_function_names_box()
|
||||
self.function_name.currentIndexChanged[str].connect(self.function_index_changed)
|
||||
@ -217,13 +217,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
pass
|
||||
|
||||
def commit(self):
|
||||
formatter_functions.reset_to_builtins()
|
||||
formatter_functions().reset_to_builtins()
|
||||
pref_value = []
|
||||
for f in self.funcs:
|
||||
if f in self.builtins:
|
||||
continue
|
||||
func = self.funcs[f]
|
||||
formatter_functions.register_function(func)
|
||||
formatter_functions().register_function(func)
|
||||
pref_value.append((func.name, func.doc, func.arg_count, func.program_text))
|
||||
self.db.prefs.set('user_template_functions', pref_value)
|
||||
|
||||
|
@ -302,6 +302,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
if cats_changed:
|
||||
self.prefs.set('user_categories', user_cats)
|
||||
|
||||
if not self.is_second_db:
|
||||
load_user_template_functions(self.prefs.get('user_template_functions', []))
|
||||
|
||||
self.conn.executescript('''
|
||||
|
@ -150,21 +150,12 @@ class Formatter(TemplateFormatter):
|
||||
traceback.print_exc()
|
||||
b = None
|
||||
if b is not None and b['datatype'] == 'composite':
|
||||
val = b.get('#value#', None)
|
||||
if val is not None:
|
||||
return val.replace('/', '_').replace('\\', '_')
|
||||
if key in self.composite_values:
|
||||
self.composite_values[key] = val
|
||||
return self.composite_values[key]
|
||||
try:
|
||||
# We really should not get here, but it is safer to try
|
||||
self.composite_values[key] = 'RECURSIVE_COMPOSITE FIELD (S2D) ' + key
|
||||
self.composite_values[key] = \
|
||||
self.vformat(b['display']['composite_template'],
|
||||
[], kwargs).replace('/', '_').replace('\\', '_')
|
||||
self.vformat(b['display']['composite_template'], [], kwargs)
|
||||
return self.composite_values[key]
|
||||
except Exception, e:
|
||||
return unicode(e)
|
||||
if key in kwargs:
|
||||
val = kwargs[key]
|
||||
if isinstance(val, list):
|
||||
@ -179,13 +170,6 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
||||
sanitize_func=ascii_filename, replace_whitespace=False,
|
||||
to_lowercase=False, safe_format=True):
|
||||
|
||||
# Note: the mi argument is assumed to be an instance of Metadata returned
|
||||
# by db.get_metadata(). Reason: the composite columns should have already
|
||||
# been evaluated, which get_metadata does. If the mi is something else and
|
||||
# if the template uses composite columns, then a best-efforts attempt is
|
||||
# made to evaluate them. This will fail if the template uses a user-defined
|
||||
# template function.
|
||||
|
||||
tsorder = tweaks['save_template_title_series_sorting']
|
||||
format_args = FORMAT_ARGS.copy()
|
||||
format_args.update(mi.all_non_none_fields())
|
||||
|
@ -65,7 +65,7 @@ def generate_template_language_help():
|
||||
|
||||
funcs = defaultdict(dict)
|
||||
|
||||
for func in formatter_functions.get_builtins().values():
|
||||
for func in formatter_functions().get_builtins().values():
|
||||
class_name = func.__class__.__name__
|
||||
func_sig = getattr(func, 'doc')
|
||||
x = func_sig.find(' -- ')
|
||||
|
@ -88,7 +88,7 @@ class _Parser(object):
|
||||
|
||||
def expr(self):
|
||||
if self.token_is_id():
|
||||
funcs = formatter_functions.get_functions()
|
||||
funcs = formatter_functions().get_functions()
|
||||
# We have an identifier. Determine if it is a function
|
||||
id = self.token()
|
||||
if not self.token_op_is_a('('):
|
||||
@ -276,7 +276,7 @@ class TemplateFormatter(string.Formatter):
|
||||
dispfmt = fmt[0:colon]
|
||||
colon += 1
|
||||
|
||||
funcs = formatter_functions.get_functions()
|
||||
funcs = formatter_functions().get_functions()
|
||||
fname = fmt[colon:p]
|
||||
if fname in funcs:
|
||||
func = funcs[fname]
|
||||
|
@ -64,8 +64,11 @@ class FormatterFunctions(object):
|
||||
for a in c.aliases:
|
||||
self._functions[a] = c
|
||||
|
||||
formatter_functions = FormatterFunctions()
|
||||
_ff = FormatterFunctions()
|
||||
|
||||
def formatter_functions():
|
||||
global _ff
|
||||
return _ff
|
||||
|
||||
class FormatterFunction(object):
|
||||
|
||||
@ -89,7 +92,7 @@ class FormatterFunction(object):
|
||||
|
||||
class BuiltinFormatterFunction(FormatterFunction):
|
||||
def __init__(self):
|
||||
formatter_functions.register_builtin(self)
|
||||
formatter_functions().register_builtin(self)
|
||||
eval_func = inspect.getmembers(self.__class__,
|
||||
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
||||
try:
|
||||
@ -1133,10 +1136,10 @@ class UserFunction(FormatterUserFunction):
|
||||
return cls
|
||||
|
||||
def load_user_template_functions(funcs):
|
||||
formatter_functions.reset_to_builtins()
|
||||
formatter_functions().reset_to_builtins()
|
||||
for func in funcs:
|
||||
try:
|
||||
cls = compile_user_function(*func)
|
||||
formatter_functions.register_function(cls)
|
||||
formatter_functions().register_function(cls)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
Loading…
x
Reference in New Issue
Block a user