<commit_before>#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This is a helper script to convert compat 1.0 scripts to the new core 2.0
framework.

NOTE: Please be aware that this script is not be able to convert your codes
completely. It may support you with some automatic replacements and it gives
some warnings and hints for converting. Please refer the converting guide
README-conversion.txt in the core framework folder and check your codes finally.

The scripts asks for the .py file and converts it to
<scriptname>-core.py in the same directory. The following options are supported:

- warnonly: Do not convert the source but show warning messages. This is good
            to check already merged scripts.

usage

to convert a script and show warnings about deprecated methods:
            pwb.py maintenance/compat2core <scriptname>

to show warnings about deprecated methods:
            pwb.py maintenance/compat2core <scriptname> -warnonly
"""
#
# (C) xqt, 2014
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#

import os
import re
import codecs
import pywikibot

# be carefull with replacement order!
replacements = (
    # doc strings
    ('#\r?\n__version__',
     '#\n# Automatically ported from compat branch by compat2core.py script\n'
     '#\n__version__'),
    ('Pywikipedia bot team', 'Pywikibot team'),
    # importing changes
    ('import wikipedia(?: as pywikibot)?', 'import pywikibot'),
    ('(?<!from pywikibot )import (config|pagegenerators)',
     r'from pywikibot import \1'),
    ('(?<!from pywikibot.compat )import query',
     'from pywikibot.compat import query'),
    # remove deprecated libs
    ('import catlib\r?\n', ''),
    ('import userlib\r?\n', ''),
    # change wikipedia to pywikibot, exclude URLs
    ('(?<!\.)wikipedia\.', u'pywikibot.'),
    # site instance call
    ('pywikibot\.getSite\s*\(\s*', 'pywikibot.Site('),
    # lang is different from code. We should use code in core
    ('([Ss])ite.lang(?:uage\(\))?', r'\1ite.code'),
    # change compat library classes to pywikibot intrinsic classes
    ('catlib\.Category\s*\(\s*', 'pywikibot.Category('),
    ('catlib\.change_category\s*\((\s*)(?P<article>.+?),\s*(?P<oldcat>.+?),',
     r'\g<article>.change_category(\1\g<oldcat>,'),
    ('userlib\.User\s*\(\s*', 'pywikibot.User('),
    # deprecated title methods
    ('\.urlname\s*\(\s*\)', '.title(asUrl=True)'),
    ('\.urlname\s*\(\s*(?:withNamespace\s*=\s*)?(True|False)+\s*\)',
     r'.title(asUrl=True, withNamespace=\1)'),
    ('\.titleWithoutNamespace\s*\(\s*\)', '.title(withNamespace=False)'),
    ('\.sectionFreeTitle\s*\(\s*\)', '.title(withSection=False)'),
    ('\.aslink\s*\(\s*\)', '.title(asLink=True)'),
    # other deprecated methods
    ('\.encoding\s*\(\s*\)', '.site.encoding()'),
    # stopme() is doen by the framework itself
    ('(\s*)try\:\s*\r?\n\s+main\(\)\s*\r?\n\s*finally\:\s*\r?\n\s+pywikibot\.stopme\(\)',
     r'\1main()'),
)

# some warnings which must be changed manually
warnings = (
    ('pywikibot.setAction(',
     'setAction() no longer works; you must pass an explicit edit summary\n'
     'message to put() or put_async()'),
    ('.removeImage(',<commit_msg>convert page.get(get_redirect=True) to page.text,<commit_after>#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This is a helper script to convert compat 1.0 scripts to the new core 2.0
framework.

NOTE: Please be aware that this script is not be able to convert your codes
completely. It may support you with some automatic replacements and it gives
some warnings and hints for converting. Please refer the converting guide
README-conversion.txt in the core framework folder and check your codes finally.

The scripts asks for the .py file and converts it to
<scriptname>-core.py in the same directory. The following options are supported:

- warnonly: Do not convert the source but show warning messages. This is good
            to check already merged scripts.

usage

to convert a script and show warnings about deprecated methods:
            pwb.py maintenance/compat2core <scriptname>

to show warnings about deprecated methods:
            pwb.py maintenance/compat2core <scriptname> -warnonly
"""
#
# (C) xqt, 2014
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#

import os
import re
import codecs
import pywikibot

# be carefull with replacement order!
replacements = (
    # doc strings
    ('#\r?\n__version__',
     '#\n# Automatically ported from compat branch by compat2core.py script\n'
     '#\n__version__'),
    ('Pywikipedia bot team', 'Pywikibot team'),
    # importing changes
    ('import wikipedia(?: as pywikibot)?', 'import pywikibot'),
    ('(?<!from pywikibot )import (config|pagegenerators)',
     r'from pywikibot import \1'),
    ('(?<!from pywikibot.compat )import query',
     'from pywikibot.compat import query'),
    # remove deprecated libs
    ('import catlib\r?\n', ''),
    ('import userlib\r?\n', ''),
    # change wikipedia to pywikibot, exclude URLs
    ('(?<!\.)wikipedia\.', u'pywikibot.'),
    # site instance call
    ('pywikibot\.getSite\s*\(\s*', 'pywikibot.Site('),
    # lang is different from code. We should use code in core
    ('([Ss])ite.lang(?:uage\(\))?', r'\1ite.code'),
    # change compat library classes to pywikibot intrinsic classes
    ('catlib\.Category\s*\(\s*', 'pywikibot.Category('),
    ('catlib\.change_category\s*\((\s*)(?P<article>.+?),\s*(?P<oldcat>.+?),',
     r'\g<article>.change_category(\1\g<oldcat>,'),
    ('userlib\.User\s*\(\s*', 'pywikibot.User('),
    # deprecated title methods
    ('\.urlname\s*\(\s*\)', '.title(asUrl=True)'),
    ('\.urlname\s*\(\s*(?:withNamespace\s*=\s*)?(True|False)+\s*\)',
     r'.title(asUrl=True, withNamespace=\1)'),
    ('\.titleWithoutNamespace\s*\(\s*\)', '.title(withNamespace=False)'),
    ('\.sectionFreeTitle\s*\(\s*\)', '.title(withSection=False)'),
    ('\.aslink\s*\(\s*\)', '.title(asLink=True)'),
    # other deprecated methods
    ('(?<!site)\.encoding\s*\(\s*\)', '.site.encoding()'),
    # new core methods
    ('\.get\s*\(\s*get_redirect\s*=\s*True\s*\)', '.text'),
    # stopme() is doen by the framework itself
    ('(\s*)try\:\s*\r?\n\s+main\(\)\s*\r?\n\s*finally\:\s*\r?\n\s+pywikibot\.stopme\(\)',
     r'\1main()'),
)

# some warnings which must be changed manually
warnings = (
    ('pywikibot.setAction(',
     'setAction() no longer works; you must pass an explicit edit summary\n'
     'message to put() or put_async()'),
    ('.removeImage(',
{'fraction_non_alphanumeric': 0.17764778325123154, 'fraction_numerical': 0.005233990147783251, 'mean_word_length': 3.5055478502080444, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4209289', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2114, 'n_words': 609}