jenkins-bot has submitted this change and it was merged.

Change subject: Dont load site at startup to log site version
......................................................................


Dont load site at startup to log site version

Move the logging of the site version into Bot() to prevent
site access issues occurring before main() or Bot() have
requested a Site() object.

Bot now log the site version of each site that is fetched via
the Bot.site property

Bot.__init__ sets self.generator if it is in kwargs.

Bot.run updates Bot.site if Bot.site was not set prior to run()
being invoked.

Remove the run() method from delete.py, so it uses the new
treat()-only system where Bot.run() manages the site property.

Bug: 69895
Change-Id: If8a71b78b639288c7cf4329b1292c88d889ce1fa
---
M pywikibot/bot.py
M scripts/delete.py
2 files changed, 70 insertions(+), 23 deletions(-)

Approvals:
  XZise: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 99c6b8f..1b5bfec 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -258,10 +258,11 @@
 
     This may help the user to track errors or report bugs.
     """
-    # if site not available it's too early to print a header (work-a-round)
-    try:
-        site = pywikibot.Site()
-    except AttributeError:
+    # If a http thread is not available, it's too early to print a header
+    # that includes version information, which may need to query a server.
+    # The http module can't be imported due to circular dependencies.
+    http = sys.modules.get('pywikibot.comms.http', None)
+    if not http or not hasattr(http, 'threads') or not len(http.threads):
         return
 
     log(u'=== Pywikibot framework v2.0 -- Logging header ===')
@@ -321,8 +322,6 @@
 
     if config.log_pywiki_repo_version:
         log(u'PYWIKI REPO VERSION: %s' % 
unicode(version.getversion_onlinerepo()))
-
-    log(u'SITE VERSION: %s' % site.version())
 
     log(u'=== ' * 14)
 
@@ -888,7 +887,12 @@
         @param kwargs: bot options
         @type kwargs: dict
         """
+        if 'generator' in kwargs:
+            self.generator = kwargs.pop('generator')
+
         self.setOptions(**kwargs)
+        self._site = None
+        self._sites = set()
 
     def setOptions(self, **kwargs):
         """
@@ -1049,13 +1053,63 @@
         raise NotImplementedError('Method %s.treat() not implemented.'
                                   % self.__class__.__name__)
 
+    @property
+    def site(self):
+        """Site that the bot is using."""
+        if not self._site:
+            warning('Bot.site was not set before being retrieved.')
+            self.site = pywikibot.Site()
+            warning('Using the default site: %s' % self.site)
+        return self._site
+
+    @site.setter
+    def site(self, site):
+        """
+        Set the Site that the bot is using.
+
+        When Bot.run() is managing the generator and site property, this is
+        set each time a page is on a site different from the previous page.
+        """
+        if site not in self._sites:
+            log(u'LOADING SITE %s VERSION: %s'
+                % (site, unicode(site.version())))
+
+            self._sites.add(site)
+            if len(self._sites) == 2:
+                log('%s uses multiple sites' % self.__class__.__name__)
+        if self._site and self._site != site:
+            log('%s: changing site from %s to %s'
+                % (self.__class__.__name__, self._site, site))
+        self._site = site
+
     def run(self):
         """Process all pages in generator."""
         if not hasattr(self, 'generator'):
             raise NotImplementedError('Variable %s.generator not set.'
                                       % self.__class__.__name__)
+
+        # This check is to remove the possibility that the superclass changing
+        # self.site causes bugs in subclasses.
+        # If the subclass has set self.site before run(), it may be that the
+        # bot processes pages on sites other than self.site, and therefore
+        # this method cant alter self.site.  To use this functionality, don't
+        # set self.site in __init__, and use page.site in treat().
+        auto_update_site = not self._site
+        if not auto_update_site:
+            warning(
+                '%s.__init__ set the Bot.site property; this is only needed '
+                'when the Bot accesses many sites.' % self.__class__.__name__)
+        else:
+            log('Bot is managing the %s.site property in run()'
+                % self.__class__.__name__)
+
         try:
             for page in self.generator:
+                # When in auto update mode, set the site when it changes,
+                # so subclasses can hook onto changes to site.
+                if (auto_update_site and
+                        (not self._site or page.site != self.site)):
+                    self.site = page.site
                 self.treat(page)
         except QuitKeyboardInterrupt:
             pywikibot.output('\nUser quit %s bot run...' %
diff --git a/scripts/delete.py b/scripts/delete.py
index 5307598..fe4f1b6 100644
--- a/scripts/delete.py
+++ b/scripts/delete.py
@@ -59,27 +59,20 @@
         self.availableOptions.update({
             'undelete': False,
         })
-        super(DeletionRobot, self).__init__(**kwargs)
+        super(DeletionRobot, self).__init__(generator=generator, **kwargs)
 
-        self.generator = generator
         self.summary = summary
 
-    def run(self):
-        """
-        Run bot.
-
-        Loop through everything in the page generator and delete it.
-        """
-        for page in self.generator:
-            self.current_page = page
-
-            if self.getOption('undelete'):
-                page.undelete(self.summary)
+    def treat(self, page):
+        """Delete one page from the generator."""
+        self.current_page = page
+        if self.getOption('undelete'):
+            page.undelete(self.summary)
+        else:
+            if page.exists():
+                page.delete(self.summary, not self.getOption('always'))
             else:
-                if page.exists():
-                    page.delete(self.summary, not self.getOption('always'))
-                else:
-                    pywikibot.output(u'Skipping: %s does not exist.' % page)
+                pywikibot.output(u'Skipping: %s does not exist.' % page)
 
 
 def main(*args):

-- 
To view, visit https://gerrit.wikimedia.org/r/166583
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If8a71b78b639288c7cf4329b1292c88d889ce1fa
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Nullzero <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to