|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +""" Utils to support python 2.6 compatibility """ |
| 4 | + |
| 5 | +from collections import MutableMapping |
| 6 | + |
| 7 | + |
| 8 | +def _import_module(module_uri): |
| 9 | + return __import__(module_uri, {}, {}, ['']) |
| 10 | + |
| 11 | + |
| 12 | +def import_module(module_uri): |
| 13 | + """ Import module by string 'from.path.module' |
| 14 | +
|
| 15 | + To support python 2.6 |
| 16 | + """ |
| 17 | + try: |
| 18 | + from importlib import import_module |
| 19 | + callback = import_module |
| 20 | + except ImportError: |
| 21 | + callback = _import_module |
| 22 | + |
| 23 | + return callback(module_uri) |
| 24 | + |
| 25 | + |
| 26 | +class _OrderedDict(dict, MutableMapping): |
| 27 | + """ |
| 28 | + Src: http://code.activestate.com/recipes/576669/ |
| 29 | + Author: Raymond Hettinger (Promoter of PEP which introduces OrderDict into |
| 30 | + colletions module in python >2.7) |
| 31 | + """ |
| 32 | + |
| 33 | + # Methods with direct access to underlying attributes |
| 34 | + |
| 35 | + def __init__(self, *args, **kwds): |
| 36 | + if len(args) > 1: |
| 37 | + raise TypeError('expected at 1 argument, got %d', len(args)) |
| 38 | + if not hasattr(self, '_keys'): |
| 39 | + self._keys = [] |
| 40 | + self.update(*args, **kwds) |
| 41 | + |
| 42 | + def clear(self): |
| 43 | + del self._keys[:] |
| 44 | + dict.clear(self) |
| 45 | + |
| 46 | + def __setitem__(self, key, value): |
| 47 | + if key not in self: |
| 48 | + self._keys.append(key) |
| 49 | + dict.__setitem__(self, key, value) |
| 50 | + |
| 51 | + def __delitem__(self, key): |
| 52 | + dict.__delitem__(self, key) |
| 53 | + self._keys.remove(key) |
| 54 | + |
| 55 | + def __iter__(self): |
| 56 | + return iter(self._keys) |
| 57 | + |
| 58 | + def __reversed__(self): |
| 59 | + return reversed(self._keys) |
| 60 | + |
| 61 | + def popitem(self): |
| 62 | + if not self: |
| 63 | + raise KeyError |
| 64 | + key = self._keys.pop() |
| 65 | + value = dict.pop(self, key) |
| 66 | + return key, value |
| 67 | + |
| 68 | + def __reduce__(self): |
| 69 | + items = [[k, self[k]] for k in self] |
| 70 | + inst_dict = vars(self).copy() |
| 71 | + inst_dict.pop('_keys', None) |
| 72 | + return (self.__class__, (items,), inst_dict) |
| 73 | + |
| 74 | + # Methods with indirect access via the above methods |
| 75 | + |
| 76 | + setdefault = MutableMapping.setdefault |
| 77 | + update = MutableMapping.update |
| 78 | + pop = MutableMapping.pop |
| 79 | + keys = MutableMapping.keys |
| 80 | + values = MutableMapping.values |
| 81 | + items = MutableMapping.items |
| 82 | + |
| 83 | + def __repr__(self): |
| 84 | + pairs = ', '.join(map('%r: %r'.__mod__, self.items())) |
| 85 | + return '%s({%s})' % (self.__class__.__name__, pairs) |
| 86 | + |
| 87 | + def copy(self): |
| 88 | + return self.__class__(self) |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def fromkeys(cls, iterable, value=None): |
| 92 | + d = cls() |
| 93 | + for key in iterable: |
| 94 | + d[key] = value |
| 95 | + return d |
| 96 | + |
| 97 | +try: |
| 98 | + from collections import OrderedDict |
| 99 | +except ImportError: |
| 100 | + OrderedDict = _OrderedDict |
0 commit comments