XZise has uploaded a new change for review.
https://gerrit.wikimedia.org/r/221119
Change subject: [FEAT] Family: Allow family name as class name
......................................................................
[FEAT] Family: Allow family name as class name
This first tries to load the module attribute using the family's name (e.g.
WikipediaFamily when the family's name is wikipedia). As a fallback it uses the
attribute with the name "Family".
generate_family_file now uses the new name (and also super). Importing the
Family class as Family into a file doesn't lead to collisions anymore.
Change-Id: I5a79a56380490655eb179682eaa4132586c14029
---
M generate_family_file.py
M pywikibot/families/wikipedia_family.py
M pywikibot/family.py
3 files changed, 20 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/19/221119/1
diff --git a/generate_family_file.py b/generate_family_file.py
index 4993fd6..e938e96 100755
--- a/generate_family_file.py
+++ b/generate_family_file.py
@@ -187,16 +187,18 @@
Please do not commit this to the Git repository!
\"\"\"
-from pywikibot import family
+from pywikibot.family import Family
from pywikibot.tools import deprecated
-class Family(family.Family):
+class %(cls_name)sFamily(Family):
+
def __init__(self):
- family.Family.__init__(self)
+ super(%(cls_name)s, self).__init__()
self.name = '%(name)s'
self.langs = {
-""".lstrip() % {'url': self.base_url, 'name': self.name})
+""".lstrip() % {'url': self.base_url, 'name': self.name,
+ 'cls_name': self.name[0].upper() + self.name[1:]})
for w in self.wikis.values():
f.write(" '%(lang)s': '%(hostname)s',\n"
diff --git a/pywikibot/families/wikipedia_family.py
b/pywikibot/families/wikipedia_family.py
index e726b71..5786711 100644
--- a/pywikibot/families/wikipedia_family.py
+++ b/pywikibot/families/wikipedia_family.py
@@ -8,7 +8,7 @@
# The Wikimedia family that is known as Wikipedia, the Free Encyclopedia
-class Family(family.WikimediaFamily):
+class WikipediaFamily(family.WikimediaFamily):
"""Family module for Wikipedia."""
@@ -48,7 +48,7 @@
def __init__(self):
"""Constructor."""
- super(Family, self).__init__()
+ super(WikipediaFamily, self).__init__()
self.name = 'wikipedia'
self.languages_by_size = [
diff --git a/pywikibot/family.py b/pywikibot/family.py
index a4634fc..6fa3ec8 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -30,7 +30,7 @@
from pywikibot import config2 as config
from pywikibot.tools import (
deprecated, deprecated_args, issue_deprecation_warning,
- FrozenDict,
+ FrozenDict, first_upper,
)
from pywikibot.exceptions import UnknownFamily, FamilyMaintenanceWarning
@@ -919,7 +919,17 @@
mod = imp.load_source(fam, config.family_files[fam])
except (ImportError, KeyError):
raise UnknownFamily(u'Family %s does not exist' % fam)
- cls = mod.Family()
+
+ try:
+ cls = getattr(mod, first_upper('{0}Family'.format(fam)))
+ except AttributeError:
+ cls = mod.Family
+ # Don't highlight as prominent
+ warn('The family class in "{0}" should not use "Family" as a '
+ 'name.'.format(config.family_files[fam]),
+ DeprecationWarning)
+
+ cls = cls()
if cls.name != fam:
warn(u'Family name %s does not match family module name %s'
% (cls.name, fam), FamilyMaintenanceWarning)
--
To view, visit https://gerrit.wikimedia.org/r/221119
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5a79a56380490655eb179682eaa4132586c14029
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits