Use decorators for builtin S&R functions: prettier :)

This commit is contained in:
Kovid Goyal 2014-11-19 09:52:18 +05:30
parent 18e61e8eab
commit bff2ebc9d5
2 changed files with 12 additions and 9 deletions

View File

@ -319,7 +319,7 @@ class Resources(Command): # {{{
from calibre.gui2.tweak_book.function_replace import builtin_functions
for func in builtin_functions():
try:
src = u''.join(inspect.getsourcelines(func)[0])
src = ''.join(inspect.getsourcelines(func)[0][1:])
except Exception:
continue
src = src.replace('def ' + func.func_name, 'def replace')

View File

@ -121,41 +121,44 @@ class FunctionBox(EditWithComplete):
# Builtin functions ##########################################################
def builtin(func, name, *args):
func.name = name
func.imports = args
def builtin(name, *args):
def f(func):
func.name = name
func.imports = args
return func
return f
@builtin('Upper-case text', upper, apply_func_to_match_groups)
def replace_uppercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Make matched text upper case. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is
changed.'''
return apply_func_to_match_groups(match, upper)
builtin(replace_uppercase, 'Upper-case text', upper, apply_func_to_match_groups)
@builtin('Lower-case text', lower, apply_func_to_match_groups)
def replace_lowercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Make matched text lower case. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is
changed.'''
return apply_func_to_match_groups(match, lower)
builtin(replace_lowercase, 'Lower-case text', lower, apply_func_to_match_groups)
@builtin('Capitalize text', capitalize, apply_func_to_match_groups)
def replace_capitalize(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Capitalize matched text. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is
changed.'''
return apply_func_to_match_groups(match, capitalize)
builtin(replace_capitalize, 'Capitalize text', capitalize, apply_func_to_match_groups)
@builtin('Title-case text', titlecase, apply_func_to_match_groups)
def replace_titlecase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Title-case matched text. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is
changed.'''
return apply_func_to_match_groups(match, titlecase)
builtin(replace_titlecase, 'Title-case text', titlecase, apply_func_to_match_groups)
@builtin('Swap the case of text', swapcase, apply_func_to_match_groups)
def replace_swapcase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Swap the case of the matched text. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is
changed.'''
return apply_func_to_match_groups(match, swapcase)
builtin(replace_swapcase, 'Swap the case of text', swapcase, apply_func_to_match_groups)