Revision: 7991 Author: xqt Date: 2010-03-12 09:03:31 +0000 (Fri, 12 Mar 2010)
Log Message: ----------- site.getmagicwords() from site.siteinfo() instead from family file
Modified Paths: -------------- trunk/pywikipedia/family.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/family.py =================================================================== --- trunk/pywikipedia/family.py 2010-03-12 08:58:10 UTC (rev 7990) +++ trunk/pywikipedia/family.py 2010-03-12 09:03:31 UTC (rev 7991) @@ -1,9 +1,17 @@ # -*- coding: utf-8 -*- -import config, urllib, re -from datetime import timedelta, datetime
+# +# (C) Pywikipedia bot team, 2004-2010 +# +# Distributed under the terms of the MIT license. +# __version__='$Id$'
+import config +import re +import urllib +from datetime import timedelta, datetime + # Parent class for all wiki families
class Family: @@ -2678,8 +2686,8 @@ }, }
- # letters that can follow a wikilink and are regarded as part - # of this link + # letters that can follow a wikilink and are regarded as part of + # this link # This depends on the linktrail setting in LanguageXx.php and on # [[MediaWiki:Linktrail]]. # Note: this is a regular expression. @@ -3113,7 +3121,9 @@
# A list with the name for cross-project cookies. # default for wikimedia centralAuth extensions. - self.cross_projects_cookies = ['centralauth_Session', 'centralauth_Token', 'centralauth_User'] + self.cross_projects_cookies = ['centralauth_Session', + 'centralauth_Token', + 'centralauth_User'] self.cross_projects_cookie_username = 'centralauth_User'
# A list with the name in the cross-language flag permissions @@ -3449,51 +3459,6 @@ def category_namespaces(self, code): return self.namespace(code, 14, all = True)
- # So can be pagename code - pagename = { - 'bg': [u'СТРАНИЦА'], - 'he': [u'שם הדף'], - 'kk': [u'БЕТАТАУЫ'], - 'nn': ['SIDENAMN', 'SIDENAVN'], - 'ru': [u'НАЗВАНИЕСТРАНИЦЫ'], - 'sr': [u'СТРАНИЦА'], - 'tt': [u'BİTİSEME'] - } - - pagenamee = { - 'he': [u'שם הדף מקודד'], - 'kk': [u'БЕТАТАУЫ2'], - 'nn': ['SIDENAMNE', 'SIDENAVNE'], - 'ru': [u'НАЗВАНИЕСТРАНИЦЫ2'], - 'sr': [u'СТРАНИЦЕ'] - } - - def pagenamecodes(self, code): - pos = ['PAGENAME'] - pos2 = [] - if code in self.pagename: - pos = pos + self.pagename[code] - elif code == 'als': - return self.pagenamecodes('de') - elif code == 'bm': - return self.pagenamecodes('fr') - for p in pos: - pos2 += [p, p.lower()] - return pos2 - - def pagename2codes(self, code): - pos = ['PAGENAME'] - pos2 = [] - if code in self.pagenamee: - pos = pos + self.pagenamee[code] - elif code == 'als': - return self.pagename2codes('de') - elif code == 'bm': - return self.pagename2codes('fr') - for p in pos: - pos2 += [p, p.lower()] - return pos2 - # Methods def protocol(self, code): """
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2010-03-12 08:58:10 UTC (rev 7990) +++ trunk/pywikipedia/wikipedia.py 2010-03-12 09:03:31 UTC (rev 7991) @@ -2156,7 +2156,7 @@ text = self.get()
# Replace {{PAGENAME}} by its value - for pagenametext in self.site().family.pagenamecodes( + for pagenametext in self.site().pagenamecodes( self.site().language()): text = text.replace(u"{{%s}}" % pagenametext, self.title())
@@ -2363,8 +2363,8 @@ # {{#if: }} if name.startswith('#'): continue - # {{DEFAULTSORT:...}} or {{#if: }} - defaultKeys = self.site().siteinfo('magicwords')['defaultsort'] + # {{DEFAULTSORT:...}} + defaultKeys = self.site().getmagicwords('defaultsort') found = False for key in defaultKeys: if name.startswith(key): @@ -6757,19 +6757,18 @@ return True return False
- def redirect(self, default = False): + def getmagicwords(self, word): + """Return list of localized "word" magic words for the site.""" + return self.siteinfo('magicwords').get(word) + + def redirect(self, default=False): """Return the localized redirect tag for the site.
- If default is True, falls back to 'REDIRECT' if the site has no - special redirect tag. + Argument is ignored (but maintained for backwards-compatibility).
""" - tag = self.siteinfo('magicwords').get('redirect') - if tag: - # remove first "#" letter - return tag[0][1:] - elif default: - return u'REDIRECT' + # return the magic word without the preceding '#' character + return self.getmagicwords('redirect')[0].lstrip("#")
def redirectRegex(self): """Return a compiled regular expression matching on redirect pages. @@ -6780,7 +6779,7 @@ #NOTE: this is needed, since the API can give false positives! default = 'REDIRECT' try: - keywords = self.siteinfo('magicwords')['redirect'] + keywords = self.getmagicwords('redirect') pattern = r'(?:' + '|'.join(keywords) + ')' except KeyError: # no localized keyword for redirects @@ -6797,6 +6796,14 @@ + '\s*:?\s*[[(.+?)(?:|.*?)?]]', re.IGNORECASE | re.UNICODE | re.DOTALL)
+ def pagenamecodes(self, default=True): + """Return list of localized PAGENAME tags for the site.""" + return self.getmagicwords('pagename') + + def pagename2codes(self, default=True): + """Return list of localized PAGENAMEE tags for the site.""" + return self.getmagicwords('pagenamee') + def resolvemagicwords(self, wikitext): """Replace the {{ns:xx}} marks in a wikitext with the namespace names"""