Dachary has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311446

Change subject: bot: move --user/--test to the Bot class
......................................................................

bot: move --user/--test to the Bot class

Because it is the consumer of these options.

Change-Id: I5f276ea8fb0f14dd41efd481c04668d2f1e29f3f
Signed-off-by: Loic Dachary <l...@dachary.org>
---
M FLOSSbot/bot.py
M FLOSSbot/main.py
M FLOSSbot/qa.py
M FLOSSbot/repository.py
M tests/test_main.py
M tests/test_repository.py
6 files changed, 47 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/bots/FLOSSbot 
refs/changes/46/311446/1

diff --git a/FLOSSbot/bot.py b/FLOSSbot/bot.py
index a58920f..f04d076 100644
--- a/FLOSSbot/bot.py
+++ b/FLOSSbot/bot.py
@@ -14,6 +14,7 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+import argparse
 import logging
 
 import pywikibot
@@ -36,6 +37,28 @@
                                                 fam="wikidata")
         self.reset_cache()
 
+    @staticmethod
+    def get_parser():
+        parser = argparse.ArgumentParser(add_help=False)
+        parser.add_argument(
+            '--test',
+            action='store_true', default=None,
+            help='use test.wikidata.org instead of wikidata.org')
+        parser.add_argument(
+            '--user',
+            default=None,
+            help='wikidata user name')
+        return parser
+
+    @staticmethod
+    def factory(cls, argv):
+        parser = argparse.ArgumentParser(
+            parents=[Bot.get_parser()],
+            add_help=False,
+            conflict_handler='resolve')
+        cls.set_subparser(parser.add_subparsers())
+        return cls(parser.parse_args(argv))
+
     def reset_cache(self):
         self.entities = {
             'property': {},
diff --git a/FLOSSbot/main.py b/FLOSSbot/main.py
index 8f3a970..a0a9862 100644
--- a/FLOSSbot/main.py
+++ b/FLOSSbot/main.py
@@ -18,7 +18,7 @@
 import logging
 import textwrap
 
-from FLOSSbot import qa, repository, util
+from FLOSSbot import bot, qa, repository, util
 
 logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s')
 
@@ -34,7 +34,8 @@
             The documentation for each subcommand can be displayed with
 
                FLOSSbot subcommand --help
-            """))
+            """),
+            parents=[bot.Bot.get_parser()])
 
         self.parser.add_argument(
             '-v', '--verbose',
@@ -46,16 +47,6 @@
             '--dry-run',
             action='store_true', default=None,
             help='no side effect')
-
-        self.parser.add_argument(
-            '--test',
-            action='store_true', default=None,
-            help='use test.wikidata.org instead of wikidata.org')
-
-        self.parser.add_argument(
-            '--user',
-            default=None,
-            help='wikidata user name')
 
         subparsers = self.parser.add_subparsers(
             title='subcommands',
diff --git a/FLOSSbot/qa.py b/FLOSSbot/qa.py
index deb58a4..20addd1 100644
--- a/FLOSSbot/qa.py
+++ b/FLOSSbot/qa.py
@@ -24,8 +24,7 @@
 import requests
 from pywikibot import pagegenerators as pg
 
-from FLOSSbot import util
-from FLOSSbot import bot
+from FLOSSbot import bot, util
 
 log = logging.getLogger(__name__)
 
@@ -34,7 +33,7 @@
 
     @staticmethod
     def get_parser():
-        parser = argparse.ArgumentParser()
+        parser = argparse.ArgumentParser(add_help=False)
         return parser
 
     @staticmethod
@@ -48,13 +47,14 @@
             help='Set the software quality assurance statement',
             parents=[QA.get_parser()],
             add_help=False,
+            conflict_handler='resolve',
         ).set_defaults(
             func=QA,
         )
 
     @staticmethod
     def factory(argv):
-        return QA(QA.get_parser().parse_args(argv))
+        return bot.Bot.factory(QA, argv)
 
     def run(self):
         QUERY = """
diff --git a/FLOSSbot/repository.py b/FLOSSbot/repository.py
index 49ad37d..8095718 100644
--- a/FLOSSbot/repository.py
+++ b/FLOSSbot/repository.py
@@ -38,7 +38,7 @@
 
     @staticmethod
     def get_parser():
-        parser = argparse.ArgumentParser()
+        parser = argparse.ArgumentParser(add_help=False)
         select = parser.add_mutually_exclusive_group()
         select.add_argument(
             '--filter',
@@ -146,13 +146,14 @@
             help='Set protocol of the source code repository',
             parents=[Repository.get_parser()],
             add_help=False,
+            conflict_handler='resolve',
         ).set_defaults(
             func=Repository,
         )
 
     @staticmethod
     def factory(argv):
-        return Repository(Repository.get_parser().parse_args(argv))
+        return bot.Bot.factory(Repository, argv)
 
     def debug(self, item, message):
         self.log(log.debug, item, message)
diff --git a/tests/test_main.py b/tests/test_main.py
index d12f27b..df56061 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -26,7 +26,7 @@
 class TestFLOSSbot(object):
 
     @mock.patch.object(FLOSSbot.qa.QA, 'run')
-    def test_run(self, m_run):
+    def test_run_main(self, m_run):
         f = main.FLOSSbot()
 
         argv = ['qa']
@@ -38,6 +38,15 @@
         assert (logging.getLogger('FLOSSbot').getEffectiveLevel() ==
                 logging.INFO)
 
+    @mock.patch.object(FLOSSbot.repository.Repository, 'run')
+    def test_run_repository(self, m_run):
+        f = main.FLOSSbot()
+
+        argv = ['repository']
+        f.run(['--verbose'] + argv)
+        assert (logging.getLogger('FLOSSbot').getEffectiveLevel() ==
+                logging.DEBUG)
+
 # Local Variables:
 # compile-command: "cd .. ; tox -e py3 tests/test_main.py"
 # End:
diff --git a/tests/test_repository.py b/tests/test_repository.py
index 95a5d79..81123af 100644
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -15,8 +15,6 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
-import argparse
-
 from FLOSSbot.repository import Repository
 from tests.wikidata import TestWikidata
 
@@ -27,10 +25,10 @@
         TestWikidata().login()
 
     def setup(self):
-        self.r = Repository(argparse.Namespace(
-            test=True,
-            user='FLOSSbotCI',
-        ))
+        self.r = Repository.factory([
+            '--user=FLOSSbotCI',
+            '--test',
+        ])
 
     def test_guessproto__github_is_git(self):
         assert(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5f276ea8fb0f14dd41efd481c04668d2f1e29f3f
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/bots/FLOSSbot
Gerrit-Branch: master
Gerrit-Owner: Dachary <l...@dachary.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to