mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
change formatter_functions from an attribute to a function
This commit is contained in:
parent
e6ded55f62
commit
bef7077158
@ -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)
|
||||
|
||||
|
@ -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