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

Change subject: Make it possible to define a custom families folder
......................................................................


Make it possible to define a custom families folder

Allow to define a custom family folder with a global function from the 
user-config.py file.
Add try-except structures that prevent issues when you try to use the API from 
login.py on
a wiki that requires you to first log in to use any part of the API.

Change-Id: I04690a85b46dbe2ce9a9697ba376feb7624a694c
Original-Change-Id: I631ecc551900cefb85d45b860dc45e187c0a7395
---
M generate_user_files.py
M pywikibot/config2.py
M pywikibot/site.py
3 files changed, 74 insertions(+), 39 deletions(-)

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



diff --git a/generate_user_files.py b/generate_user_files.py
index 0df0b15..eed4339 100644
--- a/generate_user_files.py
+++ b/generate_user_files.py
@@ -237,9 +237,34 @@
 # This is an automatically generated file. You can find more configuration
 # parameters in 'config.py' file.
 
-# The family of sites we are working on. wikipedia.py will import
-# families/xxx_family.py so if you want to change this variable,
-# you need to write such a file.
+# The family of sites to work on by default.
+#
+# ‘site.py’ imports ‘families/xxx_family.py’, so if you want to change
+# this variable, you need to use the name of one of the existing family files
+# in that folder or write your own, custom family file.
+#
+# For ‘site.py’ to be able to read your custom family file, you must
+# save it to ‘families/xxx_family.py’, where ‘xxx‘ is the codename of the
+# family that your custom ‘xxx_family.py’ family file defines.
+#
+# You can also save your custom family files to a different folder. As long
+# as you follow the ‘xxx_family.py’ naming convention, you can register your
+# custom folder in this configuration file with the following global function:
+#
+#   register_families_folder(folder_path)
+#
+# Alternatively, you can register particular family files that do not need
+# to follow the ‘xxx_family.py’ naming convention using the following
+# global function:
+#
+#   register_family_file(family_name, file_path)
+#
+# Where ‘family_name’ is the family code (the ‘xxx’ in standard family file
+# names) and ‘file_path’ is the absolute path to the target family file.
+#
+# If you use either of these functions to define the family to work on by
+# default (the ‘family’ variable below), you must place the function call
+# before the definition of the ‘family’ variable.
 family = '%s'
 
 # The language code of the site we're working on.
diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index 3f6342c..d83009d 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -106,8 +106,6 @@
 default_edit_summary = u'Wikipedia python library v.2'
 
 
-# Get the names of all known families, and initialize
-# with empty dictionaries
 def _get_base_dir():
     """Return the directory in which user-specific information is stored.
 
@@ -162,14 +160,27 @@
     return base_dir
 
 _base_dir = _get_base_dir()
-# families/ is a subdirectory of the directory in which config.py is found
-for _filename in os.listdir(
-        os.path.join(os.path.dirname(__file__), 'families')):
-    if _filename.endswith("_family.py"):
-        familyName = _filename[:-len("_family.py")]
-        usernames[familyName] = {}
-        sysopnames[familyName] = {}
-        disambiguation_comment[familyName] = {}
+
+
+family_files = {}
+
+
+def register_family_file(family_name, file_path):
+    usernames[family_name] = {}
+    sysopnames[family_name] = {}
+    disambiguation_comment[family_name] = {}
+    family_files[family_name] = file_path
+
+
+def register_families_folder(folder_path):
+    for file_name in os.listdir(folder_path):
+        if file_name.endswith("_family.py"):
+            family_name = file_name[:-len("_family.py")]
+            register_family_file(family_name, os.path.join(folder_path, 
file_name))
+
+# Get the names of all known families, and initialize with empty dictionaries.
+# ‘families/’ is a subdirectory of the directory in which config2.py is found.
+register_families_folder(os.path.join(os.path.dirname(__file__), 'families'))
 
 # Set to True to override the {{bots}} exclusion protocol (at your own risk!)
 ignore_bot_templates = False
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 141f0e9..da3d09f 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -16,6 +16,7 @@
 except ImportError:
     from md5 import md5
 import datetime
+import imp
 import itertools
 import os
 import re
@@ -84,24 +85,16 @@
     if fam is None:
         fam = config.family
     try:
-        # first try the built-in families
-        name = "pywikibot.families.%s_family" % fam
-        __import__(name)
-        myfamily = sys.modules[name]
-    except ImportError:
-        # next see if user has defined a local family module
-        try:
-            sys.path.append(config.datafilepath('families'))
-            myfamily = __import__("%s_family" % fam)
-        except ImportError:
-            if fatal:
-                pywikibot.error(u"""\
+        myfamily = imp.load_source(fam, config.family_files[fam])
+    except (ImportError, KeyError):
+        if fatal:
+            pywikibot.error(u"""\
 Error importing the %s family. This probably means the family
 does not exist. Also check your configuration file."""
-                                % fam, exc_info=True)
-                sys.exit(1)
-            else:
-                raise Error("Family %s does not exist" % fam)
+                            % fam, exc_info=True)
+            sys.exit(1)
+        else:
+            raise Error("Family %s does not exist" % fam)
     return myfamily.Family()
 
 
@@ -881,10 +874,13 @@
         self._loginstatus = LoginStatus.IN_PROGRESS
         if hasattr(self, "_userinfo"):
             del self._userinfo
-        self.getuserinfo()
-        if self.userinfo['name'] == self._username[sysop] and \
-           self.logged_in(sysop):
-            return
+        try:
+            self.getuserinfo()
+            if self.userinfo['name'] == self._username[sysop] and \
+               self.logged_in(sysop):
+                return
+        except pywikibot.data.api.APIError:  # May occur if you are not logged 
in (no API read permissions).
+            pass
         loginMan = api.LoginManager(site=self, sysop=sysop,
                                     user=self._username[sysop])
         if loginMan.login(retry=True):
@@ -1331,12 +1327,15 @@
         version numbers and any other text contained in the version.
 
         """
-        if force:
-            self._getsiteinfo(force=True)    # drop/expire cache and reload
-        versionstring = self.siteinfo['generator']
-        m = re.match(r"^MediaWiki ([0-9]+)\.([0-9]+)(.*)$", versionstring)
-        if m:
-            return (int(m.group(1)), int(m.group(2)), m.group(3))
+        try:
+            if force:
+                self._getsiteinfo(force=True)    # drop/expire cache and reload
+            versionstring = self.siteinfo['generator']
+            m = re.match(r"^MediaWiki ([0-9]+)\.([0-9]+)(.*)$", versionstring)
+            if m:
+                return (int(m.group(1)), int(m.group(2)), m.group(3))
+        except pywikibot.data.api.APIError:  # May occur if you are not logged 
in (no API read permissions).
+            return (0, 0, 0)
 
     def loadpageinfo(self, page):
         """Load page info from api and save in page attributes"""

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

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

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

Reply via email to