def time_from_template(template: Template): """ Pulls date-time information encoded by a template and returns a WikiTime object. If date-time information is missing or incomplete, None is returned instead. :param template: A mwparserfromhell Template object with date, time, and timezone parameters :return: a WikiTime object representing the date-time information encoded by this template """ tz_lookup = { 'PST': WikiTime.pst, 'CET': WikiTime.cet, 'KST': WikiTime.kst } if not template.has('date') or not template.has('time'): return None date = template.get("date").value.strip() time = template.get("time").value.strip() if date == '' or time == '': return None # Fix case of a time being written as 100 or 1100 without a : if match(r'\d\d\d\d', time): time = '{}:{}'.format(time[:2], time[3:]) elif match(r'\d\d\d', time): time = '{}:{}'.format(time[:1], time[2:]) tz_local_str = template.get('timezone').value.strip() tz_local = tz_lookup[tz_local_str] date_and_time = date + " " + time return time_from_str(date_and_time, tz=tz_local)
def try_get(template: Template, key: str, default="", warn=True, throw=False) -> str: if template.has(key): return str(template.get(key)) else: if throw: raise ValueError(f"Template {template} doesn't have value defined at index {key}!") if warn: warnings.warn(f"Template {template} doesn't have a word defined at index {key}! This is strange but apparently possible.") return default
def test_has(self): """test Template.has()""" node1 = Template(wraptext("foobar")) node2 = Template(wraptext("foo"), [pgenh("1", "bar"), pgens("\nabc ", "def")]) node3 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) self.assertFalse(node1.has("foobar", False)) self.assertTrue(node2.has(1, False)) self.assertTrue(node2.has("abc", False)) self.assertFalse(node2.has("def", False)) self.assertTrue(node3.has("1", False)) self.assertTrue(node3.has(" b ", False)) self.assertTrue(node4.has("b", False)) self.assertTrue(node3.has("b", True)) self.assertFalse(node4.has("b", True))
def repl_conditional(self, arg: Template, code: Wikicode, index: Union[str, int]): if arg.has(index): param = arg.get(index) self.apply_wikitext(param.value) code.replace( arg, str(param.value).strip() if param.showkey else param.value) else: code.remove(arg)
def flag_template(self: TemplateParser, code: Wikicode, template: Template, flag, index=None): if index and template.has(index): param = template.get(index) self.apply_wikitext(param.value) code.replace(template, param) else: code.remove(template) self.state.flags.add(flag)
def handle_template(tpl, code, namespace=None): if tpl.has('sprache'): if tpl.get('sprache').value.strip().lower() in ('englisch', 'english'): set_param_value(tpl, 'sprache', 'en') if tpl.get('sprache').value.strip().lower() in ('deutsch', 'german'): set_param_value(tpl, 'sprache', 'de') if tpl.has('wann'): if tpl.get('wann').value.strip() in ('Sommersemester', 'ss'): set_param_value(tpl, 'wann', 'SS') elif tpl.get('wann').value.strip() in ('Wintersemester', 'ws'): set_param_value(tpl, 'wann', 'WS') elif tpl.get('wann').value.strip() in ('Winter- und Sommersemester', 'Sommer- und Wintersemester'): set_param_value(tpl, 'wann', 'beide') if tpl.has('tiss'): if tpl.get('tiss').value.strip() == '1234567890': tpl.remove('tiss') archived = False successor = None if tpl.has('veraltet'): archived = True tpl.remove('veraltet') if tpl.has('nachfolger'): archived = True successor = tpl.get('nachfolger').value.strip() tpl.remove('nachfolger') for t in code.ifilter_templates( matches=lambda t: t.name.matches('Veraltet')): archived = True code.remove(t) archivedFlag = code.filter_templates( matches=lambda t: t.name.matches('Archiv')) if archived and not archivedFlag: tpl = Template(Wikicode([Text('Archiv')])) if successor: tpl.add('nachfolger', successor) code.insert(0, tpl) code.insert(1, '\n\n') if tpl.has('zuordnungen'): rels = tpl.get('zuordnungen').value.filter_templates() for rel in rels: if rel.has('2'): rel.get('2').value = str(rel.get('2').value).replace('–', '-') rels.sort(key=lambda x: x.get('1')) tpl.get('zuordnungen').value = '\n' + '\n'.join( [' ' * 4 + str(r) for r in rels]) + '\n' return 'fixe LVA-Daten'
def test_has(self): """test Template.has()""" node1 = Template(wraptext("foobar")) node2 = Template(wraptext("foo"), [pgenh("1", "bar"), pgens("\nabc ", "def")]) node3 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) self.assertFalse(node1.has("foobar", False)) self.assertTrue(node2.has(1, False)) self.assertTrue(node2.has("abc", False)) self.assertFalse(node2.has("def", False)) self.assertTrue(node3.has("1", False)) self.assertTrue(node3.has(" b ", False)) self.assertTrue(node4.has("b", False)) self.assertTrue(node3.has("b", True)) self.assertFalse(node4.has("b", True)) self.assertFalse(node1.has_param("foobar", False)) self.assertTrue(node2.has_param(1, False))
def update_template(self, template: Template): # i just added more and more checks to avoid crashing on LPL games over time if any( [_ in self.current_page.name for _ in ['LPL', 'LDL', 'Demacia']]): return for param in template.params: if '|primary=' in str(param.value): return if not template.has('statslink'): return mh = template.get('statslink').value.strip() if 'lpl.qq' in mh: return if 'wanplus' in mh: return if not mh: return # sigh ok hopefully we actually have a valid mh now print(f'Parsing mh {mh} on page {self.current_page.name}') try: result = get_riot_game(mh, add_names=True) except HTTPError: sleep(10) result = get_riot_game(mh, add_names=True) for param in template.params: param: Parameter if param.name.startswith('blue') or param.name.startswith('red'): # hax, sorry team = 'BLUE' if param.name.startswith('blue') else 'RED' player = int(param.name[-1]) - 1 w = param.value for tl in w.filter_templates(): tl: Template if not tl.name.matches('Scoreboard/Player'): continue primary = result['teams'][team]['players'][player][ 'primaryRuneTreeName'] tl.add('primary', primary, before='secondary')
def handle(site, index): src_ns = next(site.results(prop='info', titles=index))['ns'] for page in site.results(generator='allpages', gapprefix=index.split(':')[1] + '/Beispiel ', gaplimit='max', prop='revisions', rvprop='content', gapnamespace=src_ns): orig = page['revisions'][0]['*'] if mwbot.parse_redirect(orig): continue code = mwbot.parse(orig) templates = code.filter_templates( matches=lambda x: x.name.matches('Beispiel')) if len(templates) > 0: template = templates[0] else: template = Template(Wikicode([Text('Beispiel')])) code.insert(0, template) code.insert(1, '\n') # legacy format handling template.name = 'Beispiel' if template.has('1') and not template.get('1').value.startswith('\n'): template.get('1').value.insert(0, '\n') if template.has('status'): if str(template.get('status').value).strip() not in ('extern', 'Datei'): print('unknown status: {}'.format(k)) if template.has('1'): if str(template.get('1').value).strip() == 'teils': template.add('teils', '') template.remove('1') elif str(template.get('1').value).strip() == 'falsch': template.add('falsch', '') template.remove('1') if not template.has('1'): angabe_div = code.filter_tags( matches=lambda x: x.tag.matches('blockquote') or len([ x for x in x.attributes if '{{Angabe}}' in x or '#EFEFEF' in x ])) if angabe_div: template.add('1', '\n' + str(angabe_div[0].contents).strip() + '\n', showkey=True) code.remove(angabe_div[0]) else: angabe_sec = code.get_sections(matches='Angabe|Aufgabe') if angabe_sec: code.remove(angabe_sec[0].nodes[0]) template.add('1', '\n' + str(angabe_sec[0]).strip() + '\n', showkey=True) code.replace(angabe_sec[0], '\n') mwbot.save(site, page['title'], orig, str(code), 'beispiel_fixer.py', strip_consec_nl=True)