def get_templates_used_by_output(code): """Return a list of templates used inside an output format give by its code. The returned format templates also give their dependencies on elements and tags:: [ {'filename':"filename_1.bft" 'name': "a name" 'elements': [{'filename':"filename_1.py", 'name':"filename_1", 'tags': ['710__a', '920__'] }, ...] }, ... ] :param code: outpout format code :return: templates sorted by name """ format_templates = {} output_format = bibformat_engine.get_output_format(code, with_attributes=True) filenames = map(lambda x: x['template'], output_format['rules']) if output_format['default'] != "": filenames.append(output_format['default']) for filename in filenames: template = bibformat_engine.get_format_template(filename, with_attributes=True) name = template['attrs']['name'] elements = get_elements_used_by_template(filename) format_templates[name] = {'name':name, 'filename':filename, 'elements':elements} keys = format_templates.keys() keys.sort() return map(format_templates.get, keys)
def get_templates_that_use_element(name): """Return a list of format templates that call the given format element. The returned format templates also give their dependencies on tags:: [ {'filename':"filename_1.bft" 'name': "a name" 'tags': ['710__a', '920__'] }, ... ] :param name: a format element name :return: templates sorted by name """ format_templates = {} tags = [] files = os.listdir(CFG_BIBFORMAT_TEMPLATES_PATH) #Retrieve all templates for possible_template in files: if possible_template.endswith(CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION): format_elements = get_elements_used_by_template(possible_template) #Look for elements used in template format_elements = map(lambda x: x['name'].lower(), format_elements) try: #Look for element format_elements.index(name.lower()) #If not found, get out of "try" statement format_template = bibformat_engine.get_format_template(filename=possible_template, with_attributes=True) template_name = format_template['attrs']['name'] format_templates[template_name] = {'name':template_name, 'filename':possible_template} except: pass keys = format_templates.keys() keys.sort() return map(format_templates.get, keys)
def get_elements_used_by_template(filename): """ Returns a list of format elements that are called by the given format template. The returned elements also give their dependencies on tags. Dependencies on tag might be approximative. See get_tags_used_by_element() doc string. We must handle usage of bfe_field in a special way if we want to retrieve used tag: used tag is given in "tag" parameter, not inside element code. The list is returned sorted by name:: [ {'filename':"filename_1.py" 'name':"filename_1" 'tags': ['710__a', '920__'] }, ... ] :param filename: a format template filename :return: elements sorted by name """ format_elements = {} format_template = bibformat_engine.get_format_template(filename=filename, with_attributes=True) code = format_template['code'] format_elements_iter = bibformat_engine.pattern_tag.finditer(code) for result in format_elements_iter: function_name = result.group("function_name").lower() if function_name is not None and function_name not in format_elements \ and not function_name == "field": filename = bibformat_engine.resolve_format_element_filename("BFE_"+function_name) if filename is not None: tags = get_tags_used_by_element(filename) format_elements[function_name] = {'name':function_name.lower(), 'filename':filename, 'tags':tags} elif function_name == "field": # Handle bfe_field element in a special way if function_name not in format_elements: #Indicate usage of bfe_field if not already done filename = bibformat_engine.resolve_format_element_filename("BFE_"+function_name) format_elements[function_name] = {'name':function_name.lower(), 'filename':filename, 'tags':[]} # Retrieve value of parameter "tag" all_params = result.group('params') function_params_iterator = bibformat_engine.pattern_function_params.finditer(all_params) for param_match in function_params_iterator: name = param_match.group('param') if name == "tag": value = param_match.group('value') if not value in format_elements[function_name]['tags']: format_elements[function_name]['tags'].append(value) break keys = format_elements.keys() keys.sort() return map(format_elements.get, keys)
def get_templates_that_use_element(name): """Return a list of format templates that call the given format element. The returned format templates also give their dependencies on tags:: [ {'filename':"filename_1.bft" 'name': "a name" 'tags': ['710__a', '920__'] }, ... ] :param name: a format element name :return: templates sorted by name """ format_templates = {} tags = [] files = os.listdir(CFG_BIBFORMAT_TEMPLATES_PATH) #Retrieve all templates for possible_template in files: if possible_template.endswith(CFG_BIBFORMAT_FORMAT_TEMPLATE_EXTENSION): format_elements = get_elements_used_by_template( possible_template) #Look for elements used in template format_elements = map(lambda x: x['name'].lower(), format_elements) try: #Look for element format_elements.index( name.lower()) #If not found, get out of "try" statement format_template = bibformat_engine.get_format_template( filename=possible_template, with_attributes=True) template_name = format_template['attrs']['name'] format_templates[template_name] = { 'name': template_name, 'filename': possible_template } except: pass keys = format_templates.keys() keys.sort() return map(format_templates.get, keys)
def get_templates_used_by_output(code): """Return a list of templates used inside an output format give by its code. The returned format templates also give their dependencies on elements and tags:: [ {'filename':"filename_1.bft" 'name': "a name" 'elements': [{'filename':"filename_1.py", 'name':"filename_1", 'tags': ['710__a', '920__'] }, ...] }, ... ] :param code: outpout format code :return: templates sorted by name """ format_templates = {} output_format = bibformat_engine.get_output_format(code, with_attributes=True) filenames = map(lambda x: x['template'], output_format['rules']) if output_format['default'] != "": filenames.append(output_format['default']) for filename in filenames: template = bibformat_engine.get_format_template(filename, with_attributes=True) name = template['attrs']['name'] elements = get_elements_used_by_template(filename) format_templates[name] = { 'name': name, 'filename': filename, 'elements': elements } keys = format_templates.keys() keys.sort() return map(format_templates.get, keys)
def get_elements_used_by_template(filename): """ Returns a list of format elements that are called by the given format template. The returned elements also give their dependencies on tags. Dependencies on tag might be approximative. See get_tags_used_by_element() doc string. We must handle usage of bfe_field in a special way if we want to retrieve used tag: used tag is given in "tag" parameter, not inside element code. The list is returned sorted by name:: [ {'filename':"filename_1.py" 'name':"filename_1" 'tags': ['710__a', '920__'] }, ... ] :param filename: a format template filename :return: elements sorted by name """ format_elements = {} format_template = bibformat_engine.get_format_template( filename=filename, with_attributes=True) code = format_template['code'] format_elements_iter = bibformat_engine.pattern_tag.finditer(code) for result in format_elements_iter: function_name = result.group("function_name").lower() if function_name is not None and function_name not in format_elements \ and not function_name == "field": filename = bibformat_engine.resolve_format_element_filename( "BFE_" + function_name) if filename is not None: tags = get_tags_used_by_element(filename) format_elements[function_name] = { 'name': function_name.lower(), 'filename': filename, 'tags': tags } elif function_name == "field": # Handle bfe_field element in a special way if function_name not in format_elements: #Indicate usage of bfe_field if not already done filename = bibformat_engine.resolve_format_element_filename( "BFE_" + function_name) format_elements[function_name] = { 'name': function_name.lower(), 'filename': filename, 'tags': [] } # Retrieve value of parameter "tag" all_params = result.group('params') function_params_iterator = bibformat_engine.pattern_function_params.finditer( all_params) for param_match in function_params_iterator: name = param_match.group('param') if name == "tag": value = param_match.group('value') if not value in format_elements[function_name]['tags']: format_elements[function_name]['tags'].append(value) break keys = format_elements.keys() keys.sort() return map(format_elements.get, keys)
def format_template_show_preview_or_save(req, bft, ln=CFG_SITE_LANG, code=None, ln_for_preview=CFG_SITE_LANG, pattern_for_preview="", content_type_for_preview='text/html', save_action=None, navtrail=""): """ Print the preview of a record with a format template. To be included inside Format template editor. If the save_action has a value, then the code should also be saved at the same time @param req: the request object @param code: the code of a template to use for formatting @param ln: language @param ln_for_preview: the language for the preview (for bfo) @param pattern_for_preview: the search pattern to be used for the preview (for bfo) @param content_type_for_preview: the content-type to use to serve the preview page @param save_action: has a value if the code has to be saved @param bft: the filename of the template to save @param navtrail: navigation trail @return: a web page """ ln = wash_language(ln) _ = gettext_set_language(ln) (auth_code, auth_msg) = check_user(req, 'cfgbibformat') if not auth_code: user_info = collect_user_info(req) uid = user_info['uid'] bft = wash_url_argument(bft, 'str') if save_action is not None and code is not None: #save bibformatadminlib.update_format_template_code(bft, code=code) bibformat_engine.clear_caches() if code is None: code = bibformat_engine.get_format_template(bft)['code'] ln_for_preview = wash_language(ln_for_preview) pattern_for_preview = wash_url_argument(pattern_for_preview, 'str') if pattern_for_preview == "": try: recID = search_pattern(p='-collection:DELETED').pop() except KeyError: return page(title="No Document Found", body="", uid=uid, language=ln_for_preview, navtrail = "", lastupdated=__lastupdated__, req=req, navmenuid='search') pattern_for_preview = "recid:%s" % recID else: try: recID = search_pattern(p=pattern_for_preview + \ ' -collection:DELETED').pop() except KeyError: return page(title="No Record Found for %s" % pattern_for_preview, body="", uid=uid, language=ln_for_preview, navtrail = "", lastupdated=__lastupdated__, req=req) units = create_basic_search_units(None, pattern_for_preview, None) keywords = [unit[1] for unit in units if unit[0] != '-'] bfo = bibformat_engine.BibFormatObject(recID = recID, ln = ln_for_preview, search_pattern = keywords, xml_record = None, user_info = user_info) body = bibformat_engine.format_with_format_template(bft, bfo, verbose=7, format_template_code=code) if content_type_for_preview == 'text/html': #Standard page display with CDS headers, etc. return page(title="", body=body, uid=uid, language=ln_for_preview, navtrail = navtrail, lastupdated=__lastupdated__, req=req, navmenuid='search') else: #Output with chosen content-type. req.content_type = content_type_for_preview req.send_http_header() req.write(body) else: return page_not_authorized(req=req, text=auth_msg)