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))
def get_tags_used_by_element(filename): """ Returns a list of tags used by given format element APPROXIMATIVE RESULTS: the tag are retrieved in field(), fields() and control_field() function. If they are used computed, or saved in a variable somewhere else, they are not retrieved @TODO: There is room for improvements. For example catch call to BibRecord functions. :param filename: a format element filename :return: tags sorted by value """ tags = {} format_element = bibformat_engine.get_format_element(filename) if format_element is None: return [] elif format_element['type'] == "field": tags = format_element['attrs']['tags'] return tags filename = bibformat_engine.resolve_format_element_filename(filename) path = bibformat_engine.get_format_element_path(filename) format = open(path, 'r') code = format.read() format.close tags_pattern = re.compile( ''' (field|fields|control_field)\s* #Function call \(\s* #Opening parenthesis [\'"]+ #Single or double quote (?P<tag>.+?) #Tag [\'"]+\s* #Single or double quote (,[^\)]+)* #Additional function param \) #Closing parenthesis ''', re.VERBOSE | re.MULTILINE) tags_iter = tags_pattern.finditer(code) for result in tags_iter: tags[result.group("tag")] = result.group("tag") return tags.values()
def get_tags_used_by_element(filename): """ Returns a list of tags used by given format element APPROXIMATIVE RESULTS: the tag are retrieved in field(), fields() and control_field() function. If they are used computed, or saved in a variable somewhere else, they are not retrieved @TODO: There is room for improvements. For example catch call to BibRecord functions. :param filename: a format element filename :return: tags sorted by value """ tags = {} format_element = bibformat_engine.get_format_element(filename) if format_element is None: return [] elif format_element['type']=="field": tags = format_element['attrs']['tags'] return tags filename = bibformat_engine.resolve_format_element_filename(filename) path = bibformat_engine.get_format_element_path(filename) format = open(path, 'r') code = format.read() format.close tags_pattern = re.compile(''' (field|fields|control_field)\s* #Function call \(\s* #Opening parenthesis [\'"]+ #Single or double quote (?P<tag>.+?) #Tag [\'"]+\s* #Single or double quote (,[^\)]+)* #Additional function param \) #Closing parenthesis ''', re.VERBOSE | re.MULTILINE) tags_iter = tags_pattern.finditer(code) for result in tags_iter: tags[result.group("tag")] = result.group("tag") return tags.values()