Revision: 5890 Author: russblau Date: 2008-09-12 19:13:48 +0000 (Fri, 12 Sep 2008)
Log Message: ----------- update more site methods and tests
Modified Paths: -------------- branches/rewrite/pywikibot/__init__.py branches/rewrite/pywikibot/login.py branches/rewrite/pywikibot/site.py branches/rewrite/pywikibot/tests/site_tests.py
Modified: branches/rewrite/pywikibot/__init__.py =================================================================== --- branches/rewrite/pywikibot/__init__.py 2008-09-12 14:56:16 UTC (rev 5889) +++ branches/rewrite/pywikibot/__init__.py 2008-09-12 19:13:48 UTC (rev 5890) @@ -34,11 +34,11 @@ """ logger = logging.getLogger("wiki")
- if code == None: + if code is None: code = default_code - if fam == None: + if fam is None: fam = default_family - if user == None: + if user is None: try: user = config.usernames[fam][code] except KeyError:
Modified: branches/rewrite/pywikibot/login.py =================================================================== --- branches/rewrite/pywikibot/login.py 2008-09-12 14:56:16 UTC (rev 5889) +++ branches/rewrite/pywikibot/login.py 2008-09-12 19:13:48 UTC (rev 5890) @@ -70,12 +70,14 @@
class LoginManager: - def __init__(self, password = None, sysop = False, site = None): + def __init__(self, password=None, sysop=False, site=None, user=None): if site is not None: self.site = site else: self.site = pywikibot.Site() - if sysop: + if user: + self.username = user + elif sysop: try: self.username = config.sysopnames\ [self.site.family.name][self.site.code]
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2008-09-12 14:56:16 UTC (rev 5889) +++ branches/rewrite/pywikibot/site.py 2008-09-12 19:13:48 UTC (rev 5890) @@ -114,6 +114,7 @@ @property def throttle(self): """Return this Site's throttle. Initialize a new one if needed.""" + if not hasattr(self, "_throttle"): self._throttle = Throttle(self, multiplydelay=True, verbosedelay=True) try: @@ -125,16 +126,28 @@ @property def family(self): """The Family object for this Site's wiki family.""" + return self.__family
@property def code(self): """The identifying code for this Site.""" + return self.__code
+ @property + def lang(self): + """The ISO language code for this Site. + + Presumed to be equal to the wiki prefix, but this can be overridden. + + """ + return self.__code + def __cmp__(self, other): """Perform equality and inequality tests on Site objects.""" - if not isinstance(other, Site): + + if not isinstance(other, BaseSite): return 1 if self.family == other.family: return cmp(self.code, other.code) @@ -142,12 +155,14 @@
def user(self): """Return the currently-logged in bot user, or None.""" + if self.logged_in(): return self._username return None
def __getattr__(self, attr): """Calls to methods not defined in this object are passed to Family.""" + if hasattr(self.__class__, attr): return self.__class__.attr try: @@ -163,6 +178,7 @@
def sitename(self): """Return string representing this Site's name and language.""" + return self.family.name+':'+self.code
__str__ = sitename @@ -174,15 +190,34 @@ return hash(repr(self))
def linktrail(self): - """Return regex for trailing chars displayed as part of a link.""" + """Return regex for trailing chars displayed as part of a link. + + Returns a string, not a compiled regular expression object. + + This reads from the family file, and ''not'' from + [[MediaWiki:Linktrail]], because the MW software currently uses a + built-in linktrail from its message files and ignores the wiki + value. + + """ return self.family.linktrail(self.code)
def languages(self): """Return list of all valid language codes for this site's Family.""" + return self.family.langs.keys()
+ + def validLanguageLinks(self): + """Return list of language codes that can be used in interwiki links.""" + + nsnames = sum(self.namespaces().values(), []) + return [l for l in self.languages() + if l[:1].upper() + l[1:] not in self.namespaces()] + def ns_index(self, namespace): """Given a namespace name, return its int index, or None if invalid.""" + for ns in self.namespaces(): if namespace.lower() in [name.lower() for name in self.namespaces()[ns]]: @@ -193,6 +228,7 @@
def namespaces(self): """Return dict of valid namespaces on this wiki.""" + return self._namespaces
def ns_normalize(self, value): @@ -258,7 +294,7 @@ """Return Category in which disambig pages are listed."""
try: - name = self.namespace(14)+':'+self.family.disambcatname[self.code]) + name = self.namespace(14)+':'+self.family.disambcatname[self.code] except KeyError: raise Error(u"No disambiguation category name found for %(site)s" % {'site': self}) @@ -338,10 +374,12 @@
def category_on_one_line(self): """Return True if this site wants all category links on one line.""" + return self.code in self.family.category_on_one_line
def interwiki_putfirst(self): """Return list of language codes for ordering of interwiki links.""" + return self.family.interwiki_putfirst.get(self.code, None)
def getSite(self, code): @@ -447,7 +485,6 @@ } self.sitelock = threading.Lock() self._msgcache = {} - self._username = "" return
# ANYTHING BELOW THIS POINT IS NOT YET IMPLEMENTED IN __init__() @@ -493,18 +530,11 @@ self._getsiteinfo() # check whether a login cookie already exists for this user if hasattr(self, "_userinfo"): - try: - if sysop: - name = config.sysopnames[self.family.name][self.code] - else: - name = config.usernames[self.family.name][self.code] - if self._userinfo['name'] == name: - self._username = name - except KeyError: - # no username for this site - pass + if self._userinfo['name'] == self._username: + return if not self.logged_in(sysop): - loginMan = api.LoginManager(site=self, sysop=sysop) + loginMan = api.LoginManager(site=self, sysop=sysop, + user=self._username) if loginMan.login(retry = True): self._username = loginMan.username if hasattr(self, "_userinfo"): @@ -678,20 +708,25 @@ @property def siteinfo(self): """Site information dict.""" + if not hasattr(self, "_siteinfo"): self._getsiteinfo() return self._siteinfo
def case(self): + return self.siteinfo['case']
def language(self): """Return the code for the language of this Site.""" - # N.B. this code may or may not be the same as self.code + return self.siteinfo['lang']
+ lang = property(fget=language, doc=language.__doc__) + def namespaces(self): """Return dict of valid namespaces on this wiki.""" + if not hasattr(self, "_siteinfo"): self._getsiteinfo() return self._namespaces @@ -2654,7 +2689,3 @@ return False else: return False - - def validLanguageLinks(self): - """Return list of language codes that can be used in interwiki links.""" - return self._validlanguages
Modified: branches/rewrite/pywikibot/tests/site_tests.py =================================================================== --- branches/rewrite/pywikibot/tests/site_tests.py 2008-09-12 14:56:16 UTC (rev 5889) +++ branches/rewrite/pywikibot/tests/site_tests.py 2008-09-12 19:13:48 UTC (rev 5890) @@ -23,11 +23,29 @@ """Test cases for BaseSite methods""" self.assertEqual(mysite.family.name, pywikibot.config.family) self.assertEqual(mysite.code, pywikibot.config.mylang) + self.assertTrue(isinstance(mysite.language(), basestring)) + self.assertTrue(isinstance(mysite == pywikibot.Site("en", "wikipedia"), + bool)) self.assertTrue(isinstance(mysite.user(), (basestring, type(None)))) self.assertEqual(mysite.sitename(), "%s:%s" % (pywikibot.config.family, pywikibot.config.mylang)) + self.assertEqual(repr(mysite), + 'Site("%s", "%s")' + % (pywikibot.config.mylang, pywikibot.config.family)) + self.assertTrue(isinstance(mysite.linktrail(), basestring)) + langs = mysite.languages() + self.assertTrue(isinstance(langs, list)) + self.assertTrue(mysite.code in langs) + self.assertEqual(mysite.ns_index("Talk"), 1) + ns = mysite.namespaces() + self.assertTrue(isinstance(ns, dict)) + for x in xrange(0, 16): # built-in namespaces always present + self.assertTrue(x in ns) + self.assertTrue(isinstance(ns[x], list)) self.assertTrue(isinstance(mysite.ns_normalize("project"), basestring)) self.assertTrue(isinstance(mysite.redirect(), basestring)) + self.assertTrue(isinstance(mysite.disambcategory(), pywikibot.Category)) + self.assertTrue(isinstance(mysite.redirectRegex().pattern, basestring))
def testApiMethods(self): """Test generic ApiSite methods""" @@ -78,34 +96,34 @@ self.assertTrue(isinstance(mysite.token(mainpage, ttype), basestring))
- def testLinkMethods(self): - """Test site methods for getting links to and from a page""" - - backlinks = set(mysite.pagebacklinks(mainpage)) - embedded = set(mysite.page_embeddedin(mainpage)) - refs = set(mysite.pagereferences(mainpage)) - for bl in backlinks: - self.assertTrue(isinstance(bl, pywikibot.Page)) - self.assertTrue(bl in refs) - for ei in embedded: - self.assertTrue(isinstance(ei, pywikibot.Page)) - self.assertTrue(ei in refs) - for ref in refs: - self.assertTrue(ref in backlinks or ref in embedded) - for pl in mysite.pagelinks(mainpage): - self.assertTrue(isinstance(pl, pywikibot.Page)) - for cat in mysite.pagecategories(mainpage): - self.assertTrue(isinstance(cat, pywikibot.Category)) - for cm in mysite.categorymembers(cat): - self.assertTrue(isinstance(cat, pywikibot.Page)) - self.assertTrue(all(isinstance(im, pywikibot.ImagePage) - for im in mysite.pageimages(mainpage))) - self.assertTrue(all(isinstance(te, pywikibot.Page) - for te in mysite.pagetemplates(mainpage))) - for ll in mysite.pagelanglinks(mainpage): - self.assertTrue(isinstance(ll, pywikibot.Link)) - self.assertTrue(all(isinstance(el, basestring) - for el in mysite.page_extlinks(mainpage))) +## def testLinkMethods(self): +## """Test site methods for getting links to and from a page""" +## +## backlinks = set(mysite.pagebacklinks(mainpage)) +## embedded = set(mysite.page_embeddedin(mainpage)) +## refs = set(mysite.pagereferences(mainpage)) +## for bl in backlinks: +## self.assertTrue(isinstance(bl, pywikibot.Page)) +## self.assertTrue(bl in refs) +## for ei in embedded: +## self.assertTrue(isinstance(ei, pywikibot.Page)) +## self.assertTrue(ei in refs) +## for ref in refs: +## self.assertTrue(ref in backlinks or ref in embedded) +## for pl in mysite.pagelinks(mainpage): +## self.assertTrue(isinstance(pl, pywikibot.Page)) +## for cat in mysite.pagecategories(mainpage): +## self.assertTrue(isinstance(cat, pywikibot.Category)) +## for cm in mysite.categorymembers(cat): +## self.assertTrue(isinstance(cat, pywikibot.Page)) +## self.assertTrue(all(isinstance(im, pywikibot.ImagePage) +## for im in mysite.pageimages(mainpage))) +## self.assertTrue(all(isinstance(te, pywikibot.Page) +## for te in mysite.pagetemplates(mainpage))) +## for ll in mysite.pagelanglinks(mainpage): +## self.assertTrue(isinstance(ll, pywikibot.Link)) +## self.assertTrue(all(isinstance(el, basestring) +## for el in mysite.page_extlinks(mainpage)))
def testLoadRevisions(self): """Test the site.loadrevisions() method"""
pywikipedia-l@lists.wikimedia.org