http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11722
Revision: 11722 Author: xqt Date: 2013-07-07 16:51:44 +0000 (Sun, 07 Jul 2013) Log Message: ----------- pep9 changes from rewrite branch r11721
Modified Paths: -------------- trunk/pywikipedia/featured.py
Modified: trunk/pywikipedia/featured.py =================================================================== --- trunk/pywikipedia/featured.py 2013-07-07 16:44:15 UTC (rev 11721) +++ trunk/pywikipedia/featured.py 2013-07-07 16:51:44 UTC (rev 11722) @@ -43,30 +43,35 @@ # # (C) Maxim Razin, 2005 # (C) Leonardo Gregianin, 2005-2008 -# (C) xqt, 2009-2012 +# (C) xqt, 2009-2013 # (C) Pywikipedia bot team, 2005-2012 # # Distributed under the terms of the MIT license. #
-import sys, re, pickle, os.path +import os.path +import pickle +import re +import sys from copy import copy import wikipedia as pywikibot from pywikibot import i18n import catlib, config from pagegenerators import PreloadingGenerator
+ def CAT(site, name, hide): name = site.namespace(14) + ':' + name cat=catlib.Category(site, name) for article in cat.articles(endsort=hide): yield article if hide: - for article in cat.articles(startFrom=unichr(ord(hide)+1)): + for article in cat.articles(startFrom=unichr(ord(hide) + 1)): yield article
+ def BACK(site, name, hide): - p=pywikibot.Page(site, name, defaultNamespace=10) + p = pywikibot.Page(site, name, defaultNamespace=10) return [page for page in p.getReferences(follow_redirects=False, onlyTemplateInclusion=True)]
@@ -77,7 +82,7 @@ 'als': ['LinkFA'], 'an': ['Destacato', 'Destacau'], 'ar': [u'وصلة مقالة مختارة'], - 'ast':['Enllaz AD'], + 'ast': ['Enllaz AD'], 'az': ['Link FM'], 'br': ['Liamm PuB', 'Lien AdQ'], 'ca': [u'Enllaç AD', 'Destacat'], @@ -87,7 +92,7 @@ 'es': ['Destacado'], 'eu': ['NA lotura'], 'fr': ['Lien AdQ'], - 'fur':['Leam VdC'], + 'fur': ['Leam VdC'], 'ga': ['Nasc AR'], 'hi': ['Link FA', 'Lien AdQ'], 'is': [u'Tengill ÚG'], @@ -114,7 +119,7 @@ 'nn': ['Link AA'], 'no': ['Link AA'], 'pt': ['Bom interwiki'], - #'tr': ['Link GA', 'Link KM'], +## 'tr': ['Link GA', 'Link KM'], 'vi': [u'Liên kết bài chất lượng tốt'], 'wo': ['Lien BA'], } @@ -125,77 +130,77 @@ }
featured_name = { - 'af': (BACK,u"Voorbladster"), - 'als':(CAT, u"Wikipedia:Bsunders glungener Artikel"), + 'af': (BACK, u"Voorbladster"), + 'als': (CAT, u"Wikipedia:Bsunders glungener Artikel"), 'am': (CAT, u"Wikipedia:Featured article"), 'an': (CAT, u"Articlos destacatos"), - 'ang':(CAT, u"Fulgōd ȝeƿritu"), + 'ang': (CAT, u"Fulgōd ȝeƿritu"), 'ar': (CAT, u"مقالات مختارة"), - 'ast':(CAT, u"Uiquipedia:Artículos destacaos"), - 'az': (BACK,u"Seçilmiş məqalə"), - 'bar':(CAT, u"Berig"), + 'ast': (CAT, u"Uiquipedia:Artículos destacaos"), + 'az': (BACK, u"Seçilmiş məqalə"), + 'bar': (CAT, u"Berig"), 'bat-smg': (CAT, u"Vikipedėjės pavīzdėnē straipsnē"), - 'be-x-old':(CAT, u"Вікіпэдыя:Абраныя артыкулы"), + 'be-x-old': (CAT, u"Вікіпэдыя:Абраныя артыкулы"), 'bg': (CAT, u"Избрани статии"), - 'bn': (BACK,u"নির্বাচিত নিবন্ধ"), + 'bn': (BACK, u"নির্বাচিত নিবন্ধ"), 'br': (CAT, u"Pennadoù eus an dibab"), 'bs': (CAT, u"Odabrani članci"), 'ca': (CAT, u"Llista d'articles de qualitat"), - 'ceb':(CAT, u"Mga napiling artikulo"), + 'ceb': (CAT, u"Mga napiling artikulo"), 'cs': (CAT, u"Wikipedie:Nejlepší články"), - 'cy': (BACK,u"Erthygl ddethol"), + 'cy': (BACK, u"Erthygl ddethol"), 'da': (CAT, u"Fremragende artikler"), 'de': (CAT, u"Wikipedia:Exzellent"), 'dv': (BACK, u"Featured article"), - 'el': (BACK,u"Αξιόλογο άρθρο"), + 'el': (BACK, u"Αξιόλογο άρθρο"), 'en': (CAT, u"Featured articles"), 'eo': (CAT, u"Elstaraj artikoloj"), 'es': (BACK, u"Artículo destacado"), 'et': (CAT, u"Eeskujulikud artiklid"), 'eu': (CAT, u"Nabarmendutako artikuluak"), - 'ext':(BACK,u"Destacau"), - 'fa': (BACK,u"مقاله برگزیده"), + 'ext': (BACK, u"Destacau"), + 'fa': (BACK, u"مقاله برگزیده"), 'fi': (CAT, u"Suositellut sivut"), 'fo': (CAT, u"Mánaðargrein"), 'fr': (CAT, u"Article de qualité"), - 'frr':(BACK,u"Exzellent"), + 'frr': (BACK, u"Exzellent"), 'gl': (CAT, u"Wikipedia:Artigos de calidade"), 'gv': (CAT, u"Artyn reiht"), 'he': (CAT, u"ערכים מומלצים"), - 'hi': (BACK,u"निर्वाचित लेख"), + 'hi': (BACK, u"निर्वाचित लेख"), 'hr': (CAT, u"Izabrani članci"), - 'hsb':(CAT, u"Ekscelentny"), + 'hsb': (CAT, u"Ekscelentny"), 'hu': (CAT, u"Kiemelt cikkek"), - 'hy': (BACK,u"Ընտրված հոդված"), + 'hy': (BACK, u"Ընտրված հոդված"), 'ia': (CAT, u"Wikipedia:Articulos eminente"), 'id': (BACK, u"Featured article"), 'is': (CAT, u"Wikipedia:Úrvalsgreinar"), 'it': (CAT, u"Voci in vetrina"), - 'ja': (BACK,u"Featured article"), + 'ja': (BACK, u"Featured article"), 'ka': (CAT, u"რჩეული სტატიები"), 'kk': (CAT, u"Уикипедия:Таңдаулы мақалалар"), 'kl': (CAT, u"Anbefalet"), - 'km': (BACK,u"អត្ថបទពិសេស"), - 'kn': (BACK,u"ವಿಶೇಷ ಲೇಖನ"), + 'km': (BACK, u"អត្ថបទពិសេស"), + 'kn': (BACK, u"ವಿಶೇಷ ಲೇಖನ"), 'ko': (CAT, u"알찬 글"), - 'krc':(CAT, u"Википедия:Сайланнган статьяла"), + 'krc': (CAT, u"Википедия:Сайланнган статьяла"), 'kv': (CAT, u"Википедия:Бур гижӧдъяс"), 'la': (CAT, u"Paginae mensis"), - 'lad':(CAT, u"Artikolos valutosos"), + 'lad': (CAT, u"Artikolos valutosos"), 'li': (CAT, u"Wikipedia:Sjterartikele"), - 'lmo':(CAT, u"Articol ben faa"), + 'lmo': (CAT, u"Articol ben faa"), 'lo': (CAT, u"ບົດຄວາມດີເດັ່ນ"), 'lt': (CAT, u"Vikipedijos pavyzdiniai straipsniai"), 'lv': (CAT, u"Vērtīgi raksti"), 'mk': (BACK, u"Избрана"), - 'ml': (BACK,u"Featured"), + 'ml': (BACK, u"Featured"), 'mt': (CAT, u"Artikli fil-vetrina"), 'mr': (CAT, u"मुखपृष्ठ सदर लेख"), - 'ms': (BACK,u"Rencana pilihan"), - 'nah':(BACK,u"Featured article"), + 'ms': (BACK, u"Rencana pilihan"), + 'nah': (BACK, u"Featured article"), 'nds-nl': (BACK, u"Etelazie"), 'nl': (CAT, u"Wikipedia:Etalage-artikelen"), - 'nn': (BACK,u"God artikkel"), + 'nn': (BACK, u"God artikkel"), 'no': (CAT, u"Utmerkede artikler"), 'nv': (CAT, u"Naaltsoos nizhónígo ályaaígíí"), 'oc': (CAT, u"Article de qualitat"), @@ -204,33 +209,33 @@ 'qu': (CAT, u"Wikipidiya:Kusa qillqa"), 'ro': (CAT, u"Articole de calitate"), 'ru': (BACK, u"Избранная статья"), - 'sco':(CAT, u"Featurt"), + 'sco': (CAT, u"Featurt"), 'sh': (CAT, u"Izabrani članci"), 'simple': (CAT, u"Very good articles"), - 'sk': (BACK,u"Perfektný článok"), + 'sk': (BACK, u"Perfektný článok"), 'sl': (CAT, u"Vsi izbrani članki"), - 'sq': (BACK,u"Artikulli perfekt"), + 'sq': (BACK, u"Artikulli perfekt"), 'sr': (CAT, u"Изабрани"), 'sv': (CAT, u"Wikipedia:Utmärkta artiklar"), - 'sw': (BACK,u"Makala_nzuri_sana"), - 'szl':(CAT, u"Wyrůżńůne artikle"), + 'sw': (BACK, u"Makala_nzuri_sana"), + 'szl': (CAT, u"Wyrůżńůne artikle"), 'ta': (CAT, u"சிறப்புக் கட்டுரைகள்"), 'te': (CAT, u"విశేషవ్యాసాలు"), - 'th': (BACK,u"บทความคัดสรร"), - 'tl': (BACK,u"Napiling artikulo"), + 'th': (BACK, u"บทความคัดสรร"), + 'tl': (BACK, u"Napiling artikulo"), 'tn': (CAT, u"Featured articles"), - 'tr': (BACK,u"Seçkin madde"), + 'tr': (BACK, u"Seçkin madde"), 'tt': (CAT, u"Сайланган мәкаләләр"), - 'udm':(CAT, u"Википедия:Быръем статьяос"), + 'udm': (CAT, u"Википедия:Быръем статьяос"), 'uk': (CAT, u"Вибрані статті"), 'ur': (CAT, u"منتخب مقالے"), 'uz': (CAT, u"Vikipediya:Tanlangan maqolalar"), - 'vec':(BACK,u"Vetrina"), + 'vec': (BACK, u"Vetrina"), 'vi': (CAT, u"Bài viết chọn lọc"), 'vo': (CAT, u"Yegeds gudik"), 'wa': (CAT, u"Raspepyî årtike"), 'yi': (CAT, u"רעקאמענדירטע ארטיקלען"), - 'yo': (BACK,u"Àyọkà pàtàkì"), + 'yo': (BACK, u"Àyọkà pàtàkì"), 'zh': (CAT, u"特色条目"), 'zh-classical': (CAT, u"卓著"), 'zh-yue': (BACK, u"正文"), @@ -242,7 +247,7 @@ 'cs': (CAT, u"Wikipedie:Dobré články"), 'da': (CAT, u"Gode artikler"), 'de': (CAT, u"Wikipedia:Lesenswert"), - #'dsb':(CAT, u"Naraźenje za pógódnośenje"), +## 'dsb': (CAT, u"Naraźenje za pógódnośenje"), 'en': (CAT, u"Wikipedia good articles"), 'eo': (CAT, u"Legindaj artikoloj"), 'es': (CAT, u"Wikipedia:Artículos buenos"), @@ -250,12 +255,12 @@ 'fa': (CAT, u"مقالههای خوب"), 'fi': (CAT, u"Hyvät artikkelit"), 'fr': (CAT, u"Bon article"), - 'hsb':(CAT, u"Namjet za pohódnoćenje"), - 'id': (BACK,u"Artikel bagus"), + 'hsb': (CAT, u"Namjet za pohódnoćenje"), + 'id': (BACK, u"Artikel bagus"), 'is': (CAT, u"Wikipedia:Gæðagreinar"), - 'ja': (BACK,u"Good article"), + 'ja': (BACK, u"Good article"), 'ko': (CAT, u"좋은 글"), - 'ksh':(CAT, u"Joode Aatikkel"), + 'ksh': (CAT, u"Joode Aatikkel"), 'lt': (CAT, u"Vertingi straipsniai"), 'lv': (CAT, u"Labi raksti"), 'no': (CAT, u"Anbefalte artikler"), @@ -265,9 +270,9 @@ 'ro': (BACK, u"Articol bun"), 'ru': (CAT, u"Википедия:Хорошие статьи по алфавиту"), 'simple': (CAT, u"Good articles"), - 'sr': (BACK,u"Иконица добар"), + 'sr': (BACK, u"Иконица добар"), 'sv': (CAT, u"Wikipedia:Bra artiklar"), - 'tr': (BACK,u"Kaliteli madde"), + 'tr': (BACK, u"Kaliteli madde"), 'uk': (CAT, u"Вікіпедія:Добрі статті"), 'uz': (CAT, u"Vikipediya:Yaxshi maqolalar"), 'yi': (CAT, u"וויקיפעדיע גוטע ארטיקלען"), @@ -283,7 +288,7 @@ 'fa': (BACK, u"فهرست برگزیده"), 'id': (BACK, u'Featured list'), 'ja': (BACK, u'Featured List'), - 'ksh':(CAT, u"Joode Leß"), + 'ksh': (CAT, u"Joode Leß"), 'no': (BACK, u'God liste'), 'pl': (BACK, u'Medalista'), 'pt': (BACK, u'Anexo destacado'), @@ -295,7 +300,8 @@ 'zh': (BACK, u'特色列表'), }
-# Third parameter is the sort key indicating articles to hide from the given list +# Third parameter is the sort key indicating articles to hide from the given +# list former_name = { 'ca': (CAT, u"Arxiu de propostes de la retirada de la distinció"), 'en': (CAT, u"Wikipedia former featured articles", "#"), @@ -310,8 +316,9 @@ 'zh': (CAT, u"已撤销的特色条目"), }
+ def featuredArticles(site, pType): - articles=[] + articles = [] if pType == 'good': info = good_name elif pType == 'former': @@ -335,10 +342,10 @@ hide = None raw = method(site, name, hide) for p in raw: - if p.namespace() == 0: # Article + if p.namespace() == 0: # Article articles.append(p) # Article talk (like in English) - elif p.namespace() == 1 and site.lang <> 'el': + elif p.namespace() == 1 and site.lang != 'el': articles.append(pywikibot.Page(p.site(), p.title(withNamespace=False))) pywikibot.output( @@ -347,30 +354,34 @@ for p in articles: yield copy(p)
+ def findTranslated(page, oursite=None, quiet=False): if not oursite: - oursite=pywikibot.getSite() + oursite = pywikibot.getSite() if page.isRedirectPage(): page = page.getRedirectTarget() try: - iw=page.interwiki() + iw = page.interwiki() except: pywikibot.output(u"%s -> no interwiki, giving up" % page.title()) - return None - ourpage=None + return + + ourpage = None for p in iw: - if p.site()==oursite: - ourpage=p + if p.site() == oursite: + ourpage = p break if not ourpage: if not quiet: pywikibot.output(u"%s -> no corresponding page in %s" % (page.title(), oursite)) - return None + return + if not ourpage.exists(): pywikibot.output(u"%s -> our page doesn't exist: %s" % (page.title(), ourpage.title())) - return None + return + if ourpage.isRedirectPage(): ourpage = ourpage.getRedirectTarget() pywikibot.output(u"%s -> corresponding page is %s" @@ -378,25 +389,31 @@ if ourpage.namespace() != 0: pywikibot.output(u"%s -> not in the main namespace, skipping" % page.title()) - return None + return + if ourpage.isRedirectPage(): pywikibot.output(u"%s -> double redirect, skipping" % page.title()) - return None + return + if not ourpage.exists(): - pywikibot.output(u"%s -> page doesn't exist, skipping" % ourpage.title()) - return None + pywikibot.output(u"%s -> page doesn't exist, skipping" + % ourpage.title()) + return + try: iw = ourpage.interwiki() except: - return None - backpage=None + return + + backpage is None for p in iw: if p.site() == page.site(): backpage = p break if not backpage: pywikibot.output(u"%s -> no back interwiki ref" % page.title()) - return None + return + if backpage == page: # everything is ok return ourpage @@ -407,9 +424,9 @@ return ourpage pywikibot.output(u"%s -> back interwiki ref target is %s" % (page.title(), backpage.title())) - return None
-def getTemplateList (lang, pType): + +def getTemplateList(lang, pType): if pType == 'good': try: templates = template_good[lang] @@ -422,7 +439,7 @@ templates += template_lists['_default'] except KeyError: templates = template_lists['_default'] - else: #pType in ['former', 'featured'] + else: # pType in ['former', 'featured'] try: templates = template[lang] templates += template['_default'] @@ -430,6 +447,7 @@ templates = template['_default'] return templates
+ def featuredWithInterwiki(fromsite, tosite, template_on_top, pType, quiet, dry=False): if not fromsite.lang in cache: @@ -438,36 +456,39 @@ cache[fromsite.lang][tosite.lang] = {} cc = cache[fromsite.lang][tosite.lang] if nocache: - cc={} + cc = {} templatelist = getTemplateList(tosite.lang, pType) findtemplate = '(' + '|'.join(templatelist) + ')' - re_Link_FA=re.compile(ur"{{%s|%s}}" - % (findtemplate.replace(u' ', u'[ _]'), - fromsite.lang), re.IGNORECASE) + re_Link_FA = re.compile(ur"{{%s|%s}}" + % (findtemplate.replace(u' ', u'[ _]'), + fromsite.lang), re.IGNORECASE) gen = featuredArticles(fromsite, pType) gen = PreloadingGenerator(gen) - - pairs=[] + pairs = [] for a in gen: if a.title() < afterpage: continue + if u"/" in a.title() and a.namespace() != 0: pywikibot.output(u"%s is a subpage" % a.title()) continue + if a.title() in cc: - pywikibot.output(u"(cached) %s -> %s"%(a.title(), cc[a.title()])) + pywikibot.output(u"(cached) %s -> %s" % (a.title(), cc[a.title()])) continue + if a.isRedirectPage(): - a=a.getRedirectTarget() + a = a.getRedirectTarget() try: if not a.exists(): pywikibot.output(u"source page doesn't exist: %s" % a.title()) continue + atrans = findTranslated(a, tosite, quiet) - if pType!='former': + if pType != 'former': if atrans: - text=atrans.get() - m=re_Link_FA.search(text) + text = atrans.get() + m = re_Link_FA.search(text) if m: pywikibot.output(u"(already done)") else: @@ -475,14 +496,13 @@ if (not interactive or pywikibot.input( u'Connecting %s -> %s. Proceed? [Y/N]' - % (a.title(), atrans.title())) in ['Y', 'y'] - ): + % (a.title(), atrans.title())) in ['Y', 'y']): site = pywikibot.getSite() comment = pywikibot.setAction( i18n.twtranslate(site, 'featured-' + pType, {'page': unicode(a)})) ### Moving {{Link FA|xx}} to top of interwikis ### - if template_on_top == True: + if template_on_top: # Getting the interwiki iw = pywikibot.getLanguageLinks(text, site) # Removing the interwiki @@ -493,35 +513,35 @@ text = pywikibot.replaceLanguageLinks(text, iw, site)
- ### Placing {{Link FA|xx}} right next to corresponding interwiki ### + # Placing {{Link FA|xx}} right next to + # corresponding interwiki else: - text=(text[:m.end()] - + (u" {{%s|%s}}" % (templatelist[0], - fromsite.lang)) - + text[m.end():]) + text = (text[:m.end()] + + (u" {{%s|%s}}" % (templatelist[0], + fromsite.lang)) + + text[m.end():]) if not dry: try: atrans.put(text, comment) except pywikibot.LockedPage: pywikibot.output(u'Page %s is locked!' % atrans.title()) - cc[a.title()]=atrans.title() + cc[a.title()] = atrans.title() else: if atrans: - text=atrans.get() - m=re_Link_FA.search(text) + text = atrans.get() + m = re_Link_FA.search(text) if m: # insert just before interwiki if (not interactive or pywikibot.input( u'Connecting %s -> %s. Proceed? [Y/N]' - % (a.title(), atrans.title())) in ['Y', 'y'] - ): + % (a.title(), atrans.title())) in ['Y', 'y']): site = pywikibot.getSite() comment = pywikibot.setAction( i18n.twtranslate(site, 'featured-former', {'page': unicode(a)})) - text = re.sub(re_Link_FA,'',text) + text = re.sub(re_Link_FA, '', text) if not dry: try: atrans.put(text, comment) @@ -530,10 +550,11 @@ % atrans.title()) else: pywikibot.output(u"(already done)") - cc[a.title()]=atrans.title() + cc[a.title()] = atrans.title() except pywikibot.PageNotSaved, e: pywikibot.output(u"Page not saved")
+ def main(*args): global nocache, interactive, afterpage, cache nocache = 0 @@ -543,23 +564,23 @@
template_on_top = True featuredcount = False - fromlang=[] + fromlang = [] processType = 'featured' doAll = False - part = False + part = False quiet = False for arg in pywikibot.handleArgs(): if arg == '-interactive': - interactive=1 + interactive = 1 elif arg == '-nocache': - nocache=1 + nocache = 1 elif arg.startswith('-fromlang:'): - fromlang=arg[10:].split(",") + fromlang = arg[10:].split(",") part = True elif arg == '-fromall': doAll = True elif arg.startswith('-after:'): - afterpage=arg[7:] + afterpage = arg[7:] elif arg == '-side': template_on_top = False elif arg == '-count': @@ -578,8 +599,10 @@ # BUG: range with zh-min-nan (3 "-") if len(fromlang) == 1 and fromlang[0].index("-") >= 0: start, end = fromlang[0].split("--", 1) - if not start: start = "" - if not end: end = "zzzzzzz" + if not start: + start = "" + if not end: + end = "zzzzzzz" if processType == 'good': fromlang = [lang for lang in good_name.keys() if lang >= start and lang <= end] @@ -605,11 +628,11 @@ else: fromlang = featured_name.keys()
- filename="cache/" + processType + filename = "cache/" + processType try: - cache=pickle.load(file(filename,"rb")) + cache = pickle.load(file(filename, "rb")) except: - cache={} + cache = {}
if not fromlang: pywikibot.showHelp('featured') @@ -625,6 +648,7 @@ if t.exists(): hasTemplate = True break + try: for ll in fromlang: fromsite = pywikibot.getSite(ll) @@ -639,7 +663,7 @@ % (processType, pywikibot.getSite().lang)) pywikibot.output('Quitting program...') break - elif fromsite != pywikibot.getSite(): + elif fromsite != pywikibot.getSite(): featuredWithInterwiki(fromsite, pywikibot.getSite(), template_on_top, processType, quiet, pywikibot.simulate) @@ -649,6 +673,7 @@ if not nocache: pickle.dump(cache,file(filename,"wb"))
+ if __name__ == "__main__": try: main()
pywikipedia-svn@lists.wikimedia.org