"""
This is a copy of the script run by ISO 3166 bot. Keep in sync
This is the play around variant
"""
'\nThis is a copy of the script run by ISO 3166 bot. Keep in sync\nThis is the play around variant\n'
anchorReg = '(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)'
# match parameter names
# match ' = '
# match multi regions like AT-7/DE-BY
anchorRef = '\g<1>' # refer to match of anchorReg

# sample replacement
# out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-33","\g<1>CN-ZJ",out)


class iso3166changer:
    """Describe the change needed for a set of ISO 3166 changes"""
    countryCode = '' # defined in derived class
    editcomment = '' # defined in derived class, without pre and post, or defaulted
    editcommentPre = 'ISO 3166 Bot: '
    #editcommentPost = '; weitere / offene Fehler unter https://quarry.wmflabs.org/query/11685'
    editcommentPost = ''
    isoRefDate = '' # reference date of publication of new set of iso codes, in German, effective date of change
    
    category = '' # root category, without namespace

    replacements = [] # an array of pairs, defined in derived class
    def execute(self, wikitext): # may be overwritten in derived class
        out = wikitext
        for i in range(len(self.replacements)):
            out = re.sub(anchorReg+self.replacements[i][0],anchorRef+self.replacements[i][1],out)
        return out
    def getEditcomment(self):
        if (self.editcomment == ''):
            return self.editcommentPre + 'ISO-Code Änderungen gemäß [[ISO_3166-2:' + self.countryCode + \
                ']] (https://www.iso.org/obp/ui/#iso:code:3166:' + self.countryCode + '), gültig seit ' + \
                self.isoRefDate + \
                self.editcommentPost
        else:
            return self.editcommentPre + self.editcomment + self.editcommentPost
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')

globalPageNames = [
               u'Global Stratotype Section and Point',
               u'Weltraumbahnhof',
               u'DP World',
               u'Eisenbahnteststrecke',
               u'Liste von Regattastrecken',
               u'Links- und Rechtsverkehr',
               u'Liste der größten Bogenbrücken',
               u'Liste von Leuchttürmen in Asien',
               u'Liste in der europäischen Expansion entdeckter Inseln',
               u'Liste der höchsten Leuchttürme der Welt',
               u'Liste der höchsten Inseln der Erde',
              ]
globalPages = []
for name in globalPageNames:
    globalPages.append(pywikibot.Page(site, name))

globalCatNames = [
               u'Umstrittenes Territorium',
               #u'Liste (Bauwerke)',
               #u'Liste (Sport)',
               #u'Liste (geographische Rekorde)',
              ]

pg = []
pg.append(globalPages)
for c in globalCatNames:
    pg.append (pagegenerators.CategorizedPageGenerator(pywikibot.Category(site, c),7))
count = 0;
for page in pagegenerators.DuplicateFilterPageGenerator (pagegenerators.CombinedPageGenerator(pg)):
    count+=1
    #print (count, page.title())

print ('finished:', count)
finished: 144
class CN(iso3166changer):
    countryCode = 'CN' # China
    category = 'China'
    isoRefDate = '2017-11-23'

    replacements = [
                ['CN-34', 'CN-AH'],
                ['CN-35', 'CN-FJ'],
                ['CN-62', 'CN-GS'],
                ['CN-44', 'CN-GD'],
                ['CN-52', 'CN-GZ'],
                ['CN-46', 'CN-HI'],
                ['CN-13', 'CN-HE'],
                ['CN-23', 'CN-HL'],
                ['CN-41', 'CN-HA'],
                ['CN-42', 'CN-HB'],
                ['CN-43', 'CN-HN'],
                ['CN-32', 'CN-JS'],
                ['CN-36', 'CN-JX'],
                ['CN-22', 'CN-JL'],
                ['CN-21', 'CN-LN'],
                ['CN-63', 'CN-QH'],
                ['CN-61', 'CN-SN'],
                ['CN-37', 'CN-SD'],
                ['CN-14', 'CN-SX'],
                ['CN-51', 'CN-SC'],
                ['CN-71', 'CN-TW'],
                ['CN-53', 'CN-YN'],
                ['CN-33', 'CN-ZJ'],
                ['CN-45', 'CN-GX'],
                ['CN-15', 'CN-NM'],
                ['CN-64', 'CN-NX'],
                ['CN-54', 'CN-XZ'],
                ['CN-65', 'CN-XJ'],
                ['CN-11', 'CN-BJ'],
                ['CN-50', 'CN-CQ'],
                ['CN-31', 'CN-SH'],
                ['CN-12', 'CN-TJ'],
                ['CN-91', 'CN-HK'],
                ['CN-92', 'CN-MO']
            ]
class TW(iso3166changer):
    countryCode = 'TW' # Taiwan
    category = 'Taiwan'
    isoRefDate = '????-??-??'

    replacements = [
                ['TW-TPQ', 'TW-TPE'],
                ['TW-KHQ', 'TW-KHH'],
            ]
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')

class isoCodeChanger:
    def __init__(self, changer, test=False, stopAfter=0):
        self.changer = changer # type iso3166changer
        self.count   = 0
        self.successCount   = 0
        self.errCount  = 0
        self.test = test
        self.stopAfter = stopAfter
        self.editComment = self.changer.getEditcomment()
    
    def execute(self):
        cat = pywikibot.Category(site,'Category:'+self.changer.category)
        pages = pagegenerators.CategorizedPageGenerator(cat,7)

        for page in pages:
            if page.isRedirectPage():
                print (page, 'is redirect')
            oldpage = page.get(get_redirect=True) # changing the redirect instead of the page it redirects to
            newpage= self.changer.execute(oldpage)
            if (oldpage != newpage):
                print (page, 'changed(%d)'% int(self.successCount+1))
                pywikibot.showDiff(oldpage, newpage)
                self.count = self.count + 1
                if (not self.test):
                    try: # might fail if no write access (as bot is not admin)
                        page.put(newpage, self.editComment)
                        self.successCount = self.successCount + 1
                    except:
                        #NoUsername: Logged in on wikipedia:de via OAuth as Herzi Pinki, but expect as ISO 3166 Bot
                        #OtherPageSaveError
                        print ("error({0}): {1}".format(e.errno, e.strerror))
                        
                        self.errCount = self.errCount + 1
                    pass
                
            if (self.count == self.stopAfter):
                break

        print ('finished:', self.successCount, 'pages changed,', self.errCount, 'pages unchanged due to error.')
cc = isoCodeChanger (CN(), test=False, stopAfter=5)
print (cc.editComment)
cc.execute()
def TW_isofix (wikitext):
    "replace 3166-2 codes for TW"
    out = wikitext
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)TW-TPQ","\g<1>TW-TPE",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)TW-KHQ","\g<1>TW-KHH",out)
    return out
def CN_isofix (wikitext):
    "replace 3166-2 codes for CN (2017)"
    out = wikitext
    #CN-34 to CN-AH    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-34","\g<1>CN-AH",out)
    #CN-35 to CN-FJ    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-35","\g<1>CN-FJ",out)
    
    #CN-62 to CN-GS
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-62","\g<1>CN-GS",out)
    #CN-44 to CN-GD    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-44","\g<1>CN-GD",out)
    #CN-52 to CN-GZ    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-52","\g<1>CN-GZ",out)
    #CN-46 to CN-HI    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-46","\g<1>CN-HI",out)
    #CN-13 to CN-HE,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-13","\g<1>CN-HE",out)
    #CN-23 to CN-HL,    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-23","\g<1>CN-HL",out)
    #CN-41 to CN-HA    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-41","\g<1>CN-HA",out)
    #CN-42 to CN-HB    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-42","\g<1>CN-HB",out)
    

    #CN-43 to CN-HN 
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-43","\g<1>CN-HN",out)
    #CN-32 to CN-JS    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-32","\g<1>CN-JS",out)
    #CN-36 to CN-JX
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-36","\g<1>CN-JX",out)
    #CN-22 to CN-JL
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-22","\g<1>CN-JL",out)
    #CN-21 to CN-LN
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-21","\g<1>CN-LN",out)
    #CN-63 to CN-QH
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-63","\g<1>CN-QH",out)
    #CN-61 to CN-SN
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-61","\g<1>CN-SN",out)
    #CN-37 to CN-SD    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-37","\g<1>CN-SD",out)
    

    #CN-14 to CN-SX,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-14","\g<1>CN-SX",out)
    #CN-51 to CN-SC    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-51","\g<1>CN-SC",out)
    #CN-71 to CN-TW
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-71","\g<1>CN-TW",out)
    #CN-53 to CN-YN    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-53","\g<1>CN-YN",out)
    #CN-33 to CN-ZJ 
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-33","\g<1>CN-ZJ",out)
    #CN-45 to CN-GX,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-45","\g<1>CN-GX",out)
    #CN-15 to CN-NM,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-15","\g<1>CN-NM",out)
    
    #CN-64 to CN-NX,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-64","\g<1>CN-NX",out)
    #CN-54 to CN-XZ,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-54","\g<1>CN-XZ",out)
    #CN-65 to CN-XJ,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-65","\g<1>CN-XJ",out)
    #CN-11 to CN-BJ,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-11","\g<1>CN-BJ",out)
    #CN-50 to CN-CQ,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-50","\g<1>CN-CQ",out)
    
    #CN-31 to CN-SH,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-31","\g<1>CN-SH",out)
    #CN-12 to CN-TJ,
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-12","\g<1>CN-TJ",out)
    #CN-91 to CN-HK
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-91","\g<1>CN-HK",out)
    #CN-92 to CN-MO
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*([A-Z]+(\-[A-Z0-9]+)?\/)*)CN-92","\g<1>CN-MO",out)
    return out
def AL_isofix (wikitext):
    "replace 3166-2 codes for AL"
    out = wikitext
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(BR|KC|SK)","\g<1>AL-01",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(DR|KR)","\g<1>AL-02",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(EL|GR|LB|PQ)","\g<1>AL-03",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(FR|LU|MK)","\g<1>AL-04",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(GJ|PR|TE)","\g<1>AL-05",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(DV|ER|KO|PG)","\g<1>AL-06",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(HA|KU|TP)","\g<1>AL-07",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(KB|LE|MR)","\g<1>AL-08",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(BU|DI|MT)","\g<1>AL-09",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(MM|PU|SH)","\g<1>AL-10",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(KA|TR)","\g<1>AL-11",out)
    out = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)AL-(DL|SR|VL)","\g<1>AL-12",out)
    return out
def CI_isofix (wikitext):
    "replace 3166-2 codes for CI - Côte d'Ivoire"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(09)","\g<1>CI-BS",out) # not 1:1
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(08)","\g<1>CI-ZZ",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(03)","\g<1>CI-SV",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(10)","\g<1>CI-DN",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(04)","\g<1>CI-VB",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(05|13)","\g<1>CI-CM",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(15|18)","\g<1>CI-GD",out) # not 1:1
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(14|17)","\g<1>CI-WR",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(06|19)","\g<1>CI-MG",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(02|12)","\g<1>CI-SM",out)
    #out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(07|11)","\g<1>CI-LC",out) # Yamoussoukro manually
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CI-(01|16)","\g<1>CI-LG",out) # Abidjan manually
    return out
def GR_isofix (wikitext):
    "replace 3166-2 codes for GR - Greece"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(52|55|71|72|73)","\g<1>GR-A",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(53|54|57|59|61|62|64)","\g<1>GR-B",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(51|56|58|63)","\g<1>GR-C",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(31|32|33|34)","\g<1>GR-D",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(41|42|43|44)","\g<1>GR-E",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(21|22|23|24)","\g<1>GR-F",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(01|13|14)","\g<1>GR-G",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(03|04|05|06|07)","\g<1>GR-H",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(A1)","\g<1>GR-I",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(11|12|15|16|17)","\g<1>GR-J",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(83|84|85)","\g<1>GR-K",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(81|82)","\g<1>GR-L",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)GR-(91|92|93|94)","\g<1>GR-M",out)
    return out
def TT_isofix (wikitext):
    "replace 3166-2 codes for TT - Trinidad - Tobago"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)TT-(WTO|ETO)","\g<1>TT-TOB",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)TT-RCM","\g<1>TT-MRC",out)
    return out
def MX_isofix (wikitext):
    "replace 3166-2 codes for MX - Mexiko"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)MX-DIF","\g<1>MX-CMX",out)
    return out
def UG_isofix (wikitext):
    "replace 3166-2 codes for UG - Uganda, runs only once - cyclic replacements!!"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-114","\g<1>UGG-115",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-115","\g<1>UGG-116",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-116","\g<1>UGG-114",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-218","\g<1>UGG-220",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-219","\g<1>UGG-221",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-220","\g<1>UGG-222",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-221","\g<1>UGG-223",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-222","\g<1>UGG-224",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-223","\g<1>UGG-218",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-224","\g<1>UGG-219",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-314","\g<1>UGG-315",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-315","\g<1>UGG-318",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-316","\g<1>UGG-319",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-317","\g<1>UGG-314",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-318","\g<1>UGG-317",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-319","\g<1>UGG-316",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-325","\g<1>UGG-420",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-326","\g<1>UGG-325",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-327","\g<1>UGG-326",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-328","\g<1>UGG-327",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-329","\g<1>UGG-328",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-330","\g<1>UGG-329",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-331","\g<1>UGG-330",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-416","\g<1>UGG-417",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-417","\g<1>UGG-418",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-418","\g<1>UGG-419",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-419","\g<1>UGG-416",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-420","\g<1>UGG-421",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-421","\g<1>UGG-422",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-422","\g<1>UGG-423",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-423","\g<1>UGG-424",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-424","\g<1>UGG-425",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UG-425","\g<1>UGG-426",out)

    # replace intermediate iso 1 code
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)UGG-","\g<1>UG-",out)
    return out


# UG-114 to UG-115, UG-115 to UG-116, UG-116 to UG-114,
# UG-125 new
# UG-218 to UG-220, UG-219 to UG-221, UG-220 to UG-222, UG-221 to UG-223, UG-222 to UG-224, UG-223 to UG-218, UG-224 to UG-219,
# UG-233, UG-234 new
# UG-314 to UG-315, UG-315 to UG-318, UG-316 to UG-319, UG-317 to UG-314, UG-318 to UG-317, UG-319 to UG-316,
# UG-325 to UG-420,
# UG-326 to UG-325, UG-327 to UG-326, UG-328 to UG-327, UG-329 to UG-328, UG-330 to UG-329, UG-331 to UG-330,
# UG_332 new
# UG-416 to UG-417, UG-417 to UG-418, UG-418 to UG-419, UG-419 to UG-416, UG-420 to UG-421, UG-421 to UG-422, UG-422 to UG-423, UG-423 to UG-424, UG-424 to UG-425, UG-425 to UG-426
# UG-427, UG-428, UG-429, UG-430, UG-431 new
def CZ_isofix (wikitext):
    "replace 3166-2 codes for CZ - Czech republic"
    out = wikitext
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-62([1-7])","\g<1>CZ-64\g<5>",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-61([1-5])","\g<1>CZ-63\g<5>",out)

    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10A","\g<1>CZ-110",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10B","\g<1>CZ-111",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10C","\g<1>CZ-112",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10D","\g<1>CZ-113",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10E","\g<1>CZ-114",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-10F","\g<1>CZ-115",out)
    
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-JC","\g<1>CZ-31",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-JM","\g<1>CZ-64",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-KA","\g<1>CZ-41",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-KR","\g<1>CZ-52",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-LI","\g<1>CZ-51",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-MO","\g<1>CZ-80",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-OL","\g<1>CZ-71",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-PA","\g<1>CZ-53",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-PL","\g<1>CZ-32",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-PR","\g<1>CZ-10",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-ST","\g<1>CZ-20",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-US","\g<1>CZ-42",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-VY","\g<1>CZ-63",out)
    out = re.sub(r"(((region|REGION|Region)(-ISO| ISO|-iso| iso|)|ISO-CODE)\s*=\s*)CZ-ZL","\g<1>CZ-72",out)
    return out
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')
count = 0

# config #####
test = True # if test, nothing is written back
traverseByCat = True # if set, the cat is iterated, if false, the list of explicit pagenames is iterated
editor = CN_isofix
cat = pywikibot.Category(site,'Category:China')
#editComment = "siehe [[ISO_3166-2:CZ]], kraje haben seit 2016 numerische codes; weitere Fehler unter https://quarry.wmflabs.org/query/11685"
#editComment = "siehe [[ISO_3166-2:UG]], Änderungen mit Juni 2016 und November 2017; weitere Fehler unter https://quarry.wmflabs.org/query/11685"
editComment = "siehe [[ISO_3166-2:CN]], Änderungen November 2017; weitere Fehler unter https://quarry.wmflabs.org/query/11685"

pagenames = [u'Liupan Shan'
        ]

if (traverseByCat):
    pages = pagegenerators.CategorizedPageGenerator(cat,7)
else:
    pages = []
    for pn in pagenames:
        pages.append(pywikibot.Page(site, pn))

for page in pages:
    if page.isRedirectPage():
        print (page, 'is redirect')
    oldpage = page.get(get_redirect=True)
    newpage= editor(oldpage)
    if (oldpage != newpage):
        count = count + 1
        print (page, 'changed(%d)'%count)
        pywikibot.showDiff(oldpage, newpage)
        if (not test):
            page.put(newpage, editComment)
            pass
    else:
        #print (page, 'unchanged')
        pass;
    
print ('finished:', count, 'pages changed.')
"replace ISO codes for FR"
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')

deps = [
        ['Département Ain', '01', 'V'],
        ['Département Aisne', '02', 'S'],
        ['Département Allier', '03', 'C'],
        ['Département Alpes-de-Haute-Provence', '04', 'U'],
        ['Département Hautes-Alpes', '05', 'U'],
        ['Département Alpes-Maritimes', '06', 'U'],
        ['Département Ardèche', '07', 'V'],
        ['Département Ardennes', '08', 'G'],
        ['Département Ariège', '09', 'N'],
        ['Département Aube', '10', 'G'],
        ['Département Aude', '11', 'K'],
        ['Département Aveyron', '12', 'N'],
        ['Département Bouches-du-Rhône', '13', 'U'],
        ['Département Calvados', '14', 'P'],
        ['Département Cantal', '15', 'C'],
        ['Département Charente', '16', 'T'],
        ['Département Charente-Maritime', '17', 'T'],
        ['Département Cher', '18', 'F'],
        ['Département Corrèze', '19', 'L'],
        ['Département Corse-du-Sud', '2A', 'H'],
        ['Département Haute-Corse', '2B', 'H'],
        ['Département Côte-d’Or', '21', 'D'],
        ['Département Côtes-d’Armor', '22', 'E'],
        ['Département Creuse', '23', 'L'],
        ['Département Dordogne', '24', 'B'],
        ['Département Doubs', '25', 'I'],
        ['Département Drôme', '26', 'V'],
        ['Département Eure', '27', 'Q'],
        ['Département Eure-et-Loir', '28', 'F'],
        ['Département Finistère', '29', 'E'],
        ['Département Gard', '30', 'K'],
        ['Département Haute-Garonne', '31', 'N'],
        ['Département Gers', '32', 'N'],
        ['Département Gironde', '33', 'B'],
        ['Département Hérault', '34', 'K'],
        ['Département Ille-et-Vilaine', '35', 'E'],
        ['Département Indre', '36', 'F'],
        ['Département Indre-et-Loire', '37', 'F'],
        ['Département Isère', '38', 'V'],
        ['Département Jura', '39', 'I'],
        ['Département Landes', '40', 'B'],
        ['Département Loir-et-Cher', '41', 'F'],
        ['Département Loire', '42', 'V'],
        ['Département Haute-Loire', '43', 'C'],
        ['Département Loire-Atlantique', '44', 'R'],
        ['Département Loiret', '45', 'F'],
        ['Département Lot', '46', 'N'],
        ['Département Lot-et-Garonne', '47', 'B'],
        ['Département Lozère', '48', 'K'],
        ['Département Maine-et-Loire', '49', 'R'],
        ['Département Manche', '50', 'P'],
        ['Département Marne', '51', 'G'],
        ['Département Haute-Marne', '52', 'G'],
        ['Département Mayenne', '53', 'R'],
        ['Département Meurthe-et-Moselle', '54', 'M'],
        ['Département Meuse', '55', 'M'],
        ['Département Morbihan', '56', 'E'],
        ['Département Moselle', '57', 'M'],
        ['Département Nièvre', '58', 'D'],
        ['Département Nord', '59', 'O'],
        ['Département Oise', '60', 'S'],
        ['Département Orne', '61', 'P'],
        ['Département Pas-de-Calais', '62', 'O'],
        ['Département Puy-de-Dôme', '63', 'C'],
        ['Département Pyrénées-Atlantiques', '64', 'B'],
        ['Département Hautes-Pyrénées', '65', 'N'],
        ['Département Pyrénées-Orientales', '66', 'K'],
        ['Département Bas-Rhin', '67', 'A'],
        ['Département Haut-Rhin', '68', 'A'],
        ['Département Rhône', '69', 'V'],
        ['Métropole de Lyon', '69', 'V'],
        ['Département Haute-Saône', '70', 'I'],
        ['Département Saône-et-Loire', '71', 'D'],
        ['Département Sarthe', '72', 'R'],
        ['Département Savoie', '73', 'V'],
        ['Département Haute-Savoie', '74', 'V'],
        ['Paris', '75', 'J'],
        ['Département Seine-Maritime', '76', 'Q'],
        ['Département Seine-et-Marne', '77', 'J'],
        ['Département Yvelines', '78', 'J'],
        ['Département Deux-Sèvres', '79', 'T'],
        ['Département Somme', '80', 'S'],
        ['Département Tarn', '81', 'N'],
        ['Département Tarn-et-Garonne', '82', 'N'],
        ['Département Var', '83', 'U'],
        ['Département Vaucluse', '84', 'U'],
        ['Département Vendée', '85', 'R'],
        ['Département Vienne', '86', 'T'],
        ['Département Haute-Vienne', '87', 'L'],
        ['Département Vosges', '88', 'M'],
        ['Département Yonne', '89', 'D'],
        ['Territoire de Belfort', '90', 'I'],
        ['Département Essonne', '91', 'J'],
        ['Département Hauts-de-Seine', '92', 'J'],
        ['Département Seine-Saint-Denis', '93', 'J'],
        ['Département Val-de-Marne', '94', 'J'],
        ['Département Val-d’Oise', '95', 'J']
    ]

for d in deps:
    print ('\nCategory:'+d[0])
    cat = pywikibot.Category(site,'Category:'+d[0])
    pages = pagegenerators.CategorizedPageGenerator(cat,5)


    for page in pages:
        if page.isRedirectPage():
            print (page, 'is redirect')
            continue;
        oldpage = page.get()
        newpage = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)FR-"+d[2],"\g<1>FR-"+d[1],oldpage)
        if (oldpage != newpage):
            print (page, 'changed')
            pywikibot.showDiff(oldpage, newpage)
            page.put(newpage, "siehe [[ISO_3166-2:FR]]; weitere fehler unter https://quarry.wmflabs.org/query/11685. ISO Code auf level 2 (département statt region)")
        else:
            #print (page, 'unchanged')
            pass

print ('finished')
"replace ISO codes for LU"
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')

cantons = [
        ['Befort (Luxemburg)', 'EC', 'G'],
        ['Bech (Luxemburg)', 'EC', 'G'],
        ['Berdorf', 'EC', 'G'],
        ['Consdorf', 'EC', 'G'],
        ['Echternach (Luxemburg)', 'EC', 'G'],
        ['Mompach', 'EC', 'G'],
        ['Rosport', 'EC', 'G'],
        ['Waldbillig', 'EC', 'G'],

        ['Bettemburg', 'ES', 'L'],
        ['Differdingen', 'ES', 'L'],
        ['Düdelingen', 'ES', 'L'],
        ['Esch an der Alzette', 'ES', 'L'],
        ['Frisingen', 'ES', 'L'],
        ['Kayl', 'ES', 'L'],
        ['Leudelingen', 'ES', 'L'],
        ['Monnerich', 'ES', 'L'],
        ['Petingen', 'ES', 'L'],
        ['Reckingen/Mess', 'ES', 'L'],
        ['Roeser', 'ES', 'L'],
        ['Rümelingen', 'ES', 'L'],
        ['Sassenheim (Luxemburg)', 'ES', 'L'],
        ['Schifflingen', 'ES', 'L'],
    
        ['Dippach (Luxemburg)', 'CA', 'L'],
        ['Garnich', 'CA', 'L'],
        ['Hobscheid', 'CA', 'L'],
        ['Käerjeng', 'CA', 'L'],
        ['Kehlen (Luxemburg)', 'CA', 'L'],
        ['Koerich', 'CA', 'L'],
        ['Kopstal', 'CA', 'L'],
        ['Mamer', 'CA', 'L'],
        ['Simmern (Luxemburg)', 'CA', 'L'],
        ['Steinfort', 'CA', 'L'],

        ['Clerf', 'CL', 'D'],
        ['Parc Hosingen', 'CL', 'D'],
        ['Ulflingen', 'CL', 'D'],
        ['Weiswampach', 'CL', 'D'],
        ['Wintger', 'CL', 'D'],

        ['Bettendorf (Luxemburg)', 'DI', 'D'],
        ['Burscheid (Luxemburg)', 'DI', 'D'],
        ['Diekirch', 'DI', 'D'],
        ['Ernztalgemeinde', 'DI', 'D'],
        ['Erpeldingen an der Sauer', 'DI', 'D'],
        ['Ettelbrück', 'DI', 'D'],
        ['Feulen', 'DI', 'D'],
        ['Mertzig', 'DI', 'D'],
        ['Reisdorf (Luxemburg)', 'DI', 'D'],
        ['Schieren (Luxemburg)', 'DI', 'D'],

        ['Beckerich', 'RD', 'D'],
        ['Ell (Luxemburg)', 'RD', 'D'],
        ['Grosbous', 'RD', 'D'],
        ['Préizerdaul', 'RD', 'D'],
        ['Rambruch', 'RD', 'D'],
        ['Redingen', 'RD', 'D'],
        ['Saeul', 'RD', 'D'],
        ['Useldingen', 'RD', 'D'],
        ['Vichten', 'RD', 'D'],
        ['Wahl (Luxemburg)', 'RD', 'D'],

        ['Tandel', 'VD', 'D'],
        ['Putscheid', 'VD', 'D'],
        ['Vianden', 'VD', 'D'],

        ['Bauschleiden', 'WI', 'D'],
        ['Esch-Sauer', 'WI', 'D'],
        ['Goesdorf', 'WI', 'D'],
        ['Kiischpelt', 'WI', 'D'],
        ['Stauseegemeinde', 'WI', 'D'],
        ['Wiltz', 'WI', 'D'],
        ['Winseler', 'WI', 'D'],

        ['Betzdorf (Luxemburg)', 'GR', 'G'],
        ['Biwer', 'GR', 'G'],
        ['Flaxweiler', 'GR', 'G'],
        ['Grevenmacher', 'GR', 'G'],
        ['Junglinster', 'GR', 'G'],
        ['Manternach', 'GR', 'G'],
        ['Mertert', 'GR', 'G'],
        ['Wormeldingen', 'GR', 'G'],

        ['Bissen', 'ME', 'L'],
        ['Böwingen/Attert', 'ME', 'L'],
        ['Colmar-Berg', 'ME', 'L'],
        ['Fels (Luxemburg)', 'ME', 'L'],
        ['Fischbach (Luxemburg)', 'ME', 'L'],
        ['Heffingen', 'ME', 'L'],
        ['Lintgen', 'ME', 'L'],
        ['Lorentzweiler', 'ME', 'L'],
        ['Mersch', 'ME', 'L'],
        ['Nommern', 'ME', 'L'],
        ['Tüntingen', 'ME', 'L'],

        ['Bad Mondorf', 'RM', 'G'],
        ['Bous (Luxemburg)', 'RM', 'G'],
        ['Dalheim (Luxemburg)', 'RM', 'G'],
        ['Lenningen (Luxemburg)', 'RM', 'G'],
        ['Schengen', 'RM', 'G'],
        ['Remich', 'RM', 'G'],
        ['Stadtbredimus', 'RM', 'G'],
        ['Waldbredimus', 'RM', 'G'],

        ['Luxemburg (Stadt)', 'LU', 'L'],
        ['Bartringen', 'LU', 'L'],
        ['Contern', 'LU', 'L'],
        ['Hesperingen', 'LU', 'L'],
        ['Niederanven', 'LU', 'L'],
        ['Sandweiler', 'LU', 'L'],
        ['Schüttringen', 'LU', 'L'],
        ['Steinsel', 'LU', 'L'],
        ['Strassen (Luxemburg)', 'LU', 'L'],
        ['Walferdingen', 'LU', 'L'],
        ['Weiler zum Turm', 'LU', 'L'],

]

for d in cantons:
    print ('\nCategory:'+d[0])
    cat = pywikibot.Category(site,'Category:'+d[0])
    pages = pagegenerators.CategorizedPageGenerator(cat,5)


    for page in pages:
        if page.isRedirectPage():
            print (page, 'is redirect')
            oldpage = page.get(get_redirect=True)
        else:
            oldpage = page.get()
        newpage = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)LU-"+d[2]+"([^A-Z])","\g<1>LU-"+d[1]+"\g<4>",oldpage)
        newpage = re.sub(r"((region|REGION|Region)(-ISO| ISO|-iso| iso|)\s*=\s*)LU-LUU*","\g<1>LU-LU",newpage)
        if (oldpage != newpage):
            print (page, 'changed')
            pywikibot.showDiff(oldpage, newpage)
            page.put(newpage, "siehe [[ISO_3166-2:LU]]; weitere fehler unter https://quarry.wmflabs.org/query/11685. ISO Code auf level 2 (canton statt region)")
        else:
            #print (page, 'unchanged')
            pass

print ('finished')
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('de')
pages = [u'N’Zianouan', u'Haut-Sassandra', u'Savanes_(ehemalige_Region)', u'Ferkessédougou', u'Korhogo', u'Kong_(Stadt)', u'Vallée_du_Bandama_(ehemalige_Region)',
         u'Moyen-Comoé', u'Dix-Huit_Montagnes', u'Zouan-Hounien', u'Danané', u'Man_(Elfenbeinküste)', u'Lacs_(Region)', u'Bondoukou',
         u'Tain_(Fluss)', u'Denguélé_(ehemalige_Region)', u'Odienné', u'Marahoué', u'Nationalpark_Marahoué', u'Aboisso', u'Sud-Comoé',
         u'Anschläge_am_13._März_2016_in_Grand-Bassam', u'Worodougou', u'Morondo', u'Agnéby', u'Akoupé', u'Bafing_(Region)',
         u'Moyen-Cavally', u'Doké_(Stadt)', u'Bédy-Goazon', u'Toulépleu']
for pn in pages:
    page = pywikibot.Page(site, pn)
    oldpage = page.get()
    newpage= CI_isofix(oldpage)
    if (oldpage != newpage):
        pywikibot.showDiff(oldpage, newpage)
        page.put(newpage, "siehe [[ISO_3166-2:CI]]; weitere fehler unter https://quarry.wmflabs.org/query/11685")
    else:
        print (pn, 'unchanged')