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 __call__(self, req, form): argd = wash_urlargd(form, { 'id' : (int, 0), 'format' : (str, '')}) formats_dict = get_output_formats(True) formats = {} for format in formats_dict.values(): if format['attrs']['visibility']: formats[format['attrs']['code'].lower()] = format['attrs']['content_type'] del formats_dict if argd['id'] and argd['format']: ## Translate back common format names format = { 'nlm' : 'xn', 'marcxml' : 'xm', 'dc' : 'xd', 'endnote' : 'xe', 'mods' : 'xo' }.get(argd['format'], argd['format']) if format in formats: redirect_to_url(req, '%s/%s/%s/export/%s' % (CFG_SITE_URL, CFG_SITE_RECORD, argd['id'], format)) 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 test_get_output_formats(self): """ bibformat - loading multiple output formats """ outputs = bibformat_engine.get_output_formats(with_attributes=True) self.assert_(isinstance(outputs, dict)) self.assert_("TEST1.bfo" in outputs.keys()) self.assert_("TEST2.bfo" in outputs.keys()) self.assert_("unknow.bfo" not in outputs.keys()) #Test correct parsing output_1 = outputs["TEST1.bfo"] self.assertEqual(output_1['attrs']['names']['generic'], "") self.assert_(isinstance(output_1['attrs']['names']['ln'], dict)) self.assert_(isinstance(output_1['attrs']['names']['sn'], dict)) self.assertEqual(output_1['attrs']['code'], "TEST1") self.assert_(len(output_1['attrs']['code']) <= 6)
def test_get_output_formats(self): """ bibformat - loading multiple output formats """ bibformat_engine.CFG_BIBFORMAT_OUTPUTS_PATH = CFG_BIBFORMAT_OUTPUTS_PATH outputs = bibformat_engine.get_output_formats(with_attributes=True) self.assert_(isinstance(outputs, dict)) self.assert_("TEST1.bfo" in outputs.keys()) self.assert_("TEST2.bfo" in outputs.keys()) self.assert_("unknow.bfo" not in outputs.keys()) # Test correct parsing output_1 = outputs["TEST1.bfo"] self.assertEqual(output_1["attrs"]["names"]["generic"], "") self.assert_(isinstance(output_1["attrs"]["names"]["ln"], dict)) self.assert_(isinstance(output_1["attrs"]["names"]["sn"], dict)) self.assertEqual(output_1["attrs"]["code"], "TEST1") self.assert_(len(output_1["attrs"]["code"]) <= 6)
from invenio.errorlib import register_exception from invenio.bibedit_webinterface import WebInterfaceEditPages from invenio.bibeditmulti_webinterface import WebInterfaceMultiEditPages from invenio.bibmerge_webinterface import WebInterfaceMergePages from invenio.bibdocfile_webinterface import WebInterfaceManageDocFilesPages, WebInterfaceFilesPages from invenio.bibfield import get_record from invenio.shellutils import mymkdir import invenio.template websearch_templates = invenio.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 argd.has_key('as'): 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
from invenio.errorlib import register_exception from invenio.bibedit_webinterface import WebInterfaceEditPages from invenio.bibeditmulti_webinterface import WebInterfaceMultiEditPages from invenio.bibmerge_webinterface import WebInterfaceMergePages from invenio.bibdocfile_webinterface import WebInterfaceManageDocFilesPages, WebInterfaceFilesPages from invenio.search_engine import get_record from invenio.shellutils import mymkdir import invenio.template websearch_templates = invenio.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 argd.has_key('as'): argd['aas'] = argd['as'] del argd['as'] # Sometimes, users pass ot=245,700 instead of
def bft2tpl(rewrite_existing_templates=False, only_template_re=None, verbose=0): """Converts bft templates to Jinja2 templates.""" ## Import all invenio modules inside to avoid side-efects ouside ## Flask application context. from invenio.bibformat_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.bibformat_engine import get_format_element, \ get_output_formats, \ pattern_function_params, \ pattern_tag, pattern_lang, \ translation_pattern, \ ln_pattern, get_format_templates from invenio.bibformatadminlib 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', 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): def search_lang_tag(match): """ Searches for the <lang>...</lang> tag. """ ln_tags = {} def clean_language_tag(match): """ Return tag text content if tag language of match is output language. 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, get_format_templates(True).iteritems()) 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.', 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, get_output_formats(with_attributes=True). iteritems()) 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', print CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION, 'template', if only_template is not None: print 'or does not match', only_template_re, 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'