mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-02-24 14:54:37 +01:00
change formatter_functions from an attribute to a function
This commit is contained in:
parent
e6ded55f62
commit
bef7077158
6 changed files with 18 additions and 15 deletions
|
|
@ -206,7 +206,7 @@ def run(self, opts):
|
|||
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 @@ def __init__(self, parent=None):
|
|||
"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 @@ def __init__(self, parent, text, mi=None, fm=None, color_field=None):
|
|||
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 @@ def initialize(self):
|
|||
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 @@ def refresh_gui(self, gui):
|
|||
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 @@ def statement(self):
|
|||
|
||||
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 @@ def format_field(self, val, fmt):
|
|||
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 @@ def reset_to_builtins(self):
|
|||
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 @@ def eval_(self, formatter, kwargs, mi, locals, *args):
|
|||
|
||||
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…
Reference in a new issue