示例#1
0
    def add(self, name, fields):
        """Add a service.

        Args:
            name (string): The name of the service.
            fields (dict): The service fields to add.

        Returns:
            :obj:`enum 'ServiceErrorCode'`: A ServiceErrorCode enum for cases
                such as if the service already exists, etc.
            dict: the service information if fetch_bpmn_digital_steps is None,
        """
        page, page_exists = self._page(name)
        if page_exists:
            result = ErrorCode.ALREADY_EXISTS
        elif not page.can('edit'):
            result = ErrorCode.UNAUTHORIZED_ACTION
        else:
            templates_text = ''
            for tpl_name, tpl_instances in fields.items():
                for tpl_instance in tpl_instances:
                    templates_text += _template_text(tpl_name, tpl_instance)
            te = TemplateEditor(templates_text)
            if te.templates:
                page.edit(te.wikitext())
                page_name = page.page_title
                page_full_name = page.name
                result = self._service_dict(page_name, page_full_name, te)
            else:
                result = ErrorCode.INVALID_TEMPLATE
        return result
 def test_edit_anonymous_parameter2(self):
     # Check that an anomyous parameter can be edited, preserving declaration
     text = 'Lorem ipsum {{lang|1=fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['lang'][0].parameters[1] = 'no'
     tmp = text.replace('fr', 'no')
     self.assertEqual(dp.wikitext(), tmp)
 def test_add_parameter3(self):
     # Check that a parameter can be added, and that an existing whitespace pattern is followed
     text = 'Lorem ipsum {{ Infoboks A | a = 2 }} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 }} dolor sit amet'
     dp = TemplateEditor(text)
     dp.templates['infoboks A'][0].parameters['b'] = '3'
     self.assertEqual(dp.wikitext(), text2)
 def test_add_parameter2(self):
     # Check that a parameter can be added, and that an existing whitespace pattern is followed
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n| dato = TEST \n}} dolor sit amet'
     dp = TemplateEditor(text)
     dp.templates['infoboks A'][0].parameters['dato'] = 'TEST'
     self.assertEqual(dp.wikitext(), text2)
 def test_edit_parameter_using_value_that_needs_xml_escape(self):
     # Check that "&" in values are properly handled
     text = 'Lorem ipsum {{kilde www}} amet'
     result = 'Lorem ipsum {{kilde www| url=http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201&currentPage=83 }} amet'
     dp = TemplateEditor(text)
     dp.templates['kilde www'][0].parameters['url'] = 'http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201&currentPage=83'
     self.assertEqual(dp.wikitext(), result)
 def test_alter_anonymous_param1(self):
     # Check that an anomyous parameter can be edited correctly
     text = 'Lorem ipsum {{lang|fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['lang'][0].parameters[1] = 'no'
     tmp = text.replace('fr', 'no')
     self.assertEqual(dp.wikitext(), tmp)
 def test_delete_template(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit{{lang|fr}} amet'
     text2 = 'Lorem ipsum dolor sit{{lang|fr}} amet'
     dp = TemplateEditor(text)
     dp.templates['Infoboks A'][0].remove()  # Note that 'del dp.templates['Infoboks A'][0]' currently does not work
     self.assertEqual(dp.wikitext(), text2)
 def test_change_template_name(self):
     # Check that a template can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks=2\n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.name = 'Mal B'
     tmp = text.replace('Infoboks A', 'Mal B')
     self.assertEqual(dp.wikitext(), tmp)
 def test_edit_empty_parameter_with_linebreak(self):
     # Check that an empty parameter can be edited, with linebreak preserved
     text = 'Lorem ipsum {{ Infoboks A \n| date= \n| title= \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['date'] = 'test'
     tmp = text.replace('date= ', 'date= test')
     self.assertEqual(dp.wikitext(), tmp)
示例#10
0
def check_randompage(pagename):
    logger.debug('Page: %s', pagename)
    page = site.pages[pagename]
    inputtxt = page.edit(readonly=True)
    dp = TemplateEditor(inputtxt)
    outputtxt = dp.wikitext()

    assert inputtxt == outputtxt
 def test_edit_nonempty_parameter_alternative(self):
     # Check that a parameter can be renamed using 'parameter.value='
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['maks'].value = '3'
     tmp = text.replace('2', '3')
     self.assertEqual(dp.wikitext(), tmp)
 def test_edit_parameter_using_integer_alternative(self):
     # Check that a parameter can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['maks'].edit(3)
     tmp = text.replace('2', '3')
     self.assertEqual(dp.wikitext(), tmp)
 def test_delete_parameter(self):
     # Check that a parameter can be renamed, with whitespace preserved
     text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A\n}} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     del templ.parameters['maks']
     self.assertEqual(dp.wikitext(), text2)
 def test_delete_parameter_alternative(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit amet'
     text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | c = 4 }} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters.remove('b')
     self.assertEqual(dp.wikitext(), text2)
 def test_add_parameter3(self):
     # Check that a parameter can be added, when there's no existing parameters
     text = 'Lorem ipsum {{Infoboks A}} dolor sit amet'
     text2 = 'Lorem ipsum {{Infoboks A| a=2 | b=3 }} dolor sit amet'
     dp = TemplateEditor(text)
     templ = dp.templates['infoboks A'][0]
     templ.parameters['a'] = '2'
     templ.parameters['b'] = '3'
     self.assertEqual(dp.wikitext(), text2)
    def __init__(self, page, interactive_mode):

        logger.info('Checking page: %s', page.name)

        self.checked = 0
        self.modified = []
        self.unresolved = []

        # te = page.text()
        txt = page.text()

        # Due to <https://github.com/danmichaelo/mwtemplates/issues/3>
        if re.search('<nowiki ?/>', txt, re.I) is not None:
            return

        te = TemplateEditor(txt)

        modified = False
        for k, v in te.templates.iteritems():
            if k in ['Kilde www', 'Kilde bok', 'Kilde artikkel', 'Kilde avhandling', 'Kilde avis', 'Cite web', 'Citeweb', 'Cite news', 'Cite journal', 'Cite book', 'Tidningsref', 'Webbref', 'Bokref']:
                for tpl in v:
                    t = Template(tpl, interactive_mode)
                    self.checked += t.checked
                    self.modified.extend(t.modified)
                    self.unresolved.extend(t.unresolved)

        for u in self.unresolved:
            u['page'] = page.name

        if len(self.modified) != 0:
            if len(self.modified) == 1:
                summary = 'CS1-kompatible datoer: %s' % (self.format_entry(self.modified[0]))
            elif len(self.modified) == 2:
                summary = 'CS1-kompatible datoer: %s, %s' % (self.format_entry(self.modified[0]), self.format_entry(self.modified[1]))
            else:
                summary = 'CS1-kompatible datoer: Fikset %d datoer' % (len(self.modified))

            logger.info('Saving %d fixed date(s)', len(self.modified))
            time.sleep(1)

            try:
                res = page.save(te.wikitext(), summary=summary)
            except mwclient.errors.ProtectedPageError:
                logger.error('ERROR: Page protected, could not save')

            if res.get('newrevid') is not None:
                with codecs.open('modified.txt', 'a', 'utf8') as f:
                    for x in self.modified:
                        ti = quote(page.name.replace(' ', '_').encode('utf8'))
                        difflink = '//no.wikipedia.org/w/index.php?title=%s&diff=%s&oldid=%s' % (ti, res['newrevid'], res['oldrevid'])
                        f.write('| [[%s]] ([%s diff]) || Endret %s fra %s til %s || %s\n|-\n' % (page.name, difflink, x['key'], x['old'], x['new'], 'kompleks' if x['complex'] else ''))

                with codecs.open('modified-simple.txt', 'a', 'utf8') as f:
                    for x in self.modified:
                        ti = quote(page.name.replace(' ', '_').encode('utf8'))
                        f.write('%s\t%s\t%s\n' % (page.name, x['old'], x['new']))
示例#17
0
 def template_text(org_details):
     te = TemplateEditor(self.TEMPLATE)
     template = te.templates[self.TEMPLATE_NAME][0]
     # Add details to template parameters
     for key in self.TEMPLATE_FIELD_NAME_SUFFIXES:
         if '_' in key:
             details_keys = key.split('_')
         else:
             details_keys = None
         if details_keys is None:
             value = org_details.get(key, None)
         else:
             value = org_details.get(details_keys[0],
                                     {}).get(details_keys[1], None)
         if value is not None:
             if isinstance(value, list):
                 value = ','.join(value)
             value = escape(str(value))
             # Clean up telephone value
             if key == self.TEMPLATE_CONTACT_POINT_TELEPHONE:
                 value = value.replace(' ', '').replace('+30', '')
                 new_value = ''
                 for c in value:
                     if not c.isdigit():
                         break
                     new_value += c
                 value = new_value
             template.parameters[
                 f'{self.TEMPLATE_PARAM_PREFIX}{key}'] = value
     return str(template).replace(' |', '|')
示例#18
0
def user_warnings_extractor(text: str) -> Iterator[CaptureResult[UserWarning]]:

    # use template extractor from mwtemplates
    try:
        uwarnings = TemplateEditor(text)
    except:
        # there can be different type of text which are not supported by mwtemplates
        return user_warnings_extractor_handcrafted(text)


    # for each unique template name
    for key in uwarnings.templates.keys():

        # template name
        template_name = key.lower().strip()
        
        # associated language
        lang = None
        
        if template_name in italian_user_warnings:
            lang = 'it'
        elif template_name in catalan_user_warnings:
            lang = 'ca'
        elif template_name in spanish_user_warnings:
            lang = 'es'
        elif template_name in english_user_warnings:
            lang = 'en'

        # check if the template is needed or not
        if not lang:
            continue

        # for each template named key occurence
        for template in uwarnings.templates[key]:

            # basic parameters
            user_warnings_obj = UserWarning(template_name, lang, dict(), False, template_category_mapping[template_name])

            # Counter for positional arguments
            positional_counter = 1  # NOTE: starting from 1, as the first argument is 1 in the wikimarkup

            # for each parameter
            for i, parameters in enumerate(template.parameters):
                
                # named parameter
                if len(parameters.name.strip()) > 0:
                    user_warnings_obj.options[parameters.name] = parameters.value
                else:
                    # positional parameter
                    user_warnings_obj.options[positional_counter] = parameters.value
                    positional_counter += 1

                user_warnings_obj.at_least_one_parameter = True

            yield CaptureResult(
                data=(user_warnings_obj), span=None
            )
示例#19
0
文件: main.py 项目: Emojigit/wp_noref
def refURL(text):
    if "{{" not in text:
        return False
    te = TemplateEditor(text)
    tps = te.templates
    tp = tps[tps.keys()[0]][0]
    try:
        return str(tp.parameters['url'])
    except KeyError:
        return False
示例#20
0
文件: sdbot.py 项目: icaas/SDBot
    def remove_template(self, name, page_nom, page_subject):

        backlinks = page_nom.backlinks(generator = False, redirect = True)
        if page_subject.name in backlinks:
            logger.info('   Fjerner {{Sletting}} fra %s' % page_subject.name)

            text = page_subject.text()
            try:
                dp = TemplateEditor(text)
                if 'Sletting' in dp.templates:
                    dp.templates['Sletting'][0].remove()
                elif 'Slett' in dp.templates:
                    dp.templates['Slett'][0].remove()
                elif 'Slettingfordi' in dp.templates:
                    dp.templates['slettingfordi'][0].remove()
                else:
                    logger.info('  Fant ingen slettemal')
                    return
                text = dp.get_wikitext()
                text = text.strip()
                logger.info('  vha. TemplateEditor')
            except:
                text = re.sub(r'\{\{[Ss]lett.+?\}\}', '', text) # this may fail if template contains subtemplates
                logger.info('  vha. regexp')

            sleeper = sleepers.make()
            while True:
                try:
                    summary = 'Beholdt etter [[%s|slettediskusjon]]' % name
                    if self.simulate:
                        print "--------" + page_subject.name + ": " + summary + "------------"
                        print text
                    else:
                        page_subject.save(text, summary = summary)
                except mwclient.EditError, e:
                    try:
                        sleeper.sleep()
                    except mwclient.MaximumRetriesExceeded:
                        return logger.error('Unable to save page')
                else:
                    return True
示例#21
0
def wikibreaks_extractor(text: str) -> Iterator[CaptureResult[Wikibreak]]:

    # use template extractor from mwtemplates
    try:
        wbreaks = TemplateEditor(text)
    except:
        # there can be different type of text which are not supported by mwtemplates
        return wikibreaks_extractor_handcrafted(text)

    # for each unique template name
    for key in wbreaks.templates.keys():

        wiki_name = key.lower().strip()
        # check if the template is one of the desired ones
        if not wiki_name in all_wikibreaks_words:
            continue

        # for each template named key occurence
        for template in wbreaks.templates[key]:

            # basic parameters
            wikimap = wikibreaks.wikibreak_fields_to_tuple_category_subcategory[
                wiki_name]
            wiki_category = wikimap['category']
            wiki_sub_category = wikimap['subcategory']
            wikibreak_obj = Wikibreak(wiki_name, wiki_category,
                                      wiki_sub_category, dict(), False)

            # Counter for positional arguments
            positional_counter = 1  # NOTE: starting from 1, as the first argument is 1 in the wikimarkup

            # for each parameter
            for i, parameters in enumerate(template.parameters):

                # named parameter
                if len(parameters.name.strip()) > 0:
                    wikibreak_obj.options[
                        parameters.name.strip()] = parameters.value
                else:
                    # positional parameter
                    wikibreak_obj.options[
                        positional_counter] = parameters.value
                    positional_counter += 1

                wikibreak_obj.at_least_one_parameter = True

            yield CaptureResult(data=(wikibreak_obj), span=None)
示例#22
0
def parse_wiki():
    DATA_PATH.mkdir(parents=True, exist_ok=True)
    csv_file = DATA_PATH.joinpath(datetime.today().strftime('%y_%m_%d.data'))
    if csv_file.exists():
        with csv_file.open() as f:
            return read_csv(f)

    site = mwclient.Site('en.wikipedia.org')
    page = site.pages[
        u'Template:COVID-19_pandemic_data/Ukraine_medical_cases_chart']
    assert page.exists
    template_data = page.text()
    te = TemplateEditor(template_data)
    template = te.templates['Medical cases chart'][0]
    csv_data = template.parameters['data'].value
    csv_data = re.sub("(<!--.*?-->)", "", csv_data, flags=re.DOTALL).strip()

    with csv_file.open('w') as f:
        f.write(csv_data)
        return read_csv(csv_data.split('\n'))
示例#23
0
    def fetch_by_name(self, name, fetch_bpmn_digital_steps=None):
        """Fetch a service by name.

        Args:
            name (string): The name of the service.
            fetch_bpmn_digital_steps (bool): Whether to fetch digital or
                manual bpmn data. If None, do not return bpmn data.
                Default: None.

        Returns:
            :obj:`enum 'ServiceErrorCode'`: A ServiceErrorCode.NOT_FOUND
                enum, if the service is not found,
            dict: service information if `fetch_bpmn_digital_steps` is None,
            string: otherwise, the BPMN XML of the service.
        """
        self._site.login(auto=True)
        page, page_exists = self._page(name)
        if page_exists:
            page = page.resolve_redirect()
            page_name = page.page_title
            page_full_name = page.name
            current_revision = page.revisions(limit=1, dir='older').next()
            latest_update_date = datetime.utcfromtimestamp(
                mktime(current_revision['timestamp'])).isoformat()
            page_id = page._info['pageid']
            service_dict = self._service_dict(page_name, page_full_name,
                                              TemplateEditor(page.text()))
            if fetch_bpmn_digital_steps is None:
                data = service_dict
            else:
                data = BPMN(digital_steps=fetch_bpmn_digital_steps).xml(
                    service_dict).replace('\n',
                                          '').replace('\t',
                                                      '').replace('\"', '\'')

            # data = {**data, **{"update":latest_update_date,"page_id":page_id}}
            # data = {'data': data, "update":latest_update_date,"page_id":page_id}
            result = data
        else:
            result = ErrorCode.NOT_FOUND
        return result
def main(outfile, namespace=PUBLISHED_NAMESPACE):
    if not outfile.endswith(CSV_EXTENSION):
        outfile += CSV_EXTENSION
    site = Site()
    site.login(auto=True)
    services_category = site.categories['Κατάλογος Διαδικασιών']

    with open(outfile, 'w') as f:
        csv_output = csv.DictWriter(f, fieldnames=header_row)

        csv_output.writeheader()

        for page in services_category:
            name = page.name
            split_name = name.split(':')
            ns = split_name[0]
            if ns == namespace:  # print only selected Namespace
                service_dict = _service_dict(TemplateEditor(page.text()), ns)
                # print(service_dict)
                csv_output.writerow(service_dict)

        f.close()
示例#25
0
文件: parse.py 项目: sheleh/deep
def parse_wiki(country):
    DATA_PATH.mkdir(parents=True, exist_ok=True)
    csv_file = DATA_PATH.joinpath(country + '_' + datetime.today().strftime('%y_%m_%d.data'))
    site = mwclient.Site('en.wikipedia.org')
    page = site.pages[u'Template:COVID-19_pandemic_data/{}_medical_cases_chart'.format(country)]

    assert page.exists
    template_data = page.text()

    te = TemplateEditor(template_data)
    template = te.templates['Medical cases chart'][0]
    csv_data = template.parameters['data'].value
    csv_data = re.sub("(<!--.*?-->)", "", csv_data, flags=re.DOTALL)
    # print(csv_data)
    csv_data = csv_data.replace('+', ';;;')
    # print(csv_data)
    csv_data = re.sub("(;;;;;;.*?\n$)", "", csv_data, flags=re.DOTALL)
    csv_data = re.sub("(;;;.*?\n$)", "", csv_data, flags=re.DOTALL)
    # csv_data = re.sub("/\w++\..*/", "", csv_data, flags=re.DOTALL).strip()
    # csv_data = re.sub("+.*?}}", "", csv_data, flags=re.DOTALL).strip()
    #print(csv_data)
    with csv_file.open('w') as f:
        f.write(csv_data)
 def test_arabic(self):
     # Check handling of multibyte chars, unicode level 2
     text = 'Lorem ipsum {{Infoboks العربية}} dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())
示例#27
0
    def update(self, name, fields):
        """Update a service.

        Args:
            name (string): The name of the service.
            fields (dict): The service fields to update.

        Returns:
            :obj:`enum 'ServiceErrorCode'`: A ServiceErrorCode enum for cases
                such as if the service was not found, etc.
            dict: the service information if fetch_bpmn_digital_steps is None,
        """
        page, page_exists = self._page(name)
        if not page.can('edit'):
            result = ErrorCode.UNAUTHORIZED_ACTION
        elif page_exists:
            te = TemplateEditor(page.text())
            fields_updated = False
            for tpl_name, tpl_instances in fields.items():
                template_names = [tpl.lower() for tpl in te.templates.keys()]
                if tpl_name in template_names:
                    page_tpl_instances = te.templates[tpl_name]
                    # Update template instances
                    for instance_num_str, tpl_instance in \
                            tpl_instances.items():
                        if not instance_num_str.isdigit():
                            break
                        instance_num = int(instance_num_str)
                        try:
                            page_tpl = page_tpl_instances[instance_num - 1]
                        except IndexError:
                            # Template instance does not exist, create it
                            if instance_num > len(page_tpl_instances):
                                # Only if numbering is greater than the last
                                # existing instance number
                                new_tpl_text = _template_text(
                                    tpl_name, tpl_instance)
                                if new_tpl_text:
                                    new_wiki_text = ''
                                    for name, tpls in te.templates.items():
                                        for tpl in tpls:
                                            new_wiki_text += f'{str(tpl)}\n'
                                        if name.lower() == tpl_name:
                                            new_wiki_text += \
                                                f'{new_tpl_text}'
                                    if new_wiki_text:
                                        te = TemplateEditor(new_wiki_text)
                                        if not fields_updated:
                                            fields_updated = True
                        else:
                            for field_name, field_value in \
                                    tpl_instance.items():
                                # Update or create field
                                page_tpl.parameters[field_name] = \
                                    field_value
                                if not fields_updated:
                                    fields_updated = True
                else:
                    new_templates_text = ''
                    for instance_num_str, tpl_instance in \
                            tpl_instances.items():
                        new_templates_text += _template_text(
                            tpl_name, tpl_instance)
                    if new_templates_text:
                        # Append new templates to wiki text
                        new_wiki_text = \
                            f'{te.wikitext()}\n{new_templates_text}'
                        te = TemplateEditor(new_wiki_text)
                        if not fields_updated:
                            fields_updated = True
            if fields_updated:
                wikitext = te.wikitext().replace('\n\n', '\n')
                if wikitext[0] == '\n':
                    wikitext = wikitext[1:]
                page.edit(wikitext)
                page_name = page.page_title
                page_full_name = page.name
                result = self._service_dict(page_name, page_full_name,
                                            TemplateEditor(page.text()))
            else:
                result = ErrorCode.NO_FIELD_UPDATED
        else:
            result = ErrorCode.NOT_FOUND
        return result
示例#28
0
log('@ ktitle is %s' % ktitle)
pp = homesite.api('query', prop='pageprops', titles=ktitle, redirects='1')
if 'redirects' in pp['query']:
    ktitle = pp['query']['redirects'][0]['to']
    log('  -> Redirected to:  %s' % ktitle)

kpage = homesite.pages[ktitle]
if not kpage.exists:
    log('  !! kpage does not exist! Exiting')
    sys.exit(0)

ibcfg = config['templates']['infobox']
commonargs = config['templates']['commonargs']

dp = TemplateEditor(kpage.text())
try:
    infoboks = dp.templates[ibcfg['name']][0]
except:
    log(' !! Fant ikke infoboks!')
    sys.exit(0)

if infoboks.has_param(commonargs['year']) and infoboks.has_param(
        commonargs['week']):
    year = int(
        re.sub(ur'<\!--.+?-->', ur'',
               unicode(infoboks.parameters[commonargs['year']])).strip())
    startweek = int(
        re.sub(ur'<\!--.+?-->', ur'',
               unicode(infoboks.parameters[commonargs['week']])).strip())
    if infoboks.has_param(commonargs['week2']):
 def test_xml(self):
     text = 'Lorem {{Ipsum|dolor|sit=amet}}'
     te = TemplateEditor(text)
     xml = te.xml()
     self.assertEqual(xml, '<root>Lorem <template><title>Ipsum</title><part><name index="1"/><value>dolor</value></part><part><name>sit</name>=<value>amet</value></part></template></root>')
 def test_nested_templates(self):
     text = 'Lorem{{templ1|arg={{templ2|20}}}}'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())
 def test_find_param_3(self):
     # Check using get_anonymous_parameters method
     text = 'Lorem ipsum {{lang|fr}} dolor sit amet'
     tpl = TemplateEditor(text).templates['lang'][0]
     params = tpl.get_anonymous_parameters()
     self.assertEqual(params[1], 'fr')
 def test_blank_template_roundtrip(self):
     # Check that a parameter can be deleted
     text = 'Lorem ipsum {{ }} dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(dp.wikitext(), text)
 def test_nowiki_roundtrip(self):
     # Check that <nowiki/> is not changed to <nowiki>
     text = 'Lorem ipsum <nowiki/> dolor sit amet'
     dp = TemplateEditor(text)
     self.assertEqual(dp.wikitext(), text)
 def test_onlywhitespace(self):
     # Check that the parser doesn't strip off empty parameters:
     text = '{{some template |   | someval=true}}'
     dp = TemplateEditor(text)
     self.assertEqual(text, dp.wikitext())
 def test_edit_nowiki(self):
     text = '<nowiki>{{flagg|no}}</nowiki>{{flagg|%s}}'
     dp = TemplateEditor(text % 'no')
     dp.templates['flagg'][0].parameters[1] = 'en'
     self.assertEqual(dp.wikitext(), text % 'en')
示例#36
0
文件: ffbot.py 项目: tholme/FLFBot
def main(catname, pagename, what, templates, table):
    pages = [c for c in no.categories[catname]]
    entries = []
    added = []
    removed = []
    template = templates[0]

    for p in pages:
        cur.execute(
            'SELECT target, target2, date, revid, parentid, user, comment, reason FROM %s WHERE page=?'
            % table, [p.name])
        s = cur.fetchall()
        in_db = (len(s) > 0)
        logger.info('article: %s', p.name)
        if in_db:
            rev = {
                'to': s[0][0],
                'to2': s[0][1],
                'date': datetime.strptime(s[0][2], '%Y-%m-%d'),
                'id': s[0][3],
                'parent': s[0][4],
                'user': s[0][5],
                'comment': s[0][6],
                'reason': s[0][7]
            }

        else:
            dp = TemplateEditor(p.text())
            t = None
            for tpl in templates:
                if tpl in dp.templates:
                    t = dp.templates[tpl][0]
                    break
            if t == None:
                logger.warn("> fant ikke noen mal")
                continue
            if not 1 in t.parameters:
                logger.warn(" > Ingen parametre gitt til malen!")
                continue

            logger.info(' -> %s', t.parameters[1])
            fra = '[[%s]]' % p.name

            rev = find_rev(p.name, templates)
            if rev == False:
                logger.warn(' fant ikke innsettingsrevisjonen for malen')
                continue

            rev['to'] = t.parameters[1].value.strip('[]')
            rev['to2'] = ''
            rev['reason'] = ''
            if 2 in t.parameters:
                logger.info(' begrunnelse: %s', t.parameters[2])
                rev['reason'] = t.parameters[2].value
            elif 'begrunnelse' in t.parameters:
                logger.info(' begrunnelse: %s', t.parameters['begrunnelse'])
                rev['reason'] = t.parameters['begrunnelse'].value

            if 'alternativ' in t.parameters:
                rev['to2'] += t.parameters['alternativ'].value

            vals = [
                p.name, rev['to'], rev['to2'], rev['date'].strftime('%F'),
                rev['id'], rev['parent'], rev['user'], rev['comment'],
                rev['reason']
            ]
            cur.execute(
                'INSERT INTO %s (page, target, target2, date, revid, parentid, user, comment, reason) VALUES (?,?,?,?,?,?,?,?,?)'
                % table, vals)
            added.append(p.name)

        #begrunnelse = "<span style='color:#999;'>''Ikke gitt''</span>"

        q = {
            'title': p.name.encode('utf-8'),
            'oldid': rev['id'],
            'diff': 'prev'
        }
        link = '[%s Foreslått]' % (no.site['server'] + no.site['script'] +
                                   '?' + urllib.urlencode(q))
        #submitter = ''<br />%s' % (rev['user'], rev['user'], link)

        entry = ''
        if len(rev['reason']) != 0:
            entry += '<abbr style="color: #999; " title="Begrunnelse i mal">B:</abbr> %s<br />' % rev[
                'reason']
        if len(rev['comment']) != 0:
            entry += '<abbr style="color: #999; " title="Redigeringsforklaring">R:</abbr> <nowiki>%s</nowiki><br />' % rev[
                'comment']
        entry += "<small>''%s av [[Bruker:%s|%s]] den %s''</small>" % (
            link, rev['user'], rev['user'], rev['date'].strftime('%e. %B %Y'))

        fra = '[[:%s]]' % p.name
        til = '[[:%s]]' % rev['to']
        if len(rev['to2']) != 0:
            til += '<br />&nbsp;&nbsp; eller [[%s]]' % rev['to2']

        text = '|-\n| %s<br /> → %s \n| %s \n' % (fra, til, entry)
        entries.append([rev['date'], text])

        #time.sleep(1)

    sql.commit()

    # Remove processed entries from DB

    pnames = [p.name for p in pages]
    for row in cur.execute('SELECT page FROM %s' % table):
        n = row[0]
        if not n in pnames:
            removed.append(n)
    for n in removed:
        logger.info("Page %s found in db, but not in cat. Removing from db", n)
        cur.execute('DELETE FROM %s WHERE page=?' % table, [n])

    sql.commit()

    # Prepare output

    entries.sort(key=lambda x: x[0])
    text = '\n'.join([
        '<noinclude>',
        '{{Bruker:FLFBot/robotinfo|%s}}' % template, '</noinclude>',
        '{| class="wikitable"',
        '|+ Sider merket for flytting vha. {{ml|%s}}' % template,
        '! Forslag !! Begrunnelse \n' + ''.join([e[1]
                                                 for e in entries]) + '|}',
        '[[Kategori:Wikipedia-vedlikehold|%s]]' % what
    ])

    summary = []
    if len(added) == 1:
        summary.append('Nytt %s: %s' % (what.lower(), added[0]))
    elif len(added) > 1:
        summary.append('%d nye %s' % (len(added), what.lower()))
    if len(removed) == 1:
        summary.append('%s behandlet: %s' % (what, removed[0]))
    elif len(removed) > 1:
        summary.append('%d %s behandlet' % (len(removed), what.lower()))

    if len(added) == 0 and len(removed) == 0:
        pass
        logger.info("Ingen endringer, avslutter")
    else:
        if pagename == None:
            print(text)
        else:
            page = no.pages[pagename]
            page.edit()
            page.save(text, ', '.join(summary))
            logger.info('Oppdaterte %s' % pagename)