示例#1
0
    def backup_template(self, template: Template, page: Union[str, Page],
                        key: Union[str, List[str]]):
        """
        Backs up a template in the `Backup` namespace. The template can later be restored with `get_restored_template`.
        :param template: Template object
        :param page: Page or title where the template is located on
        :param key: Identifying set of params that we can use to locate the template when we restore it
        :return: null
        """
        if isinstance(page, str):
            page = self.client.pages[page]
        if isinstance(key, str):
            key = [key]
        key_template = Template('BackupKey')
        for key_param in key:
            key_template.add(key_param,
                             template.get(key_param, Parameter('', '')).value)

        # this method will be used in TemplateModifier so it is essential that
        # we do not modify the original
        copy_template = copy.deepcopy(template)
        copy_template.add('backup_key', str(key_template))
        self.client.pages['Backup:' + page.name].append('\n' +
                                                        str(copy_template),
                                                        contentmodel='text')
示例#2
0
    def test_loc_objloc(self):
        loc = Template('Object location')
        loc.add(1, "one")
        set_location(self.tree, None)
        set_object_location(self.tree, loc)
        # Might be better without the spurious newline, but this will do.
        self.assertEqual(
            str(self.tree), """
== {{int:filedesc}} ==
{{Information
|description={{en|1=Street Waste Bin Waste bin on the street outside Crosby and Blundellsands Station}}
|date=2010-04-11
|source=From [http://www.geograph.org.uk/photo/1801330 geograph.org.uk]
|author=[http://www.geograph.org.uk/profile/46411 Paul Glover]
|permission=
|other_versions=
}}
{{Object location|one}}


== {{int:license-header}} ==
{{Geograph|1801330|Paul Glover}}

[[Category:Streets in Sefton]]
[[Category:Geograph images in Merseyside]]
""")
示例#3
0
 def test_objloc(self):
     loc = Template('Object location')
     loc.add(1, "one")
     set_object_location(self.tree, loc)
     self.assertEqual(
         str(self.tree),
         "{{Information}}\n{{location dec}}\n{{Object location|one}}")
示例#4
0
    def format(self, citation: Citation):
        tname = CiteTemplate.TEMPLATE_FALLBACK.get(
            citation.type, CiteTemplate.TEMPLATE_FALLBACK['default'])
        template = Template(tname)

        for fragment in CiteTemplate.ORDER:
            func = getattr(self, '_fragment_' + fragment, None)
            if func:
                func(template, citation)
            elif fragment in citation:
                template.add(fragment, str(citation[fragment]))

        return str(template)
示例#5
0
    def format(self, citation: Citation):
        tname = CiteTemplate.TEMPLATE_FALLBACK.get(
            citation.type,
            CiteTemplate.TEMPLATE_FALLBACK['default']
        )
        template = Template(tname)

        for fragment in CiteTemplate.ORDER:
            func = getattr(self, '_fragment_' + fragment, None)
            if func:
                func(template, citation)
            elif fragment in citation:
                template.add(fragment, str(citation[fragment]))

        return str(template)
示例#6
0
    def to_mediawiki_template(self, maccabi_games: MaccabiGamesStats) -> str:
        tag_paper_template = Template(f'{_PAPER_TAG_TEMPLATE_NAME}\n')

        tag_paper_template.add(_PAPER_NAME_PARAM_NAME, f'{self.paper_name}\n')

        if self.paper_publish_date is not None:
            tag_paper_template.add(
                _PAPER_PUBLISH_DATE_PARAM_NAME,
                f'{self.paper_publish_date.strftime("%d-%m-%Y")}\n')

            if math.fabs((self.paper_publish_date -
                          self.paper_related_game_date).days) > 7:
                raise RuntimeError(
                    f'There is more than a week between the publish and the game date, is it an error?'
                )

        potential_games = maccabi_games.played_at(self.paper_related_game_date)
        if len(potential_games) != 1:
            raise RuntimeError(
                f'Could not match exactly one game for date: {self.paper_related_game_date}, '
                f'found these games: {potential_games}')

        game_page_name = _generate_page_name_from_game(potential_games[0])
        tag_paper_template.add(_PAPER_RELATED_GAME_PARAM_NAME,
                               f'{game_page_name}')

        return str(tag_paper_template)
示例#7
0
def location_from_grid(grid,
                       e,
                       n,
                       digits,
                       view_direction,
                       use6fig,
                       mapit=None):
    latstr, lonstr, prec = latlon_from_grid(grid, e, n, digits, use6fig)
    precstr = "{:g}".format(prec)
    paramstr = "source:" + source_from_grid(grid, e, n, digits)
    region = region_of(grid, e, n, latstr, lonstr, mapit)
    if region != None:
        paramstr += "_region:{}".format(region)
    if view_direction != None:
        paramstr += "_heading:{}".format(view_direction)
    t = Template(mwparserfromhell.parse('Location'))
    t.add(1, latstr)
    t.add(2, lonstr)
    t.add(3, paramstr)
    t.add('prec', precstr)
    return t
示例#8
0
 def test_loc(self):
     loc = Template('Location')
     loc.add(1, "one")
     set_location(self.tree, loc)
     self.assertEqual(str(self.tree),
                      "{{Location|one}}\n{{object location}}")