Revision: 6401
Author: russblau
Date: 2009-02-21 20:54:03 +0000 (Sat, 21 Feb 2009)
Log Message:
-----------
Better interface for setting query parameters (especially namespace filters)
Modified Paths:
--------------
branches/rewrite/pywikibot/data/api.py
branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py 2009-02-21 20:18:12 UTC (rev 6400)
+++ branches/rewrite/pywikibot/data/api.py 2009-02-21 20:54:03 UTC (rev 6401)
@@ -318,7 +318,7 @@
if name not in _modules:
self.get_module()
break
- self.set_limit()
+ self.update_limit()
if self.query_limit is not None and "generator" in kwargs:
self.prefix = "g" + self.prefix
self.request = Request(**kwargs)
@@ -346,7 +346,29 @@
raise Error("Invalid query module name '%s'." % self.module)
_modules[paraminfo["name"]] = paraminfo
- def set_limit(self):
+ def set_query_increment(self, value):
+ """Set the maximum number of items to be retrieved per API query.
+
+ If not called, the default is to ask for "max" items and let the
+ API decide how many to send.
+
+ """
+ limit = int(value)
+ # don't update if limit is greater than maximum allowed by API
+ self.update_limit()
+ if self.query_limit is None or limit < self.query_limit:
+ self.query_limit = int(limit)
+
+ def set_query_item_limit(self, value):
+ """Set the maximum number of items to be retrieved from the wiki.
+
+ If not called, most queries will continue as long as there is
+ more data to be retrieved from the API.
+
+ """
+ self.limit = int(value)
+
+ def update_limit(self):
"""Set query_limit for self.module based on api response"""
self.query_limit = None
@@ -365,6 +387,22 @@
self.query_limit))
return
+ def set_namespace(self, namespaces):
+ """Set a namespace filter on this query.
+
+ @param namespaces: Either an int or a list of ints
+
+ """
+ if isinstance(namespaces, list):
+ namespaces = "|".join(str(n) for n in namespaces)
+ else:
+ namespaces = str(namespaces)
+ for mod in self.module.split('|'):
+ for param in _modules[mod].get("parameters", []):
+ if param["name"] == "namespace":
+ self.request[self.prefix+"namespace"] = namespaces
+ return
+
def __iter__(self):
"""Submit request and iterate the response based on self.resultkey
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2009-02-21 20:18:12 UTC (rev 6400)
+++ branches/rewrite/pywikibot/site.py 2009-02-21 20:54:03 UTC (rev 6401)
@@ -1073,11 +1073,8 @@
"""
bltitle = page.title(withSection=False).encode(self.encoding())
blgen = api.PageGenerator("backlinks", gbltitle=bltitle, site=self)
- if isinstance(namespaces, list):
- blgen.request["gblnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- blgen.request["gblnamespace"] = str(namespaces)
+ if namespaces is not None:
+ blgen.set_namespace(namespaces)
if filterRedirects is not None:
blgen.request["gblfilterredir"] = filterRedirects and "redirects"\
or "nonredirects"
@@ -1114,11 +1111,8 @@
"""
eititle = page.title(withSection=False).encode(self.encoding())
eigen = api.PageGenerator("embeddedin", geititle=eititle, site=self)
- if isinstance(namespaces, list):
- eigen.request["geinamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- eigen.request["geinamespace"] = str(namespaces)
+ if namespaces is not None:
+ eigen.set_namespace(namespaces)
if filterRedirects is not None:
eigen.request["geifilterredir"] = filterRedirects and "redirects"\
or "nonredirects"
@@ -1162,11 +1156,8 @@
plgen.request['titles'] = pltitle
if follow_redirects:
plgen.request['redirects'] = ''
- if isinstance(namespaces, list):
- plgen.request["gplnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- plgen.request["gplnamespace"] = str(namespaces)
+ if namespaces is not None:
+ plgen.set_namespace(namespaces)
return plgen
@deprecate_arg("withSortKey", None) # Sortkey doesn't work with generator
@@ -1193,11 +1184,8 @@
tltitle = page.title(withSection=False).encode(self.encoding())
tlgen = api.PageGenerator("templates", titles=tltitle, site=self)
- if isinstance(namespaces, list):
- tlgen.request["gtlnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- tlgen.request["gtlnamespace"] = str(namespaces)
+ if namespaces is not None:
+ tlgen.set_namespace(namespaces)
return tlgen
def categorymembers(self, category, namespaces=None, limit=None):
@@ -1221,11 +1209,8 @@
cmtitle = category.title(withSection=False).encode(self.encoding())
cmgen = api.PageGenerator("categorymembers", gcmtitle=cmtitle,
gcmprop="ids|title|sortkey", site=self)
- if isinstance(namespaces, list):
- cmgen.request["gcmnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- cmgen.request["gcmnamespace"] = str(namespaces)
+ if namespaces is not None:
+ cmgen.set_namespace(namespaces)
if isinstance(limit, int):
cmgen.limit = limit
return cmgen
@@ -1676,11 +1661,8 @@
"""
eugen = api.PageGenerator("exturlusage", geuquery=url,
geuprotocol=protocol, site=self)
- if isinstance(namespaces, list):
- eugen.request["geunamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- eugen.request["geunamespace"] = str(namespaces)
+ if namespaces is not None:
+ eugen.set_namespace(namespaces)
if isinstance(limit, int):
eugen.limit = limit
return eugen
@@ -1700,11 +1682,8 @@
"""
iugen = api.PageGenerator("imageusage", site=self,
giutitle=image.title(withSection=False))
- if isinstance(namespaces, list):
- iugen.request["giunamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- iugen.request["giunamespace"] = str(namespaces)
+ if namespaces is not None:
+ iugen.set_namespace(namespaces)
if isinstance(limit, int):
iugen.limit = limit
if filterredir is not None:
@@ -1803,11 +1782,8 @@
rcgen.request["rcdir"] = "newer"
if isinstance(limit, int):
rcgen.limit = limit
- if isinstance(namespaces, list):
- rcgen.request["rcnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- rcgen.request["rcnamespace"] = str(namespaces)
+ if namespaces is not None:
+ rcgen.set_namespace(namespaces)
if pagelist:
if self.versionnumber() > 14:
pywikibot.output(
@@ -1859,11 +1835,8 @@
pywikibot.output(u"search: namespaces cannot be empty; using [0].",
level=pywikibot.WARNING)
namespaces = [0]
- if isinstance(namespaces, list):
- srgen.request["gsrnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- else:
- srgen.request["gsrnamespace"] = str(namespaces)
+ if namespaces is not None:
+ srgen.set_namespace(namespaces)
if getredirects:
srgen.request["gsrredirects"] = ""
if isinstance(limit, int):
@@ -1916,11 +1889,8 @@
ucgen.request["ucdir"] = "newer"
if isinstance(limit, int):
ucgen.limit = limit
- if isinstance(namespaces, list):
- ucgen.request["ucnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- ucgen.request["ucnamespace"] = str(namespaces)
+ if namespaces is not None:
+ ucgen.set_namespace(namespaces)
if showMinor is not None:
ucgen.request["ucshow"] = showMinor and "minor" or "!minor"
return ucgen
@@ -1967,11 +1937,8 @@
wlgen.request["wldir"] = "newer"
if isinstance(limit, int):
wlgen.limit = limit
- if isinstance(namespaces, list):
- wlgen.request["wlnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- wlgen.request["wlnamespace"] = str(namespaces)
+ if namespaces is not None:
+ wlgen.set_namespace(namespaces)
filters = {'minor': showMinor,
'bot': showBot,
'anon': showAnon}
@@ -2075,11 +2042,8 @@
"""
rngen = api.PageGenerator("random", site=self)
rngen.limit = limit
- if isinstance(namespaces, list):
- rngen.request["grnnamespace"] = u"|".join(unicode(ns)
- for ns in namespaces)
- elif namespaces is not None:
- rngen.request["grnnamespace"] = str(namespaces)
+ if namespaces is not None:
+ rngen.set_namespace(namespaces)
if redirects:
rngen.request["grnredirect"] = ""
return rngen
Patches item #2477551, was opened at 2008-12-30 19:56
Message generated for change (Comment added) made by misza13
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=603140&aid=2477551&group_…
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
>Status: Closed
Resolution: None
Priority: 5
Private: No
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: Pass "sysop" through wikipedia.put()
Initial Comment:
This allows the use of the sysop account to be forced; useful for eg closing XfD discussions where for transparency the admin account must be seen to make the change.
Changes:
wikipedia.py:
@@ line 1326:
-- force=False):
++ force=False, sysop=sysop):
@@ line 1342:
-- sysop = self._getActionUser(action = 'edit', restriction = self.editRestriction, sysop = False)
++ sysop = self._getActionUser(action = 'edit', restriction = self.editRestriction, sysop = sysop)
Default behavior is unchanged.
Feel free to contact me: happy <underscore> melon <at> hotmail <dot> co <dot> uk
----------------------------------------------------------------------
>Comment By: Misza (misza13)
Date: 2009-02-21 17:52
Message:
Done in r6399.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=603140&aid=2477551&group_…
Revision: 6398
Author: filnik
Date: 2009-02-21 13:55:07 +0000 (Sat, 21 Feb 2009)
Log Message:
-----------
Bugfix(?) for when the APIs are down..
Modified Paths:
--------------
trunk/pywikipedia/checkimages.py
Modified: trunk/pywikipedia/checkimages.py
===================================================================
--- trunk/pywikipedia/checkimages.py 2009-02-21 12:24:15 UTC (rev 6397)
+++ trunk/pywikipedia/checkimages.py 2009-02-21 13:55:07 UTC (rev 6398)
@@ -522,7 +522,7 @@
else:
raise wikipedia.Error(data)
if len(members) == int(cmlimit):
- raise wikipedia.Error(u'The category selected as >= %s elements, limit reached.' % cmlimit)
+ raise wikipedia.Error(u'The category selected has >= %s elements, limit reached.' % cmlimit)
allmembers = members
results = list()
for subcat in members:
@@ -1157,6 +1157,8 @@
self.whiteTemplatesFound = False
regex_find_licenses = re.compile(r'(?<!\{)\{\{(?:[Tt]emplate:|)([^{]+?)[|\n<}]', re.DOTALL)
templatesInTheImageRaw = regex_find_licenses.findall(self.imageCheckText)
+ if self.licenses_found == [] and templatesInTheImageRaw != []:
+ raise wikipedia.Error("APIs seems down. No templates found with them but actually there are templates used in the image's page!")
self.allLicenses = list()
if self.list_licenses == []:
raise wikipedia.Error(u'No licenses allowed provided, add that option to the code to make the script working correctly')