compat.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2013-2017 Vinay Sajip.
  4. # Licensed to the Python Software Foundation under a contributor agreement.
  5. # See LICENSE.txt and CONTRIBUTORS.txt.
  6. #
  7. from __future__ import absolute_import
  8. import os
  9. import re
  10. import shutil
  11. import sys
  12. try:
  13. import ssl
  14. except ImportError: # pragma: no cover
  15. ssl = None
  16. if sys.version_info[0] < 3: # pragma: no cover
  17. from StringIO import StringIO
  18. string_types = basestring,
  19. text_type = unicode
  20. from types import FileType as file_type
  21. import __builtin__ as builtins
  22. import ConfigParser as configparser
  23. from urlparse import urlparse, urlunparse, urljoin, urlsplit, urlunsplit
  24. from urllib import (urlretrieve, quote as _quote, unquote, url2pathname,
  25. pathname2url, ContentTooShortError, splittype)
  26. def quote(s):
  27. if isinstance(s, unicode):
  28. s = s.encode('utf-8')
  29. return _quote(s)
  30. import urllib2
  31. from urllib2 import (Request, urlopen, URLError, HTTPError,
  32. HTTPBasicAuthHandler, HTTPPasswordMgr, HTTPHandler,
  33. HTTPRedirectHandler, build_opener)
  34. if ssl:
  35. from urllib2 import HTTPSHandler
  36. import httplib
  37. import xmlrpclib
  38. import Queue as queue
  39. from HTMLParser import HTMLParser
  40. import htmlentitydefs
  41. raw_input = raw_input
  42. from itertools import ifilter as filter
  43. from itertools import ifilterfalse as filterfalse
  44. # Leaving this around for now, in case it needs resurrecting in some way
  45. # _userprog = None
  46. # def splituser(host):
  47. # """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'."""
  48. # global _userprog
  49. # if _userprog is None:
  50. # import re
  51. # _userprog = re.compile('^(.*)@(.*)$')
  52. # match = _userprog.match(host)
  53. # if match: return match.group(1, 2)
  54. # return None, host
  55. else: # pragma: no cover
  56. from io import StringIO
  57. string_types = str,
  58. text_type = str
  59. from io import TextIOWrapper as file_type
  60. import builtins
  61. import configparser
  62. from urllib.parse import (urlparse, urlunparse, urljoin, quote, unquote,
  63. urlsplit, urlunsplit, splittype)
  64. from urllib.request import (urlopen, urlretrieve, Request, url2pathname,
  65. pathname2url, HTTPBasicAuthHandler,
  66. HTTPPasswordMgr, HTTPHandler,
  67. HTTPRedirectHandler, build_opener)
  68. if ssl:
  69. from urllib.request import HTTPSHandler
  70. from urllib.error import HTTPError, URLError, ContentTooShortError
  71. import http.client as httplib
  72. import urllib.request as urllib2
  73. import xmlrpc.client as xmlrpclib
  74. import queue
  75. from html.parser import HTMLParser
  76. import html.entities as htmlentitydefs
  77. raw_input = input
  78. from itertools import filterfalse
  79. filter = filter
  80. try:
  81. from ssl import match_hostname, CertificateError
  82. except ImportError: # pragma: no cover
  83. class CertificateError(ValueError):
  84. pass
  85. def _dnsname_match(dn, hostname, max_wildcards=1):
  86. """Matching according to RFC 6125, section 6.4.3
  87. http://tools.ietf.org/html/rfc6125#section-6.4.3
  88. """
  89. pats = []
  90. if not dn:
  91. return False
  92. parts = dn.split('.')
  93. leftmost, remainder = parts[0], parts[1:]
  94. wildcards = leftmost.count('*')
  95. if wildcards > max_wildcards:
  96. # Issue #17980: avoid denials of service by refusing more
  97. # than one wildcard per fragment. A survey of established
  98. # policy among SSL implementations showed it to be a
  99. # reasonable choice.
  100. raise CertificateError(
  101. "too many wildcards in certificate DNS name: " + repr(dn))
  102. # speed up common case w/o wildcards
  103. if not wildcards:
  104. return dn.lower() == hostname.lower()
  105. # RFC 6125, section 6.4.3, subitem 1.
  106. # The client SHOULD NOT attempt to match a presented identifier in which
  107. # the wildcard character comprises a label other than the left-most label.
  108. if leftmost == '*':
  109. # When '*' is a fragment by itself, it matches a non-empty dotless
  110. # fragment.
  111. pats.append('[^.]+')
  112. elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
  113. # RFC 6125, section 6.4.3, subitem 3.
  114. # The client SHOULD NOT attempt to match a presented identifier
  115. # where the wildcard character is embedded within an A-label or
  116. # U-label of an internationalized domain name.
  117. pats.append(re.escape(leftmost))
  118. else:
  119. # Otherwise, '*' matches any dotless string, e.g. www*
  120. pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
  121. # add the remaining fragments, ignore any wildcards
  122. for frag in remainder:
  123. pats.append(re.escape(frag))
  124. pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
  125. return pat.match(hostname)
  126. def match_hostname(cert, hostname):
  127. """Verify that *cert* (in decoded format as returned by
  128. SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
  129. rules are followed, but IP addresses are not accepted for *hostname*.
  130. CertificateError is raised on failure. On success, the function
  131. returns nothing.
  132. """
  133. if not cert:
  134. raise ValueError("empty or no certificate, match_hostname needs a "
  135. "SSL socket or SSL context with either "
  136. "CERT_OPTIONAL or CERT_REQUIRED")
  137. dnsnames = []
  138. san = cert.get('subjectAltName', ())
  139. for key, value in san:
  140. if key == 'DNS':
  141. if _dnsname_match(value, hostname):
  142. return
  143. dnsnames.append(value)
  144. if not dnsnames:
  145. # The subject is only checked when there is no dNSName entry
  146. # in subjectAltName
  147. for sub in cert.get('subject', ()):
  148. for key, value in sub:
  149. # XXX according to RFC 2818, the most specific Common Name
  150. # must be used.
  151. if key == 'commonName':
  152. if _dnsname_match(value, hostname):
  153. return
  154. dnsnames.append(value)
  155. if len(dnsnames) > 1:
  156. raise CertificateError("hostname %r "
  157. "doesn't match either of %s" %
  158. (hostname, ', '.join(map(repr, dnsnames))))
  159. elif len(dnsnames) == 1:
  160. raise CertificateError("hostname %r "
  161. "doesn't match %r" %
  162. (hostname, dnsnames[0]))
  163. else:
  164. raise CertificateError("no appropriate commonName or "
  165. "subjectAltName fields were found")
  166. try:
  167. from types import SimpleNamespace as Container
  168. except ImportError: # pragma: no cover
  169. class Container(object):
  170. """
  171. A generic container for when multiple values need to be returned
  172. """
  173. def __init__(self, **kwargs):
  174. self.__dict__.update(kwargs)
  175. try:
  176. from shutil import which
  177. except ImportError: # pragma: no cover
  178. # Implementation from Python 3.3
  179. def which(cmd, mode=os.F_OK | os.X_OK, path=None):
  180. """Given a command, mode, and a PATH string, return the path which
  181. conforms to the given mode on the PATH, or None if there is no such
  182. file.
  183. `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
  184. of os.environ.get("PATH"), or can be overridden with a custom search
  185. path.
  186. """
  187. # Check that a given file can be accessed with the correct mode.
  188. # Additionally check that `file` is not a directory, as on Windows
  189. # directories pass the os.access check.
  190. def _access_check(fn, mode):
  191. return (os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn))
  192. # If we're given a path with a directory part, look it up directly rather
  193. # than referring to PATH directories. This includes checking relative to the
  194. # current directory, e.g. ./script
  195. if os.path.dirname(cmd):
  196. if _access_check(cmd, mode):
  197. return cmd
  198. return None
  199. if path is None:
  200. path = os.environ.get("PATH", os.defpath)
  201. if not path:
  202. return None
  203. path = path.split(os.pathsep)
  204. if sys.platform == "win32":
  205. # The current directory takes precedence on Windows.
  206. if os.curdir not in path:
  207. path.insert(0, os.curdir)
  208. # PATHEXT is necessary to check on Windows.
  209. pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
  210. # See if the given file matches any of the expected path extensions.
  211. # This will allow us to short circuit when given "python.exe".
  212. # If it does match, only test that one, otherwise we have to try
  213. # others.
  214. if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
  215. files = [cmd]
  216. else:
  217. files = [cmd + ext for ext in pathext]
  218. else:
  219. # On other platforms you don't have things like PATHEXT to tell you
  220. # what file suffixes are executable, so just pass on cmd as-is.
  221. files = [cmd]
  222. seen = set()
  223. for dir in path:
  224. normdir = os.path.normcase(dir)
  225. if normdir not in seen:
  226. seen.add(normdir)
  227. for thefile in files:
  228. name = os.path.join(dir, thefile)
  229. if _access_check(name, mode):
  230. return name
  231. return None
  232. # ZipFile is a context manager in 2.7, but not in 2.6
  233. from zipfile import ZipFile as BaseZipFile
  234. if hasattr(BaseZipFile, '__enter__'): # pragma: no cover
  235. ZipFile = BaseZipFile
  236. else: # pragma: no cover
  237. from zipfile import ZipExtFile as BaseZipExtFile
  238. class ZipExtFile(BaseZipExtFile):
  239. def __init__(self, base):
  240. self.__dict__.update(base.__dict__)
  241. def __enter__(self):
  242. return self
  243. def __exit__(self, *exc_info):
  244. self.close()
  245. # return None, so if an exception occurred, it will propagate
  246. class ZipFile(BaseZipFile):
  247. def __enter__(self):
  248. return self
  249. def __exit__(self, *exc_info):
  250. self.close()
  251. # return None, so if an exception occurred, it will propagate
  252. def open(self, *args, **kwargs):
  253. base = BaseZipFile.open(self, *args, **kwargs)
  254. return ZipExtFile(base)
  255. try:
  256. from platform import python_implementation
  257. except ImportError: # pragma: no cover
  258. def python_implementation():
  259. """Return a string identifying the Python implementation."""
  260. if 'PyPy' in sys.version:
  261. return 'PyPy'
  262. if os.name == 'java':
  263. return 'Jython'
  264. if sys.version.startswith('IronPython'):
  265. return 'IronPython'
  266. return 'CPython'
  267. import sysconfig
  268. try:
  269. callable = callable
  270. except NameError: # pragma: no cover
  271. from collections.abc import Callable
  272. def callable(obj):
  273. return isinstance(obj, Callable)
  274. try:
  275. fsencode = os.fsencode
  276. fsdecode = os.fsdecode
  277. except AttributeError: # pragma: no cover
  278. # Issue #99: on some systems (e.g. containerised),
  279. # sys.getfilesystemencoding() returns None, and we need a real value,
  280. # so fall back to utf-8. From the CPython 2.7 docs relating to Unix and
  281. # sys.getfilesystemencoding(): the return value is "the user’s preference
  282. # according to the result of nl_langinfo(CODESET), or None if the
  283. # nl_langinfo(CODESET) failed."
  284. _fsencoding = sys.getfilesystemencoding() or 'utf-8'
  285. if _fsencoding == 'mbcs':
  286. _fserrors = 'strict'
  287. else:
  288. _fserrors = 'surrogateescape'
  289. def fsencode(filename):
  290. if isinstance(filename, bytes):
  291. return filename
  292. elif isinstance(filename, text_type):
  293. return filename.encode(_fsencoding, _fserrors)
  294. else:
  295. raise TypeError("expect bytes or str, not %s" %
  296. type(filename).__name__)
  297. def fsdecode(filename):
  298. if isinstance(filename, text_type):
  299. return filename
  300. elif isinstance(filename, bytes):
  301. return filename.decode(_fsencoding, _fserrors)
  302. else:
  303. raise TypeError("expect bytes or str, not %s" %
  304. type(filename).__name__)
  305. try:
  306. from tokenize import detect_encoding
  307. except ImportError: # pragma: no cover
  308. from codecs import BOM_UTF8, lookup
  309. cookie_re = re.compile(r"coding[:=]\s*([-\w.]+)")
  310. def _get_normal_name(orig_enc):
  311. """Imitates get_normal_name in tokenizer.c."""
  312. # Only care about the first 12 characters.
  313. enc = orig_enc[:12].lower().replace("_", "-")
  314. if enc == "utf-8" or enc.startswith("utf-8-"):
  315. return "utf-8"
  316. if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \
  317. enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):
  318. return "iso-8859-1"
  319. return orig_enc
  320. def detect_encoding(readline):
  321. """
  322. The detect_encoding() function is used to detect the encoding that should
  323. be used to decode a Python source file. It requires one argument, readline,
  324. in the same way as the tokenize() generator.
  325. It will call readline a maximum of twice, and return the encoding used
  326. (as a string) and a list of any lines (left as bytes) it has read in.
  327. It detects the encoding from the presence of a utf-8 bom or an encoding
  328. cookie as specified in pep-0263. If both a bom and a cookie are present,
  329. but disagree, a SyntaxError will be raised. If the encoding cookie is an
  330. invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found,
  331. 'utf-8-sig' is returned.
  332. If no encoding is specified, then the default of 'utf-8' will be returned.
  333. """
  334. try:
  335. filename = readline.__self__.name
  336. except AttributeError:
  337. filename = None
  338. bom_found = False
  339. encoding = None
  340. default = 'utf-8'
  341. def read_or_stop():
  342. try:
  343. return readline()
  344. except StopIteration:
  345. return b''
  346. def find_cookie(line):
  347. try:
  348. # Decode as UTF-8. Either the line is an encoding declaration,
  349. # in which case it should be pure ASCII, or it must be UTF-8
  350. # per default encoding.
  351. line_string = line.decode('utf-8')
  352. except UnicodeDecodeError:
  353. msg = "invalid or missing encoding declaration"
  354. if filename is not None:
  355. msg = '{} for {!r}'.format(msg, filename)
  356. raise SyntaxError(msg)
  357. matches = cookie_re.findall(line_string)
  358. if not matches:
  359. return None
  360. encoding = _get_normal_name(matches[0])
  361. try:
  362. codec = lookup(encoding)
  363. except LookupError:
  364. # This behaviour mimics the Python interpreter
  365. if filename is None:
  366. msg = "unknown encoding: " + encoding
  367. else:
  368. msg = "unknown encoding for {!r}: {}".format(
  369. filename, encoding)
  370. raise SyntaxError(msg)
  371. if bom_found:
  372. if codec.name != 'utf-8':
  373. # This behaviour mimics the Python interpreter
  374. if filename is None:
  375. msg = 'encoding problem: utf-8'
  376. else:
  377. msg = 'encoding problem for {!r}: utf-8'.format(
  378. filename)
  379. raise SyntaxError(msg)
  380. encoding += '-sig'
  381. return encoding
  382. first = read_or_stop()
  383. if first.startswith(BOM_UTF8):
  384. bom_found = True
  385. first = first[3:]
  386. default = 'utf-8-sig'
  387. if not first:
  388. return default, []
  389. encoding = find_cookie(first)
  390. if encoding:
  391. return encoding, [first]
  392. second = read_or_stop()
  393. if not second:
  394. return default, [first]
  395. encoding = find_cookie(second)
  396. if encoding:
  397. return encoding, [first, second]
  398. return default, [first, second]
  399. # For converting & <-> &amp; etc.
  400. try:
  401. from html import escape
  402. except ImportError:
  403. from cgi import escape
  404. if sys.version_info[:2] < (3, 4):
  405. unescape = HTMLParser().unescape
  406. else:
  407. from html import unescape
  408. try:
  409. from collections import ChainMap
  410. except ImportError: # pragma: no cover
  411. from collections import MutableMapping
  412. try:
  413. from reprlib import recursive_repr as _recursive_repr
  414. except ImportError:
  415. def _recursive_repr(fillvalue='...'):
  416. '''
  417. Decorator to make a repr function return fillvalue for a recursive
  418. call
  419. '''
  420. def decorating_function(user_function):
  421. repr_running = set()
  422. def wrapper(self):
  423. key = id(self), get_ident()
  424. if key in repr_running:
  425. return fillvalue
  426. repr_running.add(key)
  427. try:
  428. result = user_function(self)
  429. finally:
  430. repr_running.discard(key)
  431. return result
  432. # Can't use functools.wraps() here because of bootstrap issues
  433. wrapper.__module__ = getattr(user_function, '__module__')
  434. wrapper.__doc__ = getattr(user_function, '__doc__')
  435. wrapper.__name__ = getattr(user_function, '__name__')
  436. wrapper.__annotations__ = getattr(user_function,
  437. '__annotations__', {})
  438. return wrapper
  439. return decorating_function
  440. class ChainMap(MutableMapping):
  441. '''
  442. A ChainMap groups multiple dicts (or other mappings) together
  443. to create a single, updateable view.
  444. The underlying mappings are stored in a list. That list is public and can
  445. accessed or updated using the *maps* attribute. There is no other state.
  446. Lookups search the underlying mappings successively until a key is found.
  447. In contrast, writes, updates, and deletions only operate on the first
  448. mapping.
  449. '''
  450. def __init__(self, *maps):
  451. '''Initialize a ChainMap by setting *maps* to the given mappings.
  452. If no mappings are provided, a single empty dictionary is used.
  453. '''
  454. self.maps = list(maps) or [{}] # always at least one map
  455. def __missing__(self, key):
  456. raise KeyError(key)
  457. def __getitem__(self, key):
  458. for mapping in self.maps:
  459. try:
  460. return mapping[
  461. key] # can't use 'key in mapping' with defaultdict
  462. except KeyError:
  463. pass
  464. return self.__missing__(
  465. key) # support subclasses that define __missing__
  466. def get(self, key, default=None):
  467. return self[key] if key in self else default
  468. def __len__(self):
  469. return len(set().union(
  470. *self.maps)) # reuses stored hash values if possible
  471. def __iter__(self):
  472. return iter(set().union(*self.maps))
  473. def __contains__(self, key):
  474. return any(key in m for m in self.maps)
  475. def __bool__(self):
  476. return any(self.maps)
  477. @_recursive_repr()
  478. def __repr__(self):
  479. return '{0.__class__.__name__}({1})'.format(
  480. self, ', '.join(map(repr, self.maps)))
  481. @classmethod
  482. def fromkeys(cls, iterable, *args):
  483. 'Create a ChainMap with a single dict created from the iterable.'
  484. return cls(dict.fromkeys(iterable, *args))
  485. def copy(self):
  486. 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
  487. return self.__class__(self.maps[0].copy(), *self.maps[1:])
  488. __copy__ = copy
  489. def new_child(self): # like Django's Context.push()
  490. 'New ChainMap with a new dict followed by all previous maps.'
  491. return self.__class__({}, *self.maps)
  492. @property
  493. def parents(self): # like Django's Context.pop()
  494. 'New ChainMap from maps[1:].'
  495. return self.__class__(*self.maps[1:])
  496. def __setitem__(self, key, value):
  497. self.maps[0][key] = value
  498. def __delitem__(self, key):
  499. try:
  500. del self.maps[0][key]
  501. except KeyError:
  502. raise KeyError(
  503. 'Key not found in the first mapping: {!r}'.format(key))
  504. def popitem(self):
  505. 'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
  506. try:
  507. return self.maps[0].popitem()
  508. except KeyError:
  509. raise KeyError('No keys found in the first mapping.')
  510. def pop(self, key, *args):
  511. 'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
  512. try:
  513. return self.maps[0].pop(key, *args)
  514. except KeyError:
  515. raise KeyError(
  516. 'Key not found in the first mapping: {!r}'.format(key))
  517. def clear(self):
  518. 'Clear maps[0], leaving maps[1:] intact.'
  519. self.maps[0].clear()
  520. try:
  521. from importlib.util import cache_from_source # Python >= 3.4
  522. except ImportError: # pragma: no cover
  523. def cache_from_source(path, debug_override=None):
  524. assert path.endswith('.py')
  525. if debug_override is None:
  526. debug_override = __debug__
  527. if debug_override:
  528. suffix = 'c'
  529. else:
  530. suffix = 'o'
  531. return path + suffix
  532. try:
  533. from collections import OrderedDict
  534. except ImportError: # pragma: no cover
  535. # {{{ http://code.activestate.com/recipes/576693/ (r9)
  536. # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
  537. # Passes Python2.7's test suite and incorporates all the latest updates.
  538. try:
  539. from thread import get_ident as _get_ident
  540. except ImportError:
  541. from dummy_thread import get_ident as _get_ident
  542. try:
  543. from _abcoll import KeysView, ValuesView, ItemsView
  544. except ImportError:
  545. pass
  546. class OrderedDict(dict):
  547. 'Dictionary that remembers insertion order'
  548. # An inherited dict maps keys to values.
  549. # The inherited dict provides __getitem__, __len__, __contains__, and get.
  550. # The remaining methods are order-aware.
  551. # Big-O running times for all methods are the same as for regular dictionaries.
  552. # The internal self.__map dictionary maps keys to links in a doubly linked list.
  553. # The circular doubly linked list starts and ends with a sentinel element.
  554. # The sentinel element never gets deleted (this simplifies the algorithm).
  555. # Each link is stored as a list of length three: [PREV, NEXT, KEY].
  556. def __init__(self, *args, **kwds):
  557. '''Initialize an ordered dictionary. Signature is the same as for
  558. regular dictionaries, but keyword arguments are not recommended
  559. because their insertion order is arbitrary.
  560. '''
  561. if len(args) > 1:
  562. raise TypeError('expected at most 1 arguments, got %d' %
  563. len(args))
  564. try:
  565. self.__root
  566. except AttributeError:
  567. self.__root = root = [] # sentinel node
  568. root[:] = [root, root, None]
  569. self.__map = {}
  570. self.__update(*args, **kwds)
  571. def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
  572. 'od.__setitem__(i, y) <==> od[i]=y'
  573. # Setting a new item creates a new link which goes at the end of the linked
  574. # list, and the inherited dictionary is updated with the new key/value pair.
  575. if key not in self:
  576. root = self.__root
  577. last = root[0]
  578. last[1] = root[0] = self.__map[key] = [last, root, key]
  579. dict_setitem(self, key, value)
  580. def __delitem__(self, key, dict_delitem=dict.__delitem__):
  581. 'od.__delitem__(y) <==> del od[y]'
  582. # Deleting an existing item uses self.__map to find the link which is
  583. # then removed by updating the links in the predecessor and successor nodes.
  584. dict_delitem(self, key)
  585. link_prev, link_next, key = self.__map.pop(key)
  586. link_prev[1] = link_next
  587. link_next[0] = link_prev
  588. def __iter__(self):
  589. 'od.__iter__() <==> iter(od)'
  590. root = self.__root
  591. curr = root[1]
  592. while curr is not root:
  593. yield curr[2]
  594. curr = curr[1]
  595. def __reversed__(self):
  596. 'od.__reversed__() <==> reversed(od)'
  597. root = self.__root
  598. curr = root[0]
  599. while curr is not root:
  600. yield curr[2]
  601. curr = curr[0]
  602. def clear(self):
  603. 'od.clear() -> None. Remove all items from od.'
  604. try:
  605. for node in self.__map.itervalues():
  606. del node[:]
  607. root = self.__root
  608. root[:] = [root, root, None]
  609. self.__map.clear()
  610. except AttributeError:
  611. pass
  612. dict.clear(self)
  613. def popitem(self, last=True):
  614. '''od.popitem() -> (k, v), return and remove a (key, value) pair.
  615. Pairs are returned in LIFO order if last is true or FIFO order if false.
  616. '''
  617. if not self:
  618. raise KeyError('dictionary is empty')
  619. root = self.__root
  620. if last:
  621. link = root[0]
  622. link_prev = link[0]
  623. link_prev[1] = root
  624. root[0] = link_prev
  625. else:
  626. link = root[1]
  627. link_next = link[1]
  628. root[1] = link_next
  629. link_next[0] = root
  630. key = link[2]
  631. del self.__map[key]
  632. value = dict.pop(self, key)
  633. return key, value
  634. # -- the following methods do not depend on the internal structure --
  635. def keys(self):
  636. 'od.keys() -> list of keys in od'
  637. return list(self)
  638. def values(self):
  639. 'od.values() -> list of values in od'
  640. return [self[key] for key in self]
  641. def items(self):
  642. 'od.items() -> list of (key, value) pairs in od'
  643. return [(key, self[key]) for key in self]
  644. def iterkeys(self):
  645. 'od.iterkeys() -> an iterator over the keys in od'
  646. return iter(self)
  647. def itervalues(self):
  648. 'od.itervalues -> an iterator over the values in od'
  649. for k in self:
  650. yield self[k]
  651. def iteritems(self):
  652. 'od.iteritems -> an iterator over the (key, value) items in od'
  653. for k in self:
  654. yield (k, self[k])
  655. def update(*args, **kwds):
  656. '''od.update(E, **F) -> None. Update od from dict/iterable E and F.
  657. If E is a dict instance, does: for k in E: od[k] = E[k]
  658. If E has a .keys() method, does: for k in E.keys(): od[k] = E[k]
  659. Or if E is an iterable of items, does: for k, v in E: od[k] = v
  660. In either case, this is followed by: for k, v in F.items(): od[k] = v
  661. '''
  662. if len(args) > 2:
  663. raise TypeError('update() takes at most 2 positional '
  664. 'arguments (%d given)' % (len(args), ))
  665. elif not args:
  666. raise TypeError('update() takes at least 1 argument (0 given)')
  667. self = args[0]
  668. # Make progressively weaker assumptions about "other"
  669. other = ()
  670. if len(args) == 2:
  671. other = args[1]
  672. if isinstance(other, dict):
  673. for key in other:
  674. self[key] = other[key]
  675. elif hasattr(other, 'keys'):
  676. for key in other.keys():
  677. self[key] = other[key]
  678. else:
  679. for key, value in other:
  680. self[key] = value
  681. for key, value in kwds.items():
  682. self[key] = value
  683. __update = update # let subclasses override update without breaking __init__
  684. __marker = object()
  685. def pop(self, key, default=__marker):
  686. '''od.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  687. If key is not found, d is returned if given, otherwise KeyError is raised.
  688. '''
  689. if key in self:
  690. result = self[key]
  691. del self[key]
  692. return result
  693. if default is self.__marker:
  694. raise KeyError(key)
  695. return default
  696. def setdefault(self, key, default=None):
  697. 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
  698. if key in self:
  699. return self[key]
  700. self[key] = default
  701. return default
  702. def __repr__(self, _repr_running=None):
  703. 'od.__repr__() <==> repr(od)'
  704. if not _repr_running:
  705. _repr_running = {}
  706. call_key = id(self), _get_ident()
  707. if call_key in _repr_running:
  708. return '...'
  709. _repr_running[call_key] = 1
  710. try:
  711. if not self:
  712. return '%s()' % (self.__class__.__name__, )
  713. return '%s(%r)' % (self.__class__.__name__, self.items())
  714. finally:
  715. del _repr_running[call_key]
  716. def __reduce__(self):
  717. 'Return state information for pickling'
  718. items = [[k, self[k]] for k in self]
  719. inst_dict = vars(self).copy()
  720. for k in vars(OrderedDict()):
  721. inst_dict.pop(k, None)
  722. if inst_dict:
  723. return (self.__class__, (items, ), inst_dict)
  724. return self.__class__, (items, )
  725. def copy(self):
  726. 'od.copy() -> a shallow copy of od'
  727. return self.__class__(self)
  728. @classmethod
  729. def fromkeys(cls, iterable, value=None):
  730. '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
  731. and values equal to v (which defaults to None).
  732. '''
  733. d = cls()
  734. for key in iterable:
  735. d[key] = value
  736. return d
  737. def __eq__(self, other):
  738. '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
  739. while comparison to a regular mapping is order-insensitive.
  740. '''
  741. if isinstance(other, OrderedDict):
  742. return len(self) == len(
  743. other) and self.items() == other.items()
  744. return dict.__eq__(self, other)
  745. def __ne__(self, other):
  746. return not self == other
  747. # -- the following methods are only used in Python 2.7 --
  748. def viewkeys(self):
  749. "od.viewkeys() -> a set-like object providing a view on od's keys"
  750. return KeysView(self)
  751. def viewvalues(self):
  752. "od.viewvalues() -> an object providing a view on od's values"
  753. return ValuesView(self)
  754. def viewitems(self):
  755. "od.viewitems() -> a set-like object providing a view on od's items"
  756. return ItemsView(self)
  757. try:
  758. from logging.config import BaseConfigurator, valid_ident
  759. except ImportError: # pragma: no cover
  760. IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
  761. def valid_ident(s):
  762. m = IDENTIFIER.match(s)
  763. if not m:
  764. raise ValueError('Not a valid Python identifier: %r' % s)
  765. return True
  766. # The ConvertingXXX classes are wrappers around standard Python containers,
  767. # and they serve to convert any suitable values in the container. The
  768. # conversion converts base dicts, lists and tuples to their wrapped
  769. # equivalents, whereas strings which match a conversion format are converted
  770. # appropriately.
  771. #
  772. # Each wrapper should have a configurator attribute holding the actual
  773. # configurator to use for conversion.
  774. class ConvertingDict(dict):
  775. """A converting dictionary wrapper."""
  776. def __getitem__(self, key):
  777. value = dict.__getitem__(self, key)
  778. result = self.configurator.convert(value)
  779. # If the converted value is different, save for next time
  780. if value is not result:
  781. self[key] = result
  782. if type(result) in (ConvertingDict, ConvertingList,
  783. ConvertingTuple):
  784. result.parent = self
  785. result.key = key
  786. return result
  787. def get(self, key, default=None):
  788. value = dict.get(self, key, default)
  789. result = self.configurator.convert(value)
  790. # If the converted value is different, save for next time
  791. if value is not result:
  792. self[key] = result
  793. if type(result) in (ConvertingDict, ConvertingList,
  794. ConvertingTuple):
  795. result.parent = self
  796. result.key = key
  797. return result
  798. def pop(self, key, default=None):
  799. value = dict.pop(self, key, default)
  800. result = self.configurator.convert(value)
  801. if value is not result:
  802. if type(result) in (ConvertingDict, ConvertingList,
  803. ConvertingTuple):
  804. result.parent = self
  805. result.key = key
  806. return result
  807. class ConvertingList(list):
  808. """A converting list wrapper."""
  809. def __getitem__(self, key):
  810. value = list.__getitem__(self, key)
  811. result = self.configurator.convert(value)
  812. # If the converted value is different, save for next time
  813. if value is not result:
  814. self[key] = result
  815. if type(result) in (ConvertingDict, ConvertingList,
  816. ConvertingTuple):
  817. result.parent = self
  818. result.key = key
  819. return result
  820. def pop(self, idx=-1):
  821. value = list.pop(self, idx)
  822. result = self.configurator.convert(value)
  823. if value is not result:
  824. if type(result) in (ConvertingDict, ConvertingList,
  825. ConvertingTuple):
  826. result.parent = self
  827. return result
  828. class ConvertingTuple(tuple):
  829. """A converting tuple wrapper."""
  830. def __getitem__(self, key):
  831. value = tuple.__getitem__(self, key)
  832. result = self.configurator.convert(value)
  833. if value is not result:
  834. if type(result) in (ConvertingDict, ConvertingList,
  835. ConvertingTuple):
  836. result.parent = self
  837. result.key = key
  838. return result
  839. class BaseConfigurator(object):
  840. """
  841. The configurator base class which defines some useful defaults.
  842. """
  843. CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
  844. WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
  845. DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*')
  846. INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*')
  847. DIGIT_PATTERN = re.compile(r'^\d+$')
  848. value_converters = {
  849. 'ext': 'ext_convert',
  850. 'cfg': 'cfg_convert',
  851. }
  852. # We might want to use a different one, e.g. importlib
  853. importer = staticmethod(__import__)
  854. def __init__(self, config):
  855. self.config = ConvertingDict(config)
  856. self.config.configurator = self
  857. def resolve(self, s):
  858. """
  859. Resolve strings to objects using standard import and attribute
  860. syntax.
  861. """
  862. name = s.split('.')
  863. used = name.pop(0)
  864. try:
  865. found = self.importer(used)
  866. for frag in name:
  867. used += '.' + frag
  868. try:
  869. found = getattr(found, frag)
  870. except AttributeError:
  871. self.importer(used)
  872. found = getattr(found, frag)
  873. return found
  874. except ImportError:
  875. e, tb = sys.exc_info()[1:]
  876. v = ValueError('Cannot resolve %r: %s' % (s, e))
  877. v.__cause__, v.__traceback__ = e, tb
  878. raise v
  879. def ext_convert(self, value):
  880. """Default converter for the ext:// protocol."""
  881. return self.resolve(value)
  882. def cfg_convert(self, value):
  883. """Default converter for the cfg:// protocol."""
  884. rest = value
  885. m = self.WORD_PATTERN.match(rest)
  886. if m is None:
  887. raise ValueError("Unable to convert %r" % value)
  888. else:
  889. rest = rest[m.end():]
  890. d = self.config[m.groups()[0]]
  891. while rest:
  892. m = self.DOT_PATTERN.match(rest)
  893. if m:
  894. d = d[m.groups()[0]]
  895. else:
  896. m = self.INDEX_PATTERN.match(rest)
  897. if m:
  898. idx = m.groups()[0]
  899. if not self.DIGIT_PATTERN.match(idx):
  900. d = d[idx]
  901. else:
  902. try:
  903. n = int(
  904. idx
  905. ) # try as number first (most likely)
  906. d = d[n]
  907. except TypeError:
  908. d = d[idx]
  909. if m:
  910. rest = rest[m.end():]
  911. else:
  912. raise ValueError('Unable to convert '
  913. '%r at %r' % (value, rest))
  914. # rest should be empty
  915. return d
  916. def convert(self, value):
  917. """
  918. Convert values to an appropriate type. dicts, lists and tuples are
  919. replaced by their converting alternatives. Strings are checked to
  920. see if they have a conversion format and are converted if they do.
  921. """
  922. if not isinstance(value, ConvertingDict) and isinstance(
  923. value, dict):
  924. value = ConvertingDict(value)
  925. value.configurator = self
  926. elif not isinstance(value, ConvertingList) and isinstance(
  927. value, list):
  928. value = ConvertingList(value)
  929. value.configurator = self
  930. elif not isinstance(value, ConvertingTuple) and isinstance(value, tuple):
  931. value = ConvertingTuple(value)
  932. value.configurator = self
  933. elif isinstance(value, string_types):
  934. m = self.CONVERT_PATTERN.match(value)
  935. if m:
  936. d = m.groupdict()
  937. prefix = d['prefix']
  938. converter = self.value_converters.get(prefix, None)
  939. if converter:
  940. suffix = d['suffix']
  941. converter = getattr(self, converter)
  942. value = converter(suffix)
  943. return value
  944. def configure_custom(self, config):
  945. """Configure an object with a user-supplied factory."""
  946. c = config.pop('()')
  947. if not callable(c):
  948. c = self.resolve(c)
  949. props = config.pop('.', None)
  950. # Check for valid identifiers
  951. kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
  952. result = c(**kwargs)
  953. if props:
  954. for name, value in props.items():
  955. setattr(result, name, value)
  956. return result
  957. def as_tuple(self, value):
  958. """Utility function which converts lists to tuples."""
  959. if isinstance(value, list):
  960. value = tuple(value)
  961. return value