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 = {}
|
function_dict = {}
|
||||||
import inspect
|
import inspect
|
||||||
from calibre.utils.formatter_functions import formatter_functions
|
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,
|
eval_func = inspect.getmembers(obj,
|
||||||
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
||||||
try:
|
try:
|
||||||
|
@ -45,7 +45,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
"keyword"))
|
"keyword"))
|
||||||
TemplateHighlighter.Rules.append((QRegExp(
|
TemplateHighlighter.Rules.append((QRegExp(
|
||||||
"|".join([r"\b%s\b" % builtin for builtin in
|
"|".join([r"\b%s\b" % builtin for builtin in
|
||||||
formatter_functions.get_builtins()])),
|
formatter_functions().get_builtins()])),
|
||||||
"builtin"))
|
"builtin"))
|
||||||
|
|
||||||
TemplateHighlighter.Rules.append((QRegExp(
|
TemplateHighlighter.Rules.append((QRegExp(
|
||||||
@ -248,8 +248,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
except:
|
except:
|
||||||
self.builtin_source_dict = {}
|
self.builtin_source_dict = {}
|
||||||
|
|
||||||
self.funcs = formatter_functions.get_functions()
|
self.funcs = formatter_functions().get_functions()
|
||||||
self.builtins = formatter_functions.get_builtins()
|
self.builtins = formatter_functions().get_builtins()
|
||||||
|
|
||||||
func_names = sorted(self.funcs)
|
func_names = sorted(self.funcs)
|
||||||
self.function.clear()
|
self.function.clear()
|
||||||
|
@ -82,8 +82,8 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
self.builtin_source_dict = {}
|
self.builtin_source_dict = {}
|
||||||
|
|
||||||
self.funcs = formatter_functions.get_functions()
|
self.funcs = formatter_functions().get_functions()
|
||||||
self.builtins = formatter_functions.get_builtins_and_aliases()
|
self.builtins = formatter_functions().get_builtins_and_aliases()
|
||||||
|
|
||||||
self.build_function_names_box()
|
self.build_function_names_box()
|
||||||
self.function_name.currentIndexChanged[str].connect(self.function_index_changed)
|
self.function_name.currentIndexChanged[str].connect(self.function_index_changed)
|
||||||
@ -217,13 +217,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
formatter_functions.reset_to_builtins()
|
formatter_functions().reset_to_builtins()
|
||||||
pref_value = []
|
pref_value = []
|
||||||
for f in self.funcs:
|
for f in self.funcs:
|
||||||
if f in self.builtins:
|
if f in self.builtins:
|
||||||
continue
|
continue
|
||||||
func = self.funcs[f]
|
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))
|
pref_value.append((func.name, func.doc, func.arg_count, func.program_text))
|
||||||
self.db.prefs.set('user_template_functions', pref_value)
|
self.db.prefs.set('user_template_functions', pref_value)
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ def generate_template_language_help():
|
|||||||
|
|
||||||
funcs = defaultdict(dict)
|
funcs = defaultdict(dict)
|
||||||
|
|
||||||
for func in formatter_functions.get_builtins().values():
|
for func in formatter_functions().get_builtins().values():
|
||||||
class_name = func.__class__.__name__
|
class_name = func.__class__.__name__
|
||||||
func_sig = getattr(func, 'doc')
|
func_sig = getattr(func, 'doc')
|
||||||
x = func_sig.find(' -- ')
|
x = func_sig.find(' -- ')
|
||||||
|
@ -88,7 +88,7 @@ class _Parser(object):
|
|||||||
|
|
||||||
def expr(self):
|
def expr(self):
|
||||||
if self.token_is_id():
|
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
|
# We have an identifier. Determine if it is a function
|
||||||
id = self.token()
|
id = self.token()
|
||||||
if not self.token_op_is_a('('):
|
if not self.token_op_is_a('('):
|
||||||
@ -276,7 +276,7 @@ class TemplateFormatter(string.Formatter):
|
|||||||
dispfmt = fmt[0:colon]
|
dispfmt = fmt[0:colon]
|
||||||
colon += 1
|
colon += 1
|
||||||
|
|
||||||
funcs = formatter_functions.get_functions()
|
funcs = formatter_functions().get_functions()
|
||||||
fname = fmt[colon:p]
|
fname = fmt[colon:p]
|
||||||
if fname in funcs:
|
if fname in funcs:
|
||||||
func = funcs[fname]
|
func = funcs[fname]
|
||||||
|
@ -64,8 +64,11 @@ class FormatterFunctions(object):
|
|||||||
for a in c.aliases:
|
for a in c.aliases:
|
||||||
self._functions[a] = c
|
self._functions[a] = c
|
||||||
|
|
||||||
formatter_functions = FormatterFunctions()
|
_ff = FormatterFunctions()
|
||||||
|
|
||||||
|
def formatter_functions():
|
||||||
|
global _ff
|
||||||
|
return _ff
|
||||||
|
|
||||||
class FormatterFunction(object):
|
class FormatterFunction(object):
|
||||||
|
|
||||||
@ -89,7 +92,7 @@ class FormatterFunction(object):
|
|||||||
|
|
||||||
class BuiltinFormatterFunction(FormatterFunction):
|
class BuiltinFormatterFunction(FormatterFunction):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
formatter_functions.register_builtin(self)
|
formatter_functions().register_builtin(self)
|
||||||
eval_func = inspect.getmembers(self.__class__,
|
eval_func = inspect.getmembers(self.__class__,
|
||||||
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
lambda x: inspect.ismethod(x) and x.__name__ == 'evaluate')
|
||||||
try:
|
try:
|
||||||
@ -1133,10 +1136,10 @@ class UserFunction(FormatterUserFunction):
|
|||||||
return cls
|
return cls
|
||||||
|
|
||||||
def load_user_template_functions(funcs):
|
def load_user_template_functions(funcs):
|
||||||
formatter_functions.reset_to_builtins()
|
formatter_functions().reset_to_builtins()
|
||||||
for func in funcs:
|
for func in funcs:
|
||||||
try:
|
try:
|
||||||
cls = compile_user_function(*func)
|
cls = compile_user_function(*func)
|
||||||
formatter_functions.register_function(cls)
|
formatter_functions().register_function(cls)
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user