This commit is contained in:
Kovid Goyal 2014-11-19 09:20:38 +05:30
parent 705ed00b5f
commit 18e61e8eab
2 changed files with 8 additions and 13 deletions

View File

@ -323,10 +323,7 @@ class Resources(Command): # {{{
except Exception: except Exception:
continue continue
src = src.replace('def ' + func.func_name, 'def replace') src = src.replace('def ' + func.func_name, 'def replace')
imports = [] imports = ['from %s import %s' % (x.__module__, x.__name__) for x in func.imports]
for x in func.imports.split():
if x and x.strip():
imports.append('from %s import %s' % tuple(x.split(':')))
if imports: if imports:
src = '\n'.join(imports) + '\n\n' + src src = '\n'.join(imports) + '\n\n' + src
function_dict[func.name] = src function_dict[func.name] = src

View File

@ -121,43 +121,41 @@ class FunctionBox(EditWithComplete):
# Builtin functions ########################################################## # Builtin functions ##########################################################
def builtin(func, name, imports='', uses_apply=False): def builtin(func, name, *args):
func.name = name func.name = name
if uses_apply: func.imports = args
imports = 'calibre.ebooks.oeb.polish.utils:apply_func_to_match_groups ' + imports
func.imports = imports.rstrip()
def replace_uppercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs): def replace_uppercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Make matched text upper case. If the regular expression contains groups, '''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 only the text in the groups will be changed, otherwise the entire text is
changed.''' changed.'''
return apply_func_to_match_groups(match, upper) return apply_func_to_match_groups(match, upper)
builtin(replace_uppercase, 'Upper-case text', 'calibre.utils.icu:upper', True) builtin(replace_uppercase, 'Upper-case text', upper, apply_func_to_match_groups)
def replace_lowercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs): def replace_lowercase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Make matched text lower case. If the regular expression contains groups, '''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 only the text in the groups will be changed, otherwise the entire text is
changed.''' changed.'''
return apply_func_to_match_groups(match, lower) return apply_func_to_match_groups(match, lower)
builtin(replace_lowercase, 'Lower-case text', 'calibre.utils.icu:lower', True) builtin(replace_lowercase, 'Lower-case text', lower, apply_func_to_match_groups)
def replace_capitalize(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs): def replace_capitalize(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Capitalize matched text. If the regular expression contains groups, '''Capitalize matched text. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is only the text in the groups will be changed, otherwise the entire text is
changed.''' changed.'''
return apply_func_to_match_groups(match, capitalize) return apply_func_to_match_groups(match, capitalize)
builtin(replace_capitalize, 'Capitalize text', 'calibre.utils.icu:capitalize', True) builtin(replace_capitalize, 'Capitalize text', capitalize, apply_func_to_match_groups)
def replace_titlecase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs): def replace_titlecase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs):
'''Title-case matched text. If the regular expression contains groups, '''Title-case matched text. If the regular expression contains groups,
only the text in the groups will be changed, otherwise the entire text is only the text in the groups will be changed, otherwise the entire text is
changed.''' changed.'''
return apply_func_to_match_groups(match, titlecase) return apply_func_to_match_groups(match, titlecase)
builtin(replace_titlecase, 'Title-case text', 'calibre.utils.titlecase:titlecase', True) builtin(replace_titlecase, 'Title-case text', titlecase, apply_func_to_match_groups)
def replace_swapcase(match, number, file_name, metadata, dictionaries, functions, *args, **kwargs): 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, '''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 only the text in the groups will be changed, otherwise the entire text is
changed.''' changed.'''
return apply_func_to_match_groups(match, swapcase) return apply_func_to_match_groups(match, swapcase)
builtin(replace_swapcase, 'Swap the case of text', 'calibre.utils.icu:swapcase', True) builtin(replace_swapcase, 'Swap the case of text', swapcase, apply_func_to_match_groups)