[Pywikipedia-l] SVN: [6311] branches/rewrite/pywikibot

russblau at svn.wikimedia.org russblau at svn.wikimedia.org
Thu Jan 29 20:08:52 UTC 2009


Revision: 6311
Author:   russblau
Date:     2009-01-29 20:08:52 +0000 (Thu, 29 Jan 2009)

Log Message:
-----------
Output formatting tweaks.

Modified Paths:
--------------
    branches/rewrite/pywikibot/comms/http.py
    branches/rewrite/pywikibot/comms/threadedhttp.py
    branches/rewrite/pywikibot/config2.py
    branches/rewrite/pywikibot/data/api.py
    branches/rewrite/pywikibot/login.py
    branches/rewrite/pywikibot/page.py
    branches/rewrite/pywikibot/pagegenerators.py
    branches/rewrite/pywikibot/site.py

Modified: branches/rewrite/pywikibot/comms/http.py
===================================================================
--- branches/rewrite/pywikibot/comms/http.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/comms/http.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -27,7 +27,8 @@
 import logging
 import atexit
 
-from pywikibot import config, VERBOSE
+from pywikibot import config
+import pywikibot
 import cookielib
 import threadedhttp
 
@@ -54,7 +55,8 @@
 
 
 # Build up HttpProcessors
-logger.log(VERBOSE, 'Starting %(numthreads)i threads...', locals())
+pywikibot.output('Starting %(numthreads)i threads...' % locals(),
+                 level=pywikibot.VERBOSE)
 for i in range(numthreads):
     proc = threadedhttp.HttpProcessor(http_queue, cookie_jar, connection_pool)
     proc.setDaemon(True)
@@ -65,7 +67,8 @@
 def _flush():
     for i in threads:
         http_queue.put(None)
-    logger.log(VERBOSE, 'Waiting for threads to finish... ')
+    pywikibot.output(u'Waiting for threads to finish... ',
+                     level=pywikibot.VERBOSE)
     for i in threads:
         i.join()
     logger.debug('All threads finished.')

Modified: branches/rewrite/pywikibot/comms/threadedhttp.py
===================================================================
--- branches/rewrite/pywikibot/comms/threadedhttp.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/comms/threadedhttp.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -30,6 +30,8 @@
 import cookielib
 import sys
 
+import pywikibot
+
 logger = logging.getLogger("comms.threadedhttp")
 
 
@@ -37,8 +39,9 @@
 try:
     import pkg_resources
 except ImportError:
-    logger.critical(
-        "Error: You need the python module setuptools to use this module")
+    pywikibot.output(
+        u"Error: You need the python module setuptools to use this module",
+        level=pywikibot.CRITICAL)
     sys.exit(1)
 pkg_resources.require("httplib2")
 import httplib2

Modified: branches/rewrite/pywikibot/config2.py
===================================================================
--- branches/rewrite/pywikibot/config2.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/config2.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -504,7 +504,7 @@
         del nt,ot
     else:
         logger.warn(
-    "Configuration variable %(_key)r is defined but unknown. Misspelled?"
+    "Configuration variable %(_key)r is defined but unknown. Misspelled?\n"
             % locals())
 
 # Fix up default console_encoding

Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/data/api.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -172,7 +172,7 @@
                     self.params[key] = self.params[key].encode(
                                                 self.site.encoding())
             except Exception:
-                logger.exception("key=%s, params=%s" % (key, self.params[key]))
+                logger.exception("key=%s, params=%s\n" % (key, self.params[key]))
         params = urllib.urlencode(self.params)
         while True:
             # TODO catch http errors
@@ -194,11 +194,14 @@
                     uri = uri + "?" + params
                     rawdata = http.request(self.site, uri)
             except Exception, e: #TODO: what exceptions can occur here?
-                logger.warning(traceback.format_exc())
-                logger.warning("%s, %s", uri, params)
+                pywikibot.output(traceback.format_exc(),
+                                 level=pywikibot.ERROR)
+                pywikibot.output("%s, %s" % (uri, params),
+                                 level=pywikibot.VERBOSE)
                 self.wait()
                 continue
-            logger.debug("API response received:\n%s", rawdata)
+            pywikibot.output("API response received:\n%s" % rawdata,
+                             level=pywikibot.DEBUG)
             if not isinstance(rawdata, unicode):
                 rawdata = rawdata.decode(self.site.encoding())
             if rawdata.startswith(u"unknown_action"):
@@ -208,10 +211,10 @@
             except ValueError:
                 # if the result isn't valid JSON, there must be a server
                 # problem.  Wait a few seconds and try again
-                logger.warning(
+                pywikibot.output(
 "Non-JSON response received from server %s; the server may be down."
-                              % self.site)
-                logger.debug(rawdata)
+                                 % self.site, level=pywikibot.WARNING)
+                pywikibot.output(rawdata, level=pywikibot.DEBUG)
                 self.wait()
                 continue
             if not result:
@@ -231,9 +234,10 @@
             if "warnings" in result:
                 modules = [k for k in result["warnings"] if k != "info"]
                 for mod in modules:
-                    logger.warning(
-                        "API warning (%s): %s"
-                        % (mod, result["warnings"][mod]["*"]))
+                    pywikibot.output(
+                        u"API warning (%s): %s"
+                         % (mod, result["warnings"][mod]["*"]),
+                        level=pywikibot.WARNING)
             if "error" not in result:
                 return result
             if "*" in result["error"]:
@@ -262,7 +266,9 @@
         self.max_retries -= 1
         if self.max_retries < 0:
             raise TimeoutError("Maximum retries attempted without success.")
-        logger.warn("Waiting %s seconds before retrying." % self.retry_wait)
+        pywikibot.output(u"Waiting %s seconds before retrying."
+                          % self.retry_wait,
+                         level=pywikibot.WARNING)
         time.sleep(self.retry_wait)
         # double the next wait, but do not exceed 120 seconds
         self.retry_wait = min(120, self.retry_wait * 2)
@@ -354,8 +360,10 @@
                     else:
                         self.query_limit = int(param["max"])
                     self.prefix = _modules[mod]["prefix"]
-                    logger.debug("%s: Set query_limit to %i."
-                                  % (self.__class__.__name__, self.query_limit))
+                    pywikibot.output(u"%s: Set query_limit to %i."
+                                     % (self.__class__.__name__,
+                                        self.query_limit),
+                                     level=pywikibot.DEBUG)
                     return
 
     def __iter__(self):
@@ -377,27 +385,31 @@
                     self.request[self.prefix+"limit"] = str(new_limit)
             self.data = self.request.submit()
             if not self.data or not isinstance(self.data, dict):
-                logger.debug(
-                    "%s: stopped iteration because no dict retrieved from api."
-                    % self.__class__.__name__)
+                pywikibot.output(
+                    u"%s: stopped iteration because no dict retrieved from api."
+                     % self.__class__.__name__,
+                    level=pywikibot.DEBUG)
                 return
             if not ("query" in self.data
                     and self.resultkey in self.data["query"]):
-                logger.debug(
-"%s: stopped iteration because 'query' and '%s' not found in api response.",
-                        self.__class__.__name__, self.resultkey)
-                logger.debug(self.data)
+                pywikibot.output(
+u"%s: stopped iteration because 'query' and '%s' not found in api response."
+                    % (self.__class__.__name__, self.resultkey),
+                    level=pywikibot.DEBUG)
+                pywikibot.output(unicode(self.data), level=pywikibot.DEBUG)
                 return
             pagedata = self.data["query"][self.resultkey]
             if isinstance(pagedata, dict):
-                logger.debug("%s received %s; limit=%s"
-                         % (self.__class__.__name__, pagedata.keys(),
-                            self.limit))
+                pywikibot.output(u"%s received %s; limit=%s"
+                                  % (self.__class__.__name__, pagedata.keys(),
+                                     self.limit),
+                                 level=pywikibot.DEBUG)
                 pagedata = pagedata.values()
             else:
-                logger.debug("%s received %s; limit=%s"
-                         % (self.__class__.__name__, pagedata,
-                            self.limit))                
+                pywikibot.output(u"%s received %s; limit=%s"
+                                  % (self.__class__.__name__, pagedata,
+                                     self.limit),
+                                 level=pywikibot.DEBUG)
             for item in pagedata:
                 yield self.result(item)
                 count += 1

Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/login.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -194,7 +194,8 @@
         try:
             cookiedata = self.getCookie()
         except pywikibot.data.api.APIError, e:
-            logger.error("Login failed (%s).", e.code)
+            pywikibot.output("Login failed (%s)." % e.code,
+                             level=pywikibot.ERROR)
             if retry:
                 self.password = None
                 return self.login(retry = True)

Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/page.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -659,7 +659,7 @@
             else:
                 pywikibot.output("Page %s saved" % self.title(asLink=True))
         except pywikibot.Error, err:
-            logger.exception("Error saving page %s" % self.title(asLink=True))
+            logger.exception("Error saving page %s\n" % self.title(asLink=True))
         if callback:
             callback(self, err)
 
@@ -1139,31 +1139,33 @@
 
     def encoding(self):
         """DEPRECATED: use Site.encoding() instead"""
-        logger.debug(u"Page.encoding() is deprecated; use Site.encoding().")
+        pywikibot.output(u"Page.encoding() is deprecated; use Site.encoding().",
+                         level=pywikibot.DEBUG)
         return self.site().encoding()
 
     def titleWithoutNamespace(self, underscore=False):
         """DEPRECATED: use self.title(withNamespace=False) instead."""
-        logger.debug(
-            u"Page.titleWithoutNamespace() method is deprecated.")
+        pywikibot.output(u"Page.titleWithoutNamespace() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.title(underscore=underscore, withNamespace=False,
                           withSection=False)
 
     def titleForFilename(self):
         """DEPRECATED: use self.title(as_filename=True) instead."""
-        logger.debug(
-            u"Page.titleForFilename() method is deprecated.")
+        pywikibot.output(u"Page.titleForFilename() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.title(as_filename=True)
 
     def sectionFreeTitle(self, underscore=False):
         """DEPRECATED: use self.title(withSection=False) instead."""
-        logger.debug(
-            u"Page.sectionFreeTitle() method is deprecated.")
+        pywikibot.output(u"Page.sectionFreeTitle() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.title(underscore=underscore, withSection=False)
 
     def aslink(self, forceInterwiki=False, textlink=False, noInterwiki=False):
         """DEPRECATED: use self.title(asLink=True) instead."""
-        logger.debug(u"Page.aslink() method is deprecated.")
+        pywikibot.output(u"Page.aslink() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.title(asLink=True, forceInterwiki=forceInterwiki,
                           allowInterwiki=not noInterwiki, textlink=textlink)
 
@@ -1173,7 +1175,8 @@
         DEPRECATED: use self.title(asUrl=True) instead.
 
         """
-        logger.debug(u"Page.urlname() method is deprecated.")
+        pywikibot.output(u"Page.urlname() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.title(asUrl=True)
 
 ####### DISABLED METHODS (warnings provided) ######
@@ -1182,12 +1185,14 @@
 
     def removeImage(self, image, put=False, summary=None, safe=True):
         """Old method to remove all instances of an image from page."""
-        logger.warning(u"Page.removeImage() is no longer supported.")
+        pywikibot.output(u"Page.removeImage() is no longer supported.",
+                         level=pywikibot.WARNING)
 
     def replaceImage(self, image, replacement=None, put=False, summary=None,
                      safe=True):
         """Old method to replace all instances of an image with another."""
-        logger.warning(u"Page.replaceImage() is no longer supported.")
+        pywikibot.output(u"Page.replaceImage() is no longer supported.",
+                         level=pywikibot.WARNING)
 
 
 class ImagePage(Page):
@@ -1250,8 +1255,9 @@
 
     def getFileMd5Sum(self):
         """Return image file's MD5 checksum."""
-        logger.debug(
-            "ImagePage.getFileMd5Sum() is deprecated; use getFileSHA1Sum().")
+        pywikibot.output(
+            u"ImagePage.getFileMd5Sum() is deprecated; use getFileSHA1Sum().",
+            level=pywikibot.DEBUG)
 # FIXME: MD5 might be performed on incomplete file due to server disconnection
 # (see bug #1795683).
         import md5, urllib
@@ -1399,8 +1405,9 @@
         catname = self.site().category_namespace() + ':' + catname
         targetCat = Category(self.site(), catname)
         if targetCat.exists():
-            logger.warn('Target page %s already exists!'
-                             % targetCat.title())
+            pywikibot.output(u'Target page %s already exists!'
+                              % targetCat.title(),
+                             level=pywikibot.WARNING)
             return False
         else:
             pywikibot.output('Moving text from %s to %s.'
@@ -1435,8 +1442,9 @@
         catname = self.site().category_namespace() + ':' + catname
         targetCat = Category(self.site(), catname)
         if targetCat.exists():
-            logger.warn('Target page %s already exists!'
-                             % targetCat.title())
+            pywikibot.output(u'Target page %s already exists!'
+                              % targetCat.title(),
+                             level=pywikibot.WARNING)
             return False
         else:
             pywikibot.output('Moving text from %s to %s.'
@@ -1463,22 +1471,26 @@
 #### DEPRECATED METHODS ####
     def subcategoriesList(self, recurse=False):
         """DEPRECATED: Equivalent to list(self.subcategories(...))"""
-        logger.debug("Category.subcategoriesList() method is deprecated.")
+        pywikibot.output(u"Category.subcategoriesList() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return sorted(list(set(self.subcategories(recurse))))
 
     def articlesList(self, recurse=False):
         """DEPRECATED: equivalent to list(self.articles(...))"""
-        logger.debug("Category.articlesList() method is deprecated.")
+        pywikibot.output(u"Category.articlesList() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return sorted(list(set(self.articles(recurse))))
 
     def supercategories(self):
         """DEPRECATED: equivalent to self.categories()"""
-        logger.debug("Category.supercategories() method is deprecated.")
+        pywikibot.output(u"Category.supercategories() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return self.categories()
 
     def supercategoriesList(self):
         """DEPRECATED: equivalent to list(self.categories(...))"""
-        logger.debug("Category.articlesList() method is deprecated.")
+        pywikibot.output(u"Category.articlesList() method is deprecated.",
+                         level=pywikibot.DEBUG)
         return sorted(list(set(self.categories())))
 
 

Modified: branches/rewrite/pywikibot/pagegenerators.py
===================================================================
--- branches/rewrite/pywikibot/pagegenerators.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/pagegenerators.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -167,7 +167,8 @@
             gensList = CombinedPageGenerator(self.gens)
         genToReturn = DuplicateFilterPageGenerator(gensList)
         if self.namespaces:
-            genToReturn = NamespaceFilterPageGenerator(genToReturn, map(int, self.namespaces))
+            genToReturn = NamespaceFilterPageGenerator(
+                              genToReturn, map(int, self.namespaces))
         return genToReturn
 
     def getCategoryGen(self, arg, length, recurse = False):

Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py	2009-01-29 11:59:34 UTC (rev 6310)
+++ branches/rewrite/pywikibot/site.py	2009-01-29 20:08:52 UTC (rev 6311)
@@ -1851,7 +1851,8 @@
         srgen = api.PageGenerator("search", gsrsearch=searchstring,
                                   gsrwhat=where, site=self)
         if not namespaces:
-            logger.warning("search: namespaces cannot be empty; using [0].")
+            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)
@@ -2219,21 +2220,24 @@
                         continue
                     else:
                         self.unlock_page(page)
-                        logger.error(
-"editpage: unknown CAPTCHA response %s, page not saved"
-                                      % captcha)
+                        pywikibot.output(
+                    u"editpage: unknown CAPTCHA response %s, page not saved"
+                                         % captcha,
+                                         level=pywikibot.ERROR)
                         return False
                 else:
                     self.unlock_page(page)
-                    logger.error("editpage: unknown failure reason %s"
-                                  % str(result))
+                    pywikibot.output(u"editpage: unknown failure reason %s"
+                                      % str(result),
+                                     level=pywikibot.ERROR)
                     return False
             else:
                 self.unlock_page(page)
-                logger.error(
-"editpage: Unknown result code '%s' received; page not saved"
-                    % result["edit"]["result"])
-                logger.error(str(result))
+                pywikibot.output(
+u"editpage: Unknown result code '%s' received; page not saved"
+                                   % result["edit"]["result"],
+                                 level=pywikibot.ERROR)
+                pywikibot.output(str(result), level=pywikibot.VERBOSE)
                 return False
 
     # catalog of move errors for use in error messages
@@ -2320,12 +2324,13 @@
         finally:
             self.unlock_page(page)
         if "move" not in result:
-            logger.error("movepage: %s" % result)
+            pywikibot.output(u"movepage: %s" % result, level=pywikibot.ERROR)
             raise Error("movepage: unexpected response")
         # TODO: Check for talkmove-error messages
         if "talkmove-error-code" in result["move"]:
-            logger.warning(u"movepage: Talk page %s not moved"
-                            % (page.toggleTalkPage().title(asLink=True)))
+            pywikibot.output(u"movepage: Talk page %s not moved"
+                              % (page.toggleTalkPage().title(asLink=True)),
+                             level=pywikibot.WARNING)
         return pywikibot.Page(page, newtitle)
 
     # catalog of rollback errors for use in error messages





More information about the Pywikipedia-l mailing list