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

Change subject: pwb.py: mock pywikibot if it is not available
......................................................................


pwb.py: mock pywikibot if it is not available

run_python_file() can only be called after pywikibot has been loaded.
However, it is impossible to load pywikibot if a user-config.py is
not yet available. We therefore now mock pywikibot, by creating
and object (lambda function) with argvu attribute.

Bug: 56324
Change-Id: If0a1e08e29da3a3780b0b9300fcad140ae7087c5
---
M pwb.py
1 file changed, 19 insertions(+), 2 deletions(-)

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



diff --git a/pwb.py b/pwb.py
index 08e586e..1c49181 100644
--- a/pwb.py
+++ b/pwb.py
@@ -23,6 +23,19 @@
 import sys
 
 
+def tryimport_pwb():
+    # See if we can import pywikibot. If so, we need to patch pwb.argvu, too.
+    # If pywikibot is not available, we create a mock object to remove the
+    # need for if statements further on.
+    global pwb
+    try:
+        import pywikibot
+        pwb = pywikibot
+    except RuntimeError:
+        pwb = lambda: None
+        pwb.argvu = []
+
+
 def run_python_file(filename, argv, argvu):
     """Run a python file as if it were the main program on the command line.
 
@@ -30,6 +43,8 @@
     `args` is the argument array to present as sys.argv, as unicode strings.
 
     """
+    tryimport_pwb()
+
     # Create a module to serve as __main__
     old_main_mod = sys.modules['__main__']
     main_mod = imp.new_module('__main__')
@@ -88,13 +103,15 @@
 if not os.path.exists(user_config_path):
     print "NOTE:", user_config_path, "was not found!"
     print "Please follow the prompts to create it:"
-    run_python_file('generate_user_files.py', ['generate_user_files.py'])
+    path = 'generate_user_files.py'
+    run_python_file(path, [path], [path.decode('ascii')])
 
-import pywikibot as pwb
 if len(sys.argv) > 1:
+    tryimport_pwb()
     fn = sys.argv[1]
     argv = sys.argv[1:]
     argvu = pwb.argvu[1:]
+    print argvu
 
     if not os.path.exists(fn):
         testpath = os.path.join(os.path.split(__file__)[0], 'scripts', fn)

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If0a1e08e29da3a3780b0b9300fcad140ae7087c5
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to