def parse_entries(self, contents, default_params=None, list_delimiter='/'): """ Return a list of all parameters for instances of a given template. Values are returned in the same format as their default values (a string or a list of strings). If the corresponding default value is a list then "/" (or another specified list_delimiter) in parameter values are interpreted as a separator for list entries. "<small>"-tags are striped from the input. @param content: wikicode @param default_params: dict of expected params and their default values. Values must be either strings or lists. @param list_delimiter: delimiter used as separator for list entries. Defaults to '/'. @return: list of MappingEntry """ default_params = default_params or { 'name': '', 'more': '', 'frequency': '', 'technique': [], 'creator': '', 'wikidata': '', 'link': '', 'category': [], 'other': '' } entries = helpers.get_all_template_entries(contents, self.row_template) units = [] for entry in entries: params = default_params.copy() for key, value in entry.items(): # @todo: consider dropping this or moving to entry cleaner value = value.replace('<small>', '').replace('</small>', '') value = value.strip() # in case of empty <small>-tags if not value: continue if key in params: if isinstance(params[key], list): params[key] = [ v.strip() for v in value.split(list_delimiter) ] else: params[key] = value else: pywikibot.output('Unrecognised parameter: {} = {}'.format( key, value)) unit = MappingEntry(**params.copy()) if unit not in units: # remove identical duplicates units.append(unit) return units
def test_get_all_template_entries_multiple(self): template = 'a' wikitext = '{{a|b=b}} {{a|b=b}} {{a|c}}' expected = [{'b': 'b'}, {'b': 'b'}, {'1': 'c'}] self.assertListEqual(get_all_template_entries(wikitext, template), expected)
def test_get_all_template_entries_single(self): template = 'a' wikitext = '{{a|A|b=b|c={{c|c=pling}}}}' expected = [{'1': 'A', 'c': '{{c|c=pling}}', 'b': 'b'}] self.assertListEqual(get_all_template_entries(wikitext, template), expected)
def test_get_all_template_entries_empty(self): self.assertEqual(get_all_template_entries('', ''), [])
def get_all_template_entries(wikitext, template_name): """Return a list of all arguments for instances of a given template.""" import batchupload.helpers as helpers return helpers.get_all_template_entries(wikitext, template_name)