def __call__(self, req, form): argd = wash_urlargd(form, { 'id' : (int, 0), 'format' : (str, '')}) formats_dict = get_output_formats(True) formats = {} for f in formats_dict.values(): if f['attrs']['visibility']: formats[f['attrs']['code'].lower()] = f['attrs']['content_type'] del formats_dict if argd['id'] and argd['format']: ## Translate back common format names f = { 'nlm' : 'xn', 'marcxml' : 'xm', 'dc' : 'xd', 'endnote' : 'xe', 'mods' : 'xo' }.get(argd['format'], argd['format']) if f in formats: redirect_to_url(req, '%s/%s/%s/export/%s' % (CFG_SITE_URL, CFG_SITE_RECORD, argd['id'], f)) else: raise apache.SERVER_RETURN, apache.HTTP_NOT_ACCEPTABLE elif argd['id']: return websearch_templates.tmpl_unapi(formats, identifier=argd['id']) else: return websearch_templates.tmpl_unapi(formats)
def get_outputs_that_use_template(filename): """Return a list of output formats that call the given format template. The returned output formats also give their dependencies on tags. We don't return the complete output formats but some reference to them (filename + names):: [ {'filename':"filename_1.bfo" 'names': {'en':"a name", 'fr': "un nom", 'generic':"a name"} 'tags': ['710__a', '920__'] }, ... ] :param filename: a format template filename :return: output formats references sorted by (generic) name """ output_formats_list = {} tags = [] output_formats = bibformat_engine.get_output_formats(with_attributes=True) for output_format in output_formats: name = output_formats[output_format]['attrs']['names']['generic'] # First look at default template, and add it if necessary if output_formats[output_format]['default'] == filename: output_formats_list[name] = { 'filename': output_format, 'names': output_formats[output_format]['attrs']['names'], 'tags': [] } # Second look at each rule found = False for rule in output_formats[output_format]['rules']: if rule['template'] == filename: found = True tags.append(rule['field']) #Also build dependencies on tags # Finally add dependency on template from rule (overwrite default dependency, # which is weaker in term of tag) if found: output_formats_list[name] = { 'filename': output_format, 'names': output_formats[output_format]['attrs']['names'], 'tags': tags } keys = output_formats_list.keys() keys.sort() return map(output_formats_list.get, keys)
def get_outputs_that_use_template(filename): """Return a list of output formats that call the given format template. The returned output formats also give their dependencies on tags. We don't return the complete output formats but some reference to them (filename + names):: [ {'filename':"filename_1.bfo" 'names': {'en':"a name", 'fr': "un nom", 'generic':"a name"} 'tags': ['710__a', '920__'] }, ... ] :param filename: a format template filename :return: output formats references sorted by (generic) name """ output_formats_list = {} tags = [] output_formats = bibformat_engine.get_output_formats(with_attributes=True) for output_format in output_formats: name = output_formats[output_format]['attrs']['names']['generic'] # First look at default template, and add it if necessary if output_formats[output_format]['default'] == filename: output_formats_list[name] = {'filename':output_format, 'names':output_formats[output_format]['attrs']['names'], 'tags':[]} # Second look at each rule found = False for rule in output_formats[output_format]['rules']: if rule['template'] == filename: found = True tags.append(rule['field']) #Also build dependencies on tags # Finally add dependency on template from rule (overwrite default dependency, # which is weaker in term of tag) if found: output_formats_list[name] = {'filename':output_format, 'names':output_formats[output_format]['attrs']['names'], 'tags':tags} keys = output_formats_list.keys() keys.sort() return map(output_formats_list.get, keys)
def bft2tpl(rewrite_existing_templates=False, only_template_re=None, verbose=0): """Convert *bft* templates to Jinja2 *tpl* templates.""" # Import all invenio modules inside to avoid side-efects ouside # Flask application context. from invenio.modules.formatter.config import CFG_BIBFORMAT_OUTPUTS_PATH, \ CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION, \ CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, \ CFG_BIBFORMAT_FORMAT_JINJA_TEMPLATE_EXTENSION, \ CFG_BIBFORMAT_JINJA_TEMPLATE_PATH from invenio.modules.formatter.engine import get_format_element, \ get_output_formats, \ pattern_function_params, \ pattern_tag, pattern_lang, \ translation_pattern, \ ln_pattern, get_format_templates from invenio.legacy.bibformat.adminlib import \ update_output_format_rules only_template = re.compile(only_template_re) \ if only_template_re is not None else None def rename_template(template): if template[-3:] == CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION and \ (only_template is None or only_template.match(template)): return template[:-3] + \ CFG_BIBFORMAT_FORMAT_JINJA_TEMPLATE_EXTENSION return template def update_rule(rule): rule['template'] = rename_template(rule['template']) print(' ...', rule['template'], 'to', end=' ') print(rename_template(rule['template'])) print(' ', rule) return rule def eval_format_template_elements(format_template, bfo, verbose=0): def insert_element_code(match): error = [] function_name = match.group("function_name") try: format_element = get_format_element(function_name, verbose) except Exception: error.append('Invalid function name %s' % (function_name, )) params_str = [] if format_element is not None: params = {} # Look for function parameters given in format template code all_params = match.group('params') if all_params is not None: function_params_iterator = pattern_function_params.\ finditer(all_params) for param_match in function_params_iterator: sep = param_match.group('sep') name = param_match.group('param') value = param_match.group('value') params[name] = value params_str.append(name + '=' + sep + value + sep) # Replace element with function call with params. result = '{{ bfe_%s(bfo, %s) }}' % (function_name.lower(), ', '.join(params_str)) return result print('\n'.join(error)) # Substitute special tags in the format by our own text. # Special tags have the form # <BFE_format_element_name [param="value"]* /> format = pattern_tag.sub(insert_element_code, format_template) return format def translate(match): """Translate matching values.""" word = match.group("word") translated_word = '{{ _("' + word + '") }}' return translated_word def filter_languages(format_template): """Filter languages in format template.""" def search_lang_tag(match): """Searche for the <lang>...</lang> tag.""" ln_tags = {} def clean_language_tag(match): """Return tag text content. It contains if statement block to match output language. It is called by substitution in 'filter_languages(...)'. @param match: a match object corresponding to the special tag that must be interpreted """ ln_tags[match.group(1)] = match.group(2) return '{% if g.ln == "' + match.group(1) + '" %}' + \ match.group(2) + '{% endif %}' # End of clean_language_tag lang_tag_content = match.group("langs") return '{% lang %}' + lang_tag_content + '{% endlang %}' cleaned_lang_tag = ln_pattern.sub(clean_language_tag, lang_tag_content) # FIXME no traslation for current language # if len(ln_tags) > 0: # cleaned_lang_tag += '{% if not g.ln in ["' + \ # '", "'.join(ln_tags.keys()) + '"] %}' + \ # ln_tags.get(CFG_SITE_LANG, '') + '{% endif %}' return cleaned_lang_tag # End of search_lang_tag filtered_format_template = pattern_lang.sub(search_lang_tag, format_template) return filtered_format_template skip_templates = lambda (name, key): name[-3:] != 'xsl' format_templates = filter(skip_templates, iteritems(get_format_templates(True))) print('>>> Going to migrate %d format template(s) ...' % ( len(format_templates), )) if not os.path.exists(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH): os.makedirs(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH) for name, template in format_templates: if not (only_template is None or only_template.match(name)): continue new_name = os.path.join(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH, rename_template(name)) if os.path.exists(new_name): print(' [!] File', new_name, 'already exists.', end=' ') if not rewrite_existing_templates: print('Skipped.') continue else: shutil.copy2(new_name, new_name + '.backup') print('Rewritten.') print(' ... migrating', name, 'to', new_name) with open(new_name, 'w+') as f: code = template['code'] ln_tags_format = filter_languages(code) localized_format = translation_pattern.sub(translate, ln_tags_format) evaled = eval_format_template_elements(localized_format, None) f.write(evaled) print() skip_legacy = lambda (name, key): name[-11:] != '_legacy.' + \ CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION output_formats = filter( skip_legacy, iteritems(get_output_formats(with_attributes=True))) print('>>> Going to migrate %d output format(s) ...' % ( len(output_formats))) for name, output_format in output_formats: if not any(map(lambda rule: rule['template'][-3:] == CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, output_format['rules'])): print(' [!]', name, 'does not contain any', end=' ') print(CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, 'template', end=' ') if only_template is not None: print('or does not match', only_template_re, end=' ') print('.') continue new_name = name[:-4] + \ '_legacy.' + CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION if os.path.exists(os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, new_name)): print(' [!] File', new_name, 'already exists. Skipped.') continue shutil.copy2( os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, name), os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, new_name)) # rename template names print(' ... migrating', name, 'to', new_name) update_output_format_rules(name, map(update_rule, output_format['rules']), rename_template(output_format['default'])) print() print('>>> Please re-run `bibreformat` for all cached output formats.') print(' $ bibreformat -oHB,HD -a')
def bft2tpl(rewrite_existing_templates=False, only_template_re=None, verbose=0): """Convert *bft* templates to Jinja2 *tpl* templates.""" # Import all invenio modules inside to avoid side-efects ouside # Flask application context. from invenio.modules.formatter.config import CFG_BIBFORMAT_OUTPUTS_PATH, \ CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION, \ CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, \ CFG_BIBFORMAT_FORMAT_JINJA_TEMPLATE_EXTENSION, \ CFG_BIBFORMAT_JINJA_TEMPLATE_PATH from invenio.modules.formatter.engine import get_format_element, \ get_output_formats, \ pattern_function_params, \ pattern_tag, pattern_lang, \ translation_pattern, \ ln_pattern, get_format_templates from invenio.legacy.bibformat.adminlib import \ update_output_format_rules only_template = re.compile(only_template_re) \ if only_template_re is not None else None def rename_template(template): if template[-3:] == CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION and \ (only_template is None or only_template.match(template)): return template[:-3] + \ CFG_BIBFORMAT_FORMAT_JINJA_TEMPLATE_EXTENSION return template def update_rule(rule): rule['template'] = rename_template(rule['template']) print(' ...', rule['template'], 'to', end=' ') print(rename_template(rule['template'])) print(' ', rule) return rule def eval_format_template_elements(format_template, bfo, verbose=0): def insert_element_code(match): error = [] function_name = match.group("function_name") try: format_element = get_format_element(function_name, verbose) except Exception: error.append('Invalid function name %s' % (function_name, )) params_str = [] if format_element is not None: params = {} # Look for function parameters given in format template code all_params = match.group('params') if all_params is not None: function_params_iterator = pattern_function_params.\ finditer(all_params) for param_match in function_params_iterator: sep = param_match.group('sep') name = param_match.group('param') value = param_match.group('value') params[name] = value params_str.append(name + '=' + sep + value + sep) # Replace element with function call with params. result = '{{ bfe_%s(bfo, %s) }}' % (function_name.lower(), ', '.join(params_str)) return result print('\n'.join(error)) # Substitute special tags in the format by our own text. # Special tags have the form # <BFE_format_element_name [param="value"]* /> format = pattern_tag.sub(insert_element_code, format_template) return format def translate(match): """Translate matching values.""" word = match.group("word") translated_word = '{{ _("' + word + '") }}' return translated_word def filter_languages(format_template): """Filter languages in format template.""" def search_lang_tag(match): """Searche for the <lang>...</lang> tag.""" ln_tags = {} def clean_language_tag(match): """Return tag text content. It contains if statement block to match output language. It is called by substitution in 'filter_languages(...)'. @param match: a match object corresponding to the special tag that must be interpreted """ ln_tags[match.group(1)] = match.group(2) return '{% if g.ln == "' + match.group(1) + '" %}' + \ match.group(2) + '{% endif %}' # End of clean_language_tag lang_tag_content = match.group("langs") return '{% lang %}' + lang_tag_content + '{% endlang %}' cleaned_lang_tag = ln_pattern.sub(clean_language_tag, lang_tag_content) # FIXME no traslation for current language # if len(ln_tags) > 0: # cleaned_lang_tag += '{% if not g.ln in ["' + \ # '", "'.join(ln_tags.keys()) + '"] %}' + \ # ln_tags.get(CFG_SITE_LANG, '') + '{% endif %}' return cleaned_lang_tag # End of search_lang_tag filtered_format_template = pattern_lang.sub(search_lang_tag, format_template) return filtered_format_template skip_templates = lambda (name, key): name[-3:] != 'xsl' format_templates = filter(skip_templates, iteritems(get_format_templates(True))) print('>>> Going to migrate %d format template(s) ...' % (len(format_templates), )) if not os.path.exists(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH): os.makedirs(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH) for name, template in format_templates: if not (only_template is None or only_template.match(name)): continue new_name = os.path.join(CFG_BIBFORMAT_JINJA_TEMPLATE_PATH, rename_template(name)) if os.path.exists(new_name): print(' [!] File', new_name, 'already exists.', end=' ') if not rewrite_existing_templates: print('Skipped.') continue else: shutil.copy2(new_name, new_name + '.backup') print('Rewritten.') print(' ... migrating', name, 'to', new_name) with open(new_name, 'w+') as f: code = template['code'] ln_tags_format = filter_languages(code) localized_format = translation_pattern.sub(translate, ln_tags_format) evaled = eval_format_template_elements(localized_format, None) f.write(evaled) print() skip_legacy = lambda (name, key): name[-11:] != '_legacy.' + \ CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION output_formats = filter( skip_legacy, iteritems(get_output_formats(with_attributes=True))) print('>>> Going to migrate %d output format(s) ...' % (len(output_formats))) for name, output_format in output_formats: if not any( map( lambda rule: rule['template'][ -3:] == CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, output_format['rules'])): print(' [!]', name, 'does not contain any', end=' ') print(CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, 'template', end=' ') if only_template is not None: print('or does not match', only_template_re, end=' ') print('.') continue new_name = name[:-4] + \ '_legacy.' + CFG_BIBFORMAT_FORMAT_OUTPUT_EXTENSION if os.path.exists(os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, new_name)): print(' [!] File', new_name, 'already exists. Skipped.') continue shutil.copy2(os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, name), os.path.join(CFG_BIBFORMAT_OUTPUTS_PATH, new_name)) # rename template names print(' ... migrating', name, 'to', new_name) update_output_format_rules(name, map(update_rule, output_format['rules']), rename_template(output_format['default'])) print() print('>>> Please re-run `bibreformat` for all cached output formats.') print(' $ bibreformat -oHB,HD -a')
from invenio.ext.logging import register_exception from invenio.legacy.bibedit.webinterface import WebInterfaceEditPages from invenio.legacy.bibeditmulti.webinterface import WebInterfaceMultiEditPages from invenio.legacy.bibmerge.webinterface import WebInterfaceMergePages from invenio.legacy.bibdocfile.webinterface import WebInterfaceManageDocFilesPages, WebInterfaceFilesPages from invenio.legacy.search_engine import get_record from invenio.utils.shell import mymkdir import invenio.legacy.template websearch_templates = invenio.legacy.template.load('websearch') search_results_default_urlargd = websearch_templates.search_results_default_urlargd search_interface_default_urlargd = websearch_templates.search_interface_default_urlargd try: output_formats = [output_format['attrs']['code'].lower() for output_format in \ get_output_formats(with_attributes=True).values()] except KeyError: output_formats = ['xd', 'xm', 'hd', 'hb', 'hs', 'hx'] output_formats.extend(['hm', 't', 'h']) def wash_search_urlargd(form): """ Create canonical search arguments from those passed via web form. """ argd = wash_urlargd(form, search_results_default_urlargd) if 'as' in argd: argd['aas'] = argd['as'] del argd['as'] if argd.get('aas', CFG_WEBSEARCH_DEFAULT_SEARCH_INTERFACE) not in CFG_WEBSEARCH_ENABLED_SEARCH_INTERFACES: argd['aas'] = CFG_WEBSEARCH_DEFAULT_SEARCH_INTERFACE