John Vandenberg has uploaded a new change for review. https://gerrit.wikimedia.org/r/147665
Change subject: Add support for using OrderedDict ...................................................................... Add support for using OrderedDict OrderedDict is a python 2.7+ feature. For Python 2.6, require the ordereddict module in setup.py pwb.py checks for the ordereddict module and warns if it isnt present. wikibase and textlib use a fake OrderedDict which can emit a warning when instansiated and fallback gracefully to dict. Change-Id: I265ee4726d124b8e5dfa8ec2e2f9314b2c2132e7 --- M pwb.py M pywikibot/__init__.py M pywikibot/page.py M pywikibot/textlib.py M setup.py 5 files changed, 61 insertions(+), 6 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/65/147665/1 diff --git a/pwb.py b/pwb.py index 2fba1d3..0afbe36 100644 --- a/pwb.py +++ b/pwb.py @@ -111,6 +111,13 @@ print("Did you clone without --recursive? Try running 'git submodule update --init'.") sys.exit(1) +if sys.version_info[0] == 2 and sys.version_info[1] == 6: + try: + import ordereddict + except ImportError as e: + print("Warning: using Python 2.6 without module ordereddict.") + print("Upgrade to Python 2.7, or run 'pip install ordereddict'") + if "PYWIKIBOT2_DIR" not in os.environ: os.environ["PYWIKIBOT2_DIR"] = os.path.split(__file__)[0] diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index c9594e4..85687f0 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -30,6 +30,47 @@ from pywikibot import config2 as config from pywikibot.bot import * from pywikibot.exceptions import * + + +class NotImplementedClass(object): + + """Not implemented.""" + + fallback = False + fallback_message = None + warn_once = True + warned = False + + def __init__(self, *args, **kwargs): + if self.fallback: + if self.warn_once and not self.__class__.warned: + pywikibot.warning(self.__doc__) + pywikibot.warning(self.fallback_message) + self.__class__.warned = True + else: + pywikibot.error(self.__doc__) + raise NotImplementedError(self.__doc__) + super(NotImplementedClass, self).__init__(*args, **kwargs) + + +try: + from collections import OrderedDict +except ImportError: + try: + from ordereddict import OrderedDict + except ImportError: + class OrderedDict(NotImplementedClass, dict): + + """ No ordered dictionary implementation found. + + collections.OrderedDict is missing on python 2.6. + Please upgrade to python 2.7 or install ordereddict. + """ + + fallback = True + fallback_message = "Falling back to using dict." + + from pywikibot.textlib import * from pywikibot.i18n import translate diff --git a/pywikibot/page.py b/pywikibot/page.py index 1f1877b..8e6f82e 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -17,6 +17,7 @@ import pywikibot.site from pywikibot.tools import ComparableMixin import hashlib +from pywikibot import OrderedDict try: import htmlentitydefs @@ -2877,7 +2878,7 @@ if self.isQualifier and self.isReference: raise ValueError(u'Claim cannot be both a qualifier and reference.') self.sources = [] - self.qualifiers = collections.defaultdict(list) + self.qualifiers = OrderedDict() self.target = None self.snaktype = 'value' self.rank = 'normal' @@ -2920,6 +2921,7 @@ claim.sources.append(Claim.referenceFromJSON(site, source)) if 'qualifiers' in data: for prop in data['qualifiers']: + claim.qualifiers[prop] = [] for qualifier in data['qualifiers'][prop]: qual = Claim.qualifierFromJSON(site, qualifier) claim.qualifiers[prop].append(qual) @@ -2932,11 +2934,13 @@ bit differently, and require some more handling. """ - source = collections.defaultdict(list) + source = OrderedDict() for prop in list(data['snaks'].values()): for claimsnak in prop: claim = Claim.fromJSON(site, {'mainsnak': claimsnak, 'hash': data['hash']}) + if claim.getID() not in source: + source[claim.getID()] = [] source[claim.getID()].append(claim) return source diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index df37f95..099e25a 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -27,7 +27,8 @@ else: from html.parser import HTMLParser -from . import config2 as config +from pywikibot import config2 as config +from pywikibot import OrderedDict TEMP_REGEX = re.compile( '{{(?:msg:)?(?P<name>[^{\|]+?)(?:\|(?P<params>[^{]+?(?:{[^{]+?}[^{]*?)?))?}}') @@ -920,7 +921,7 @@ code = mwparserfromhell.parse(text) result = [] for template in code.filter_templates(recursive=True): - params = {} + params = OrderedDict() for param in template.params: params[unicode(param.name)] = unicode(param.value) result.append((unicode(template.name.strip()), params)) @@ -1043,7 +1044,7 @@ # Parameters paramString = m.group('params') - params = {} + params = OrderedDict() numbered_param = 1 if paramString: # Replace wikilinks with markers diff --git a/setup.py b/setup.py index 357a1fe..a0f21ee 100644 --- a/setup.py +++ b/setup.py @@ -22,12 +22,15 @@ test_deps = [] testcollector = "tests" +dependencies = ['httplib2>=0.6.0'] + if sys.version_info[0] == 2: if sys.version_info < (2, 6, 5): raise RuntimeError("ERROR: Pywikibot only runs under Python 2.6.5 or higher") elif sys.version_info[1] == 6: test_deps = ['unittest2'] testcollector = "tests.utils.collector" + dependencies.append('ordereddict') if sys.version_info[0] == 3: if not os.environ.get('PY3', False): @@ -38,7 +41,6 @@ print("ERROR: Python 3.3 or higher is required!") sys.exit(1) -dependencies = ['httplib2>=0.6.0'] if os.name != 'nt': # See bug 66010, Windows users will have issues # when trying to build the C modules. -- To view, visit https://gerrit.wikimedia.org/r/147665 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I265ee4726d124b8e5dfa8ec2e2f9314b2c2132e7 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: John Vandenberg <jay...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits