예제 #1
0
    def change_content_action(self, page, content, **kwargs):
        # print self.i
        self.save_step()
        title = page.title
        if u':' in title:
            return content
        new_content = content
        # morph = u'прил'
        morph = u'гл'
        lang = u'uk'
        parts = get_inflection_templates(content, morph, lang)

        if parts:
            # print title
            for part in parts:
                ok = False
                new_tpl = old_tpl = part['tpl']
                # print old_tpl
                if u'|слоги={{' not in old_tpl:
                    # if u'{{' in old_tpl[3:-3]:
                    #     print title
                    #     print old_tpl
                    # print title
                    # print old_tpl
                    # print
                    m = re.search(u'\|слоги=([^|}\s]*)', old_tpl)
                    if m:
                        value = m.group(1)
                        check_value = value.replace(u'́', u'').replace(u'-', u'')
                        if title.replace(u'-', u'') != check_value:
                            # print title, value
                            raise Exception()
                        else:
                            new_tpl = new_tpl.replace(
                                u'|слоги=%s' % value,
                                u'|слоги={{по-слогам|%s}}' % value
                            )
                            ok = True
                            # print title, value
                            # print title
                            # print old_tpl
                            # print new_tpl
                            # print u'---'
                if u'|соотв=' not in new_tpl:
                    new_tpl = new_tpl.replace(u'|show-text=1', u'|соотв=')
                if old_tpl != new_tpl: # and ok:
                    new_content = new_content.replace(old_tpl, new_tpl)
                    # print title
                #     print title
                #     print old_tpl
                #     print new_tpl
                #     print
            # print
        # return content
        if new_content != content:
            new_content = default_wikifier(new_content)
        return new_content
예제 #2
0
 def additional_change_content(self, content):
     content = super(DefaultWikifierMixin, self).\
         additional_change_content(content)
     return default_wikifier(content)
예제 #3
0
    def section_action(self, page, lang, section_content):
        super(ReplaceOldMorphoRu, self).section_action(page, lang, section_content)
        title = page.title
        if lang != 'ru':
            return
        m = re.search(u'(=== *Морфологические и синтаксические свойства *=== *(.*?)\n)=',
                      section_content, re.UNICODE | re.DOTALL)
        if not m:
            return
        old_body = m.group(2)
        p = re.compile(
            u"""(\{\{
                (?P<title>морфо\s*)  # заголовок
                \|(?P<params>[^{}]+?(\{\{[^{}]+?\}\})?[^{}]+?)  # параметры
                \}\})""",
            flags=re.UNICODE + re.DOTALL + re.VERBOSE)
        parts = p.findall(old_body)
        new_body = old_body
        # print title.encode('utf-8')
        for part in parts:
            old_params = part[2]
            is_title = old_params
            is_title = re.sub(u'(префд|прист|суфф-?|корень|инф|соед|оконч-?|интер|суффд|частица|постфикс|источник)\d*=', '', is_title)
            is_title = re.sub(u'\|', '', is_title).strip()
            if is_title == title or is_title == title + u'т' or is_title == title + u'к':
                # print
                # print title
                # print part[0]
                # {{морфо||адрес|ова|суфф2=нн|ый}}
                try:
                    sres = get_sorted_list(get_old_list(old_params))
                except ValueError as e:
                    print title.encode('utf-8')
                    # sent = False
                    # try:
                    #     send_mail(subject="morpho exception in: " + title,
                    #                       message=title + "\n\n" + repr(e),
                    #                       from_email=settings.DEFAULT_FROM_EMAIL,
                    #                       recipient_list=['*****@*****.**'])
                    #     sent = True
                    # except:
                    #     pass
                    # if sent:
                    #     return
                    # else:
                    #     raise
                    raise

                # for key, value in sres:
                #     print "- %s = '%s'" % (key, value)
                res = get_new_list(sres)
                ress = '|'.join(res)
                # print ress
                new_body = new_body.replace(part[0], u'{{морфо-ru|%s}}' % ress)
                # params = part[2].strip().split('|')
                # for value in params:
                #     value = value.strip()
                #     if '=' in value:
                #         name, value = value.split('=', 1)
                #     if '-' in value:
                #         append_dict_list(self.morpho_wrong_hyphens, title, part[0])
                #         break
            elif not is_title:
                new_body = new_body.replace(part[0], u'{{морфо-ru|}}')
        if old_body != new_body:
            # print '=' * 100
            # print old_body
            # print '-' * 100
            # print new_body
            # print '=' * 100
            old_section_content = section_content
            new_section_content = section_content.replace(old_body, new_body)
            try:
                old_content = get_wiki_page_content(title)
            except NoPage:
                send_wiki_mail(subject="pywikibot.exceptions.NoPage: " + title,
                               message=title)
                return
            except IsRedirectPage:
                send_wiki_mail(
                    subject="pywikibot.exceptions.IsRedirectPage: " + title,
                    message=title
                )
                return
            new_content = old_content.replace(old_section_content, new_section_content)
            if new_content != old_content:
                desc = u"Замена {{морфо}} на {{морфо-ru}}"
                # self.n_step += 1
                # if self.n_step >= 100:
                #     self.stop()
                new_content = default_wikifier(new_content)
                save_wiki_page(title, new_content, desc)

                # todo: create special function for this:
                d = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                append_file(os.path.join(settings.FILES_PATH, 'reports',
                                         self.report_name),
                            "%s: %s" % (d, title),
                            encode='utf-8')