http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10422
Revision: 10422
Author: valhallasw
Date: 2012-06-26 19:10:02 +0000 (Tue, 26 Jun 2012)
Log Message:
-----------
Added must_be decorator to use with site methods. This decorator will make sure
the user is logged in (as 'user' or as 'sysop', currently) before the method is
run.
+ tests
+ applied to APISite.editpage
Modified Paths:
--------------
branches/rewrite/pywikibot/site.py
branches/rewrite/tests/dry_site_tests.py
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2012-06-26 19:05:50 UTC (rev 10421)
+++ branches/rewrite/pywikibot/site.py 2012-06-26 19:10:02 UTC (rev 10422)
@@ -579,6 +579,36 @@
raise NotImplementedError
+def must_be(group=None,right=None):
+ """ Decorator to require a certain user status. For now, only the values
+ group = 'user' and group = 'sysop' are supported. The right property
+ will be ignored for now.
+
+ @param group: the group the logged in user should belong to
+ legal values: 'user' and 'sysop'
+ @param right: the rights the logged in user hsould have
+ not supported yet and thus ignored.
+ @returns: a decorator to make sure the requirement is statisfied when
+ the decorated function is called.
+ """
+ if group == 'user':
+ run = lambda self: self.login(False)
+ elif group == 'sysop':
+ run = lambda self: self.login(True)
+ else:
+ raise Exception("Not implemented")
+
+ def decorator(fn):
+ def callee(self, *args, **kwargs):
+ run(self)
+ return fn(self, *args, **kwargs)
+ callee.__name__ = fn.__name__
+ callee.__doc__ = fn.__doc__
+ return callee
+
+ return decorator
+
+
class APISite(BaseSite):
"""API interface to MediaWiki site.
@@ -2363,6 +2393,7 @@
"editconflict": "Page %(title)s not saved due to edit conflict.",
}
+ @must_be(group='user')
def editpage(self, page, summary, minor=True, notminor=False,
bot=True, recreate=True, createonly=False, watch=None):
"""Submit an edited Page object to be saved to the wiki.
Modified: branches/rewrite/tests/dry_site_tests.py
===================================================================
--- branches/rewrite/tests/dry_site_tests.py 2012-06-26 19:05:50 UTC (rev 10421)
+++ branches/rewrite/tests/dry_site_tests.py 2012-06-26 19:10:02 UTC (rev 10422)
@@ -22,3 +22,30 @@
x._userinfo['groups'] = ['sysop']
assert x.logged_in(True)
assert not x.logged_in(False)
+
+class SiteMock(object):
+ last_login = None
+ last_fn_called = False
+
+ def login(self, as_sysop):
+ self.last_login = 'sysop' if as_sysop else 'user'
+
+ def inner_fn(self, *args, **kwargs):
+ self.last_fn_called = (args, kwargs)
+ return (args, kwargs)
+
+def test_must_be_user():
+ x = SiteMock()
+ wrapped_inner = pywikibot.site.must_be(group='user')(x.inner_fn)
+ assert(wrapped_inner(x,1,2,3,a='a', b='b') == ((x,1,2,3), {'a': 'a', 'b': 'b'}))
+ assert(x.last_fn_called == ((x,1,2,3), {'a': 'a', 'b': 'b'}))
+ assert(x.last_login == 'user')
+
+def test_must_be_sysop():
+ x = SiteMock()
+ wrapped_inner = pywikibot.site.must_be(group='sysop')(x.inner_fn)
+ assert(wrapped_inner(x,1,2,3,a='a', b='b') == ((x,1,2,3), {'a': 'a', 'b': 'b'}))
+ assert(x.last_fn_called == ((x,1,2,3), {'a': 'a', 'b': 'b'}))
+ assert(x.last_login == 'sysop')
+
+
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10421
Revision: 10421
Author: valhallasw
Date: 2012-06-26 19:05:50 +0000 (Tue, 26 Jun 2012)
Log Message:
-----------
Changed behaviour of site.logged_in back to original and updated docs & tests to reflect this.
site.logged_in will return False if the user is logged in as a different user than requested.
Modified Paths:
--------------
branches/rewrite/pywikibot/site.py
branches/rewrite/tests/dry_site_tests.py
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2012-06-24 18:26:16 UTC (rev 10420)
+++ branches/rewrite/pywikibot/site.py 2012-06-26 19:05:50 UTC (rev 10421)
@@ -679,9 +679,11 @@
return gen
def logged_in(self, sysop=False):
- """Return True if logged in with specified privileges, otherwise False.
+ """Return True if logged in with the user specified in user-config.py
+ (or the sysop user specified if the sysop parameter is True).
- @param sysop: if True, require sysop privileges.
+ @param sysop: if True, test if user is logged in as the sysop user
+ instead of the normal user.
"""
if not hasattr(self, "_userinfo"):
@@ -694,7 +696,7 @@
return False
if self.userinfo['name'] != self._username[sysop]:
- pywikibot.warning("Logged in as %s instead of %s" % (self.userinfo['name'], self._username[sysop]))
+ return False
return True
Modified: branches/rewrite/tests/dry_site_tests.py
===================================================================
--- branches/rewrite/tests/dry_site_tests.py 2012-06-24 18:26:16 UTC (rev 10420)
+++ branches/rewrite/tests/dry_site_tests.py 2012-06-26 19:05:50 UTC (rev 10421)
@@ -21,4 +21,4 @@
x._userinfo['name'] = 'sysop_user'
x._userinfo['groups'] = ['sysop']
assert x.logged_in(True)
- assert x.logged_in(False)
\ No newline at end of file
+ assert not x.logged_in(False)
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10417
Revision: 10417
Author: multichill
Date: 2012-06-23 16:12:26 +0000 (Sat, 23 Jun 2012)
Log Message:
-----------
Add the EdittimeFilterPageGenerator() generator filter.
Modified Paths:
--------------
trunk/pywikipedia/pagegenerators.py
Modified: trunk/pywikipedia/pagegenerators.py
===================================================================
--- trunk/pywikipedia/pagegenerators.py 2012-06-23 16:10:55 UTC (rev 10416)
+++ trunk/pywikipedia/pagegenerators.py 2012-06-23 16:12:26 UTC (rev 10417)
@@ -28,6 +28,7 @@
import re
import sys
import codecs
+import datetime
import urllib, urllib2, time
import date, catlib, userlib, query
@@ -1131,6 +1132,22 @@
yield page
break
+def EdittimeFilterPageGenerator(generator, begintime=datetime.datetime.min, endtime=datetime.datetime.max):
+ """
+ Wraps around another generator. Yields only those pages which were changed
+ between begintime and endtime.
+
+ @param generator: A generator object
+ @param begintime: A datetime object. Only pages after this time will be returned.
+ @param endtime: A datetime object Only pages before this time will be returned.
+ """
+ for page in generator:
+ if page.editTime(datetime=True)==None:
+ # FIXME: The page object should probably handle this
+ page.get()
+ if page.editTime(datetime=True) and begintime < page.editTime(datetime=True) and page.editTime(datetime=True) < endtime:
+ yield page
+
def CombinedPageGenerator(generators):
"""
Wraps around a list of other generators. Yields all pages generated by
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10416
Revision: 10416
Author: multichill
Date: 2012-06-23 16:10:55 +0000 (Sat, 23 Jun 2012)
Log Message:
-----------
Set self._editTime to None in the page object if it has not been fetched yet.
Add option to get the editTime() of a page object as a datetime object.
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2012-06-23 06:13:55 UTC (rev 10415)
+++ trunk/pywikipedia/wikipedia.py 2012-06-23 16:10:55 UTC (rev 10416)
@@ -418,7 +418,7 @@
self._permalink = None
self._userName = None
self._ipedit = None
- self._editTime = '0'
+ self._editTime = None
self._startTime = '0'
# For the Flagged Revisions MediaWiki extension
self._revisionId = None
@@ -1260,12 +1260,16 @@
"""
return self._ipedit
- def editTime(self):
+ def editTime(self, datetime=False):
"""Return timestamp (in MediaWiki format) of last revision to page.
- Returns None if last edit time is unknown.
+ Returns None unless page was retrieved with getAll() or _getEditPage().
"""
+ if self._editTime and datetime:
+ import datetime
+ return datetime.datetime.strptime(str(self._editTime), '%Y%m%d%H%M%S')
+
return self._editTime
def previousRevision(self):