Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/distlib/compat.py: 70%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

54 statements  

1# -*- coding: utf-8 -*- 

2# 

3# Copyright (C) 2013-2026 Vinay Sajip. 

4# Licensed to the Python Software Foundation under a contributor agreement. 

5# See LICENSE.txt and CONTRIBUTORS.txt. 

6# 

7from __future__ import absolute_import 

8 

9import os 

10import re 

11import shutil 

12import sys 

13 

14try: 

15 import ssl 

16except ImportError: # pragma: no cover 

17 ssl = None 

18 

19if sys.version_info[0] < 3: # pragma: no cover 

20 from StringIO import StringIO 

21 string_types = basestring, 

22 text_type = unicode 

23 from types import FileType as file_type 

24 from codecs import open as text_open 

25 import __builtin__ as builtins 

26 import ConfigParser as configparser 

27 from urlparse import urlparse, urlunparse, urljoin, urlsplit, urlunsplit 

28 from urllib import (urlretrieve, quote as _quote, unquote, url2pathname, pathname2url, ContentTooShortError, 

29 splittype) 

30 

31 def quote(s): 

32 if isinstance(s, unicode): 

33 s = s.encode('utf-8') 

34 return _quote(s) 

35 

36 import urllib2 

37 from urllib2 import (Request, urlopen, URLError, HTTPError, HTTPBasicAuthHandler, HTTPPasswordMgr, HTTPHandler, 

38 HTTPRedirectHandler, build_opener) 

39 if ssl: 

40 from urllib2 import HTTPSHandler 

41 import httplib 

42 import xmlrpclib 

43 import Queue as queue 

44 from HTMLParser import HTMLParser 

45 import htmlentitydefs 

46 raw_input = raw_input 

47 from itertools import ifilter as filter 

48 from itertools import ifilterfalse as filterfalse 

49 

50 # Leaving this around for now, in case it needs resurrecting in some way 

51 # _userprog = None 

52 # def splituser(host): 

53 # """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.""" 

54 # global _userprog 

55 # if _userprog is None: 

56 # import re 

57 # _userprog = re.compile('^(.*)@(.*)$') 

58 

59 # match = _userprog.match(host) 

60 # if match: return match.group(1, 2) 

61 # return None, host 

62 

63else: # pragma: no cover 

64 from io import StringIO 

65 string_types = str, 

66 text_type = str 

67 from io import TextIOWrapper as file_type 

68 import builtins 

69 

70 def text_open(filename, mode='r', encoding=None): 

71 # Replaces codecs.open(), deprecated since Python 3.14: like it, don't 

72 # translate newlines when an encoding is given. 

73 if encoding is None: 

74 return open(filename, mode) 

75 return open(filename, mode, encoding=encoding, newline='') 

76 

77 import configparser 

78 from urllib.parse import (urlparse, urlunparse, urljoin, quote, unquote, urlsplit, urlunsplit, splittype) 

79 from urllib.request import (urlopen, urlretrieve, Request, url2pathname, pathname2url, HTTPBasicAuthHandler, 

80 HTTPPasswordMgr, HTTPHandler, HTTPRedirectHandler, build_opener) 

81 if ssl: 

82 from urllib.request import HTTPSHandler 

83 from urllib.error import HTTPError, URLError, ContentTooShortError 

84 import http.client as httplib 

85 import urllib.request as urllib2 

86 import xmlrpc.client as xmlrpclib 

87 import queue 

88 from html.parser import HTMLParser 

89 import html.entities as htmlentitydefs 

90 raw_input = input 

91 from itertools import filterfalse 

92 filter = filter 

93 

94try: 

95 from ssl import match_hostname, CertificateError 

96except ImportError: # pragma: no cover 

97 

98 class CertificateError(ValueError): 

99 pass 

100 

101 def _dnsname_match(dn, hostname, max_wildcards=1): 

102 """Matching according to RFC 6125, section 6.4.3 

103 

104 http://tools.ietf.org/html/rfc6125#section-6.4.3 

105 """ 

106 pats = [] 

107 if not dn: 

108 return False 

109 

110 parts = dn.split('.') 

111 leftmost, remainder = parts[0], parts[1:] 

112 

113 wildcards = leftmost.count('*') 

114 if wildcards > max_wildcards: 

115 # Issue #17980: avoid denials of service by refusing more 

116 # than one wildcard per fragment. A survey of established 

117 # policy among SSL implementations showed it to be a 

118 # reasonable choice. 

119 raise CertificateError('Too many wildcards in certificate DNS name: %r' % dn) 

120 

121 # speed up common case w/o wildcards 

122 if not wildcards: 

123 return dn.lower() == hostname.lower() 

124 

125 # RFC 6125, section 6.4.3, subitem 1. 

126 # The client SHOULD NOT attempt to match a presented identifier in which 

127 # the wildcard character comprises a label other than the left-most label. 

128 if leftmost == '*': 

129 # When '*' is a fragment by itself, it matches a non-empty dotless 

130 # fragment. 

131 pats.append('[^.]+') 

132 elif leftmost.startswith('xn--') or hostname.startswith('xn--'): 

133 # RFC 6125, section 6.4.3, subitem 3. 

134 # The client SHOULD NOT attempt to match a presented identifier 

135 # where the wildcard character is embedded within an A-label or 

136 # U-label of an internationalized domain name. 

137 pats.append(re.escape(leftmost)) 

138 else: 

139 # Otherwise, '*' matches any dotless string, e.g. www* 

140 pats.append(re.escape(leftmost).replace(r'\*', '[^.]*')) 

141 

142 # add the remaining fragments, ignore any wildcards 

143 for frag in remainder: 

144 pats.append(re.escape(frag)) 

145 

146 pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE) 

147 return pat.match(hostname) 

148 

149 def match_hostname(cert, hostname): 

150 """Verify that *cert* (in decoded format as returned by 

151 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125 

152 rules are followed, but IP addresses are not accepted for *hostname*. 

153 

154 CertificateError is raised on failure. On success, the function 

155 returns nothing. 

156 """ 

157 if not cert: 

158 raise ValueError("empty or no certificate, match_hostname needs a " 

159 "SSL socket or SSL context with either " 

160 "CERT_OPTIONAL or CERT_REQUIRED") 

161 dnsnames = [] 

162 san = cert.get('subjectAltName', ()) 

163 for key, value in san: 

164 if key == 'DNS': 

165 if _dnsname_match(value, hostname): 

166 return 

167 dnsnames.append(value) 

168 if not dnsnames: 

169 # The subject is only checked when there is no dNSName entry 

170 # in subjectAltName 

171 for sub in cert.get('subject', ()): 

172 for key, value in sub: 

173 # XXX according to RFC 2818, the most specific Common Name 

174 # must be used. 

175 if key == 'commonName': 

176 if _dnsname_match(value, hostname): 

177 return 

178 dnsnames.append(value) 

179 if len(dnsnames) > 1: 

180 raise CertificateError("hostname %r " 

181 "doesn't match either of %s" % (hostname, ', '.join(map(repr, dnsnames)))) 

182 elif len(dnsnames) == 1: 

183 raise CertificateError("hostname %r " 

184 "doesn't match %r" % (hostname, dnsnames[0])) 

185 else: 

186 raise CertificateError("no appropriate commonName or " 

187 "subjectAltName fields were found") 

188 

189 

190try: 

191 from types import SimpleNamespace as Container 

192except ImportError: # pragma: no cover 

193 

194 class Container(object): 

195 """ 

196 A generic container for when multiple values need to be returned 

197 """ 

198 

199 def __init__(self, **kwargs): 

200 self.__dict__.update(kwargs) 

201 

202 

203try: 

204 from shutil import which 

205except ImportError: # pragma: no cover 

206 # Implementation from Python 3.3 

207 def which(cmd, mode=os.F_OK | os.X_OK, path=None): 

208 """Given a command, mode, and a PATH string, return the path which 

209 conforms to the given mode on the PATH, or None if there is no such 

210 file. 

211 

212 `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result 

213 of os.environ.get("PATH"), or can be overridden with a custom search 

214 path. 

215 

216 """ 

217 

218 # Check that a given file can be accessed with the correct mode. 

219 # Additionally check that `file` is not a directory, as on Windows 

220 # directories pass the os.access check. 

221 def _access_check(fn, mode): 

222 return (os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn)) 

223 

224 # If we're given a path with a directory part, look it up directly rather 

225 # than referring to PATH directories. This includes checking relative to the 

226 # current directory, e.g. ./script 

227 if os.path.dirname(cmd): 

228 if _access_check(cmd, mode): 

229 return cmd 

230 return None 

231 

232 if path is None: 

233 path = os.environ.get("PATH", os.defpath) 

234 if not path: 

235 return None 

236 path = path.split(os.pathsep) 

237 

238 if sys.platform == "win32": 

239 # The current directory takes precedence on Windows. 

240 if os.curdir not in path: 

241 path.insert(0, os.curdir) 

242 

243 # PATHEXT is necessary to check on Windows. 

244 pathext = os.environ.get("PATHEXT", "").split(os.pathsep) 

245 # See if the given file matches any of the expected path extensions. 

246 # This will allow us to short circuit when given "python.exe". 

247 # If it does match, only test that one, otherwise we have to try 

248 # others. 

249 if any(cmd.lower().endswith(ext.lower()) for ext in pathext): 

250 files = [cmd] 

251 else: 

252 files = [cmd + ext for ext in pathext] 

253 else: 

254 # On other platforms you don't have things like PATHEXT to tell you 

255 # what file suffixes are executable, so just pass on cmd as-is. 

256 files = [cmd] 

257 

258 seen = set() 

259 for dir in path: 

260 normdir = os.path.normcase(dir) 

261 if normdir not in seen: 

262 seen.add(normdir) 

263 for thefile in files: 

264 name = os.path.join(dir, thefile) 

265 if _access_check(name, mode): 

266 return name 

267 return None 

268 

269 

270# ZipFile is a context manager in 2.7, but not in 2.6 

271 

272from zipfile import ZipFile as BaseZipFile 

273 

274if hasattr(BaseZipFile, '__enter__'): # pragma: no cover 

275 ZipFile = BaseZipFile 

276else: # pragma: no cover 

277 from zipfile import ZipExtFile as BaseZipExtFile 

278 

279 class ZipExtFile(BaseZipExtFile): 

280 

281 def __init__(self, base): 

282 self.__dict__.update(base.__dict__) 

283 

284 def __enter__(self): 

285 return self 

286 

287 def __exit__(self, *exc_info): 

288 self.close() 

289 # return None, so if an exception occurred, it will propagate 

290 

291 class ZipFile(BaseZipFile): 

292 

293 def __enter__(self): 

294 return self 

295 

296 def __exit__(self, *exc_info): 

297 self.close() 

298 # return None, so if an exception occurred, it will propagate 

299 

300 def open(self, *args, **kwargs): 

301 base = BaseZipFile.open(self, *args, **kwargs) 

302 return ZipExtFile(base) 

303 

304 

305try: 

306 from platform import python_implementation 

307except ImportError: # pragma: no cover 

308 

309 def python_implementation(): 

310 """Return a string identifying the Python implementation.""" 

311 if 'PyPy' in sys.version: 

312 return 'PyPy' 

313 if os.name == 'java': 

314 return 'Jython' 

315 if sys.version.startswith('IronPython'): 

316 return 'IronPython' 

317 return 'CPython' 

318 

319 

320import sysconfig 

321 

322try: 

323 callable = callable 

324except NameError: # pragma: no cover 

325 from collections.abc import Callable 

326 

327 def callable(obj): 

328 return isinstance(obj, Callable) 

329 

330 

331try: 

332 fsencode = os.fsencode 

333 fsdecode = os.fsdecode 

334except AttributeError: # pragma: no cover 

335 # Issue #99: on some systems (e.g. containerised), 

336 # sys.getfilesystemencoding() returns None, and we need a real value, 

337 # so fall back to utf-8. From the CPython 2.7 docs relating to Unix and 

338 # sys.getfilesystemencoding(): the return value is "the user’s preference 

339 # according to the result of nl_langinfo(CODESET), or None if the 

340 # nl_langinfo(CODESET) failed." 

341 _fsencoding = sys.getfilesystemencoding() or 'utf-8' 

342 if _fsencoding == 'mbcs': 

343 _fserrors = 'strict' 

344 else: 

345 _fserrors = 'surrogateescape' 

346 

347 def fsencode(filename): 

348 if isinstance(filename, bytes): 

349 return filename 

350 elif isinstance(filename, text_type): 

351 return filename.encode(_fsencoding, _fserrors) 

352 else: 

353 raise TypeError("expect bytes or str, not %s" % type(filename).__name__) 

354 

355 def fsdecode(filename): 

356 if isinstance(filename, text_type): 

357 return filename 

358 elif isinstance(filename, bytes): 

359 return filename.decode(_fsencoding, _fserrors) 

360 else: 

361 raise TypeError("expect bytes or str, not %s" % type(filename).__name__) 

362 

363 

364try: 

365 from tokenize import detect_encoding 

366except ImportError: # pragma: no cover 

367 from codecs import BOM_UTF8, lookup 

368 

369 cookie_re = re.compile(r"coding[:=]\s*([-\w.]+)") 

370 

371 def _get_normal_name(orig_enc): 

372 """Imitates get_normal_name in tokenizer.c.""" 

373 # Only care about the first 12 characters. 

374 enc = orig_enc[:12].lower().replace("_", "-") 

375 if enc == "utf-8" or enc.startswith("utf-8-"): 

376 return "utf-8" 

377 if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \ 

378 enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")): 

379 return "iso-8859-1" 

380 return orig_enc 

381 

382 def detect_encoding(readline): 

383 """ 

384 The detect_encoding() function is used to detect the encoding that should 

385 be used to decode a Python source file. It requires one argument, readline, 

386 in the same way as the tokenize() generator. 

387 

388 It will call readline a maximum of twice, and return the encoding used 

389 (as a string) and a list of any lines (left as bytes) it has read in. 

390 

391 It detects the encoding from the presence of a utf-8 bom or an encoding 

392 cookie as specified in pep-0263. If both a bom and a cookie are present, 

393 but disagree, a SyntaxError will be raised. If the encoding cookie is an 

394 invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found, 

395 'utf-8-sig' is returned. 

396 

397 If no encoding is specified, then the default of 'utf-8' will be returned. 

398 """ 

399 try: 

400 filename = readline.__self__.name 

401 except AttributeError: 

402 filename = None 

403 bom_found = False 

404 encoding = None 

405 default = 'utf-8' 

406 

407 def read_or_stop(): 

408 try: 

409 return readline() 

410 except StopIteration: 

411 return b'' 

412 

413 def find_cookie(line): 

414 try: 

415 # Decode as UTF-8. Either the line is an encoding declaration, 

416 # in which case it should be pure ASCII, or it must be UTF-8 

417 # per default encoding. 

418 line_string = line.decode('utf-8') 

419 except UnicodeDecodeError: 

420 msg = "invalid or missing encoding declaration" 

421 if filename is not None: 

422 msg = '{} for {!r}'.format(msg, filename) 

423 raise SyntaxError(msg) 

424 

425 matches = cookie_re.findall(line_string) 

426 if not matches: 

427 return None 

428 encoding = _get_normal_name(matches[0]) 

429 try: 

430 codec = lookup(encoding) 

431 except LookupError: 

432 # This behaviour mimics the Python interpreter 

433 if filename is None: 

434 msg = "unknown encoding: " + encoding 

435 else: 

436 msg = "unknown encoding for {!r}: {}".format(filename, encoding) 

437 raise SyntaxError(msg) 

438 

439 if bom_found: 

440 if codec.name != 'utf-8': 

441 # This behaviour mimics the Python interpreter 

442 if filename is None: 

443 msg = 'Encoding problem: utf-8' 

444 else: 

445 msg = 'Encoding problem for %r: utf-8' % filename 

446 raise SyntaxError(msg) 

447 encoding += '-sig' 

448 return encoding 

449 

450 first = read_or_stop() 

451 if first.startswith(BOM_UTF8): 

452 bom_found = True 

453 first = first[3:] 

454 default = 'utf-8-sig' 

455 if not first: 

456 return default, [] 

457 

458 encoding = find_cookie(first) 

459 if encoding: 

460 return encoding, [first] 

461 

462 second = read_or_stop() 

463 if not second: 

464 return default, [first] 

465 

466 encoding = find_cookie(second) 

467 if encoding: 

468 return encoding, [first, second] 

469 

470 return default, [first, second] 

471 

472 

473# For converting & <-> &amp; etc. 

474try: 

475 from html import escape 

476except ImportError: 

477 from cgi import escape 

478if sys.version_info[:2] < (3, 4): 

479 unescape = HTMLParser().unescape 

480else: 

481 from html import unescape 

482 

483_get_ident = None 

484 

485 

486def _thread_get_ident(): 

487 global _get_ident 

488 if _get_ident is None: 

489 try: 

490 # Python 2 

491 from thread import get_ident as _get_ident 

492 except ImportError: 

493 try: 

494 from dummy_thread import get_ident as _get_ident 

495 except ImportError: 

496 # Python >= 3 

497 try: 

498 from _thread import get_ident as _get_ident 

499 except ImportError: 

500 from _dummy_thread import get_ident as _get_ident 

501 return _get_ident 

502 

503 

504try: 

505 # Python >= 3.3 

506 from collections import ChainMap 

507except ImportError: # pragma: no cover 

508 from collections import MutableMapping 

509 

510 try: 

511 # Python >= 3.2 

512 from reprlib import recursive_repr as _recursive_repr 

513 except ImportError: 

514 

515 _get_ident = _thread_get_ident() 

516 

517 def _recursive_repr(fillvalue='...'): 

518 ''' 

519 Decorator to make a repr function return fillvalue for a recursive 

520 call 

521 ''' 

522 

523 def decorating_function(user_function): 

524 repr_running = set() 

525 

526 def wrapper(self): 

527 key = id(self), _get_ident() 

528 if key in repr_running: 

529 return fillvalue 

530 repr_running.add(key) 

531 try: 

532 result = user_function(self) 

533 finally: 

534 repr_running.discard(key) 

535 return result 

536 

537 # Can't use functools.wraps() here because of bootstrap issues 

538 wrapper.__module__ = getattr(user_function, '__module__') 

539 wrapper.__doc__ = getattr(user_function, '__doc__') 

540 wrapper.__name__ = getattr(user_function, '__name__') 

541 wrapper.__annotations__ = getattr(user_function, '__annotations__', {}) 

542 return wrapper 

543 

544 return decorating_function 

545 

546 class ChainMap(MutableMapping): 

547 ''' 

548 A ChainMap groups multiple dicts (or other mappings) together 

549 to create a single, updateable view. 

550 

551 The underlying mappings are stored in a list. That list is public and can 

552 accessed or updated using the *maps* attribute. There is no other state. 

553 

554 Lookups search the underlying mappings successively until a key is found. 

555 In contrast, writes, updates, and deletions only operate on the first 

556 mapping. 

557 ''' 

558 

559 def __init__(self, *maps): 

560 '''Initialize a ChainMap by setting *maps* to the given mappings. 

561 If no mappings are provided, a single empty dictionary is used. 

562 

563 ''' 

564 self.maps = list(maps) or [{}] # always at least one map 

565 

566 def __missing__(self, key): 

567 raise KeyError(key) 

568 

569 def __getitem__(self, key): 

570 for mapping in self.maps: 

571 try: 

572 return mapping[key] # can't use 'key in mapping' with defaultdict 

573 except KeyError: 

574 pass 

575 return self.__missing__(key) # support subclasses that define __missing__ 

576 

577 def get(self, key, default=None): 

578 return self[key] if key in self else default 

579 

580 def __len__(self): 

581 return len(set().union(*self.maps)) # reuses stored hash values if possible 

582 

583 def __iter__(self): 

584 return iter(set().union(*self.maps)) 

585 

586 def __contains__(self, key): 

587 return any(key in m for m in self.maps) 

588 

589 def __bool__(self): 

590 return any(self.maps) 

591 

592 @_recursive_repr() 

593 def __repr__(self): 

594 return '{0.__class__.__name__}({1})'.format(self, ', '.join(map(repr, self.maps))) 

595 

596 @classmethod 

597 def fromkeys(cls, iterable, *args): 

598 'Create a ChainMap with a single dict created from the iterable.' 

599 return cls(dict.fromkeys(iterable, *args)) 

600 

601 def copy(self): 

602 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]' 

603 return self.__class__(self.maps[0].copy(), *self.maps[1:]) 

604 

605 __copy__ = copy 

606 

607 def new_child(self): # like Django's Context.push() 

608 'New ChainMap with a new dict followed by all previous maps.' 

609 return self.__class__({}, *self.maps) 

610 

611 @property 

612 def parents(self): # like Django's Context.pop() 

613 'New ChainMap from maps[1:].' 

614 return self.__class__(*self.maps[1:]) 

615 

616 def __setitem__(self, key, value): 

617 self.maps[0][key] = value 

618 

619 def __delitem__(self, key): 

620 try: 

621 del self.maps[0][key] 

622 except KeyError: 

623 raise KeyError('Key not found in the first mapping: {!r}'.format(key)) 

624 

625 def popitem(self): 

626 'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.' 

627 try: 

628 return self.maps[0].popitem() 

629 except KeyError: 

630 raise KeyError('No keys found in the first mapping.') 

631 

632 def pop(self, key, *args): 

633 'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].' 

634 try: 

635 return self.maps[0].pop(key, *args) 

636 except KeyError: 

637 raise KeyError('Key not found in the first mapping: {!r}'.format(key)) 

638 

639 def clear(self): 

640 'Clear maps[0], leaving maps[1:] intact.' 

641 self.maps[0].clear() 

642 

643 

644try: 

645 from importlib.util import cache_from_source # Python >= 3.4 

646except ImportError: # pragma: no cover 

647 

648 def cache_from_source(path, debug_override=None, optimization=None): 

649 assert path.endswith('.py') 

650 if debug_override is None: 

651 debug_override = __debug__ 

652 if debug_override: 

653 suffix = 'c' 

654 else: 

655 suffix = 'o' 

656 return path + suffix 

657 

658 

659try: 

660 # Python >= 3.1 

661 from collections import OrderedDict 

662except ImportError: # pragma: no cover 

663 # {{{ http://code.activestate.com/recipes/576693/ (r9) 

664 # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy. 

665 # Passes Python2.7's test suite and incorporates all the latest updates. 

666 _get_ident = _thread_get_ident() 

667 

668 try: 

669 from _abcoll import KeysView, ValuesView, ItemsView 

670 except ImportError: 

671 pass 

672 

673 class OrderedDict(dict): 

674 'Dictionary that remembers insertion order' 

675 

676 # An inherited dict maps keys to values. 

677 # The inherited dict provides __getitem__, __len__, __contains__, and get. 

678 # The remaining methods are order-aware. 

679 # Big-O running times for all methods are the same as for regular dictionaries. 

680 

681 # The internal self.__map dictionary maps keys to links in a doubly linked list. 

682 # The circular doubly linked list starts and ends with a sentinel element. 

683 # The sentinel element never gets deleted (this simplifies the algorithm). 

684 # Each link is stored as a list of length three: [PREV, NEXT, KEY]. 

685 

686 def __init__(self, *args, **kwds): 

687 '''Initialize an ordered dictionary. Signature is the same as for 

688 regular dictionaries, but keyword arguments are not recommended 

689 because their insertion order is arbitrary. 

690 

691 ''' 

692 if len(args) > 1: 

693 raise TypeError('expected at most 1 arguments, got %d' % len(args)) 

694 try: 

695 self.__root 

696 except AttributeError: 

697 self.__root = root = [] # sentinel node 

698 root[:] = [root, root, None] 

699 self.__map = {} 

700 self.__update(*args, **kwds) 

701 

702 def __setitem__(self, key, value, dict_setitem=dict.__setitem__): 

703 'od.__setitem__(i, y) <==> od[i]=y' 

704 # Setting a new item creates a new link which goes at the end of the linked 

705 # list, and the inherited dictionary is updated with the new key/value pair. 

706 if key not in self: 

707 root = self.__root 

708 last = root[0] 

709 last[1] = root[0] = self.__map[key] = [last, root, key] 

710 dict_setitem(self, key, value) 

711 

712 def __delitem__(self, key, dict_delitem=dict.__delitem__): 

713 'od.__delitem__(y) <==> del od[y]' 

714 # Deleting an existing item uses self.__map to find the link which is 

715 # then removed by updating the links in the predecessor and successor nodes. 

716 dict_delitem(self, key) 

717 link_prev, link_next, key = self.__map.pop(key) 

718 link_prev[1] = link_next 

719 link_next[0] = link_prev 

720 

721 def __iter__(self): 

722 'od.__iter__() <==> iter(od)' 

723 root = self.__root 

724 curr = root[1] 

725 while curr is not root: 

726 yield curr[2] 

727 curr = curr[1] 

728 

729 def __reversed__(self): 

730 'od.__reversed__() <==> reversed(od)' 

731 root = self.__root 

732 curr = root[0] 

733 while curr is not root: 

734 yield curr[2] 

735 curr = curr[0] 

736 

737 def clear(self): 

738 'od.clear() -> None. Remove all items from od.' 

739 try: 

740 for node in self.__map.itervalues(): 

741 del node[:] 

742 root = self.__root 

743 root[:] = [root, root, None] 

744 self.__map.clear() 

745 except AttributeError: 

746 pass 

747 dict.clear(self) 

748 

749 def popitem(self, last=True): 

750 '''od.popitem() -> (k, v), return and remove a (key, value) pair. 

751 Pairs are returned in LIFO order if last is true or FIFO order if false. 

752 

753 ''' 

754 if not self: 

755 raise KeyError('dictionary is empty') 

756 root = self.__root 

757 if last: 

758 link = root[0] 

759 link_prev = link[0] 

760 link_prev[1] = root 

761 root[0] = link_prev 

762 else: 

763 link = root[1] 

764 link_next = link[1] 

765 root[1] = link_next 

766 link_next[0] = root 

767 key = link[2] 

768 del self.__map[key] 

769 value = dict.pop(self, key) 

770 return key, value 

771 

772 # -- the following methods do not depend on the internal structure -- 

773 

774 def keys(self): 

775 'od.keys() -> list of keys in od' 

776 return list(self) 

777 

778 def values(self): 

779 'od.values() -> list of values in od' 

780 return [self[key] for key in self] 

781 

782 def items(self): 

783 'od.items() -> list of (key, value) pairs in od' 

784 return [(key, self[key]) for key in self] 

785 

786 def iterkeys(self): 

787 'od.iterkeys() -> an iterator over the keys in od' 

788 return iter(self) 

789 

790 def itervalues(self): 

791 'od.itervalues -> an iterator over the values in od' 

792 for k in self: 

793 yield self[k] 

794 

795 def iteritems(self): 

796 'od.iteritems -> an iterator over the (key, value) items in od' 

797 for k in self: 

798 yield (k, self[k]) 

799 

800 def update(*args, **kwds): 

801 '''od.update(E, **F) -> None. Update od from dict/iterable E and F. 

802 

803 If E is a dict instance, does: for k in E: od[k] = E[k] 

804 If E has a .keys() method, does: for k in E.keys(): od[k] = E[k] 

805 Or if E is an iterable of items, does: for k, v in E: od[k] = v 

806 In either case, this is followed by: for k, v in F.items(): od[k] = v 

807 

808 ''' 

809 if len(args) > 2: 

810 raise TypeError('update() takes at most 2 positional ' 

811 'arguments (%d given)' % (len(args), )) 

812 elif not args: 

813 raise TypeError('update() takes at least 1 argument (0 given)') 

814 self = args[0] 

815 # Make progressively weaker assumptions about "other" 

816 other = () 

817 if len(args) == 2: 

818 other = args[1] 

819 if isinstance(other, dict): 

820 for key in other: 

821 self[key] = other[key] 

822 elif hasattr(other, 'keys'): 

823 for key in other.keys(): 

824 self[key] = other[key] 

825 else: 

826 for key, value in other: 

827 self[key] = value 

828 for key, value in kwds.items(): 

829 self[key] = value 

830 

831 __update = update # let subclasses override update without breaking __init__ 

832 

833 __marker = object() 

834 

835 def pop(self, key, default=__marker): 

836 '''od.pop(k[,d]) -> v, remove specified key and return the corresponding value. 

837 If key is not found, d is returned if given, otherwise KeyError is raised. 

838 

839 ''' 

840 if key in self: 

841 result = self[key] 

842 del self[key] 

843 return result 

844 if default is self.__marker: 

845 raise KeyError(key) 

846 return default 

847 

848 def setdefault(self, key, default=None): 

849 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od' 

850 if key in self: 

851 return self[key] 

852 self[key] = default 

853 return default 

854 

855 def __repr__(self, _repr_running=None): 

856 'od.__repr__() <==> repr(od)' 

857 if not _repr_running: 

858 _repr_running = {} 

859 call_key = id(self), _get_ident() 

860 if call_key in _repr_running: 

861 return '...' 

862 _repr_running[call_key] = 1 

863 try: 

864 if not self: 

865 return '%s()' % (self.__class__.__name__, ) 

866 return '%s(%r)' % (self.__class__.__name__, self.items()) 

867 finally: 

868 del _repr_running[call_key] 

869 

870 def __reduce__(self): 

871 'Return state information for pickling' 

872 items = [[k, self[k]] for k in self] 

873 inst_dict = vars(self).copy() 

874 for k in vars(OrderedDict()): 

875 inst_dict.pop(k, None) 

876 if inst_dict: 

877 return (self.__class__, (items, ), inst_dict) 

878 return self.__class__, (items, ) 

879 

880 def copy(self): 

881 'od.copy() -> a shallow copy of od' 

882 return self.__class__(self) 

883 

884 @classmethod 

885 def fromkeys(cls, iterable, value=None): 

886 '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S 

887 and values equal to v (which defaults to None). 

888 

889 ''' 

890 d = cls() 

891 for key in iterable: 

892 d[key] = value 

893 return d 

894 

895 def __eq__(self, other): 

896 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive 

897 while comparison to a regular mapping is order-insensitive. 

898 

899 ''' 

900 if isinstance(other, OrderedDict): 

901 return len(self) == len(other) and self.items() == other.items() 

902 return dict.__eq__(self, other) 

903 

904 def __ne__(self, other): 

905 return not self == other 

906 

907 # -- the following methods are only used in Python 2.7 -- 

908 

909 def viewkeys(self): 

910 "od.viewkeys() -> a set-like object providing a view on od's keys" 

911 return KeysView(self) 

912 

913 def viewvalues(self): 

914 "od.viewvalues() -> an object providing a view on od's values" 

915 return ValuesView(self) 

916 

917 def viewitems(self): 

918 "od.viewitems() -> a set-like object providing a view on od's items" 

919 return ItemsView(self) 

920 

921 

922try: 

923 from logging.config import BaseConfigurator, valid_ident 

924except ImportError: # pragma: no cover 

925 IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I) 

926 

927 def valid_ident(s): 

928 m = IDENTIFIER.match(s) 

929 if not m: 

930 raise ValueError('Not a valid Python identifier: %r' % s) 

931 return True 

932 

933 # The ConvertingXXX classes are wrappers around standard Python containers, 

934 # and they serve to convert any suitable values in the container. The 

935 # conversion converts base dicts, lists and tuples to their wrapped 

936 # equivalents, whereas strings which match a conversion format are converted 

937 # appropriately. 

938 # 

939 # Each wrapper should have a configurator attribute holding the actual 

940 # configurator to use for conversion. 

941 

942 class ConvertingDict(dict): 

943 """A converting dictionary wrapper.""" 

944 

945 def __getitem__(self, key): 

946 value = dict.__getitem__(self, key) 

947 result = self.configurator.convert(value) 

948 # If the converted value is different, save for next time 

949 if value is not result: 

950 self[key] = result 

951 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

952 result.parent = self 

953 result.key = key 

954 return result 

955 

956 def get(self, key, default=None): 

957 value = dict.get(self, key, default) 

958 result = self.configurator.convert(value) 

959 # If the converted value is different, save for next time 

960 if value is not result: 

961 self[key] = result 

962 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

963 result.parent = self 

964 result.key = key 

965 return result 

966 

967 def pop(self, key, default=None): 

968 value = dict.pop(self, key, default) 

969 result = self.configurator.convert(value) 

970 if value is not result: 

971 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

972 result.parent = self 

973 result.key = key 

974 return result 

975 

976 class ConvertingList(list): 

977 """A converting list wrapper.""" 

978 

979 def __getitem__(self, key): 

980 value = list.__getitem__(self, key) 

981 result = self.configurator.convert(value) 

982 # If the converted value is different, save for next time 

983 if value is not result: 

984 self[key] = result 

985 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

986 result.parent = self 

987 result.key = key 

988 return result 

989 

990 def pop(self, idx=-1): 

991 value = list.pop(self, idx) 

992 result = self.configurator.convert(value) 

993 if value is not result: 

994 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

995 result.parent = self 

996 return result 

997 

998 class ConvertingTuple(tuple): 

999 """A converting tuple wrapper.""" 

1000 

1001 def __getitem__(self, key): 

1002 value = tuple.__getitem__(self, key) 

1003 result = self.configurator.convert(value) 

1004 if value is not result: 

1005 if type(result) in (ConvertingDict, ConvertingList, ConvertingTuple): 

1006 result.parent = self 

1007 result.key = key 

1008 return result 

1009 

1010 class BaseConfigurator(object): 

1011 """ 

1012 The configurator base class which defines some useful defaults. 

1013 """ 

1014 

1015 CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$') 

1016 

1017 WORD_PATTERN = re.compile(r'^\s*(\w+)\s*') 

1018 DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*') 

1019 INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*') 

1020 DIGIT_PATTERN = re.compile(r'^\d+$') 

1021 

1022 value_converters = { 

1023 'ext': 'ext_convert', 

1024 'cfg': 'cfg_convert', 

1025 } 

1026 

1027 # We might want to use a different one, e.g. importlib 

1028 importer = staticmethod(__import__) 

1029 

1030 def __init__(self, config): 

1031 self.config = ConvertingDict(config) 

1032 self.config.configurator = self 

1033 

1034 def resolve(self, s): 

1035 """ 

1036 Resolve strings to objects using standard import and attribute 

1037 syntax. 

1038 """ 

1039 name = s.split('.') 

1040 used = name.pop(0) 

1041 try: 

1042 found = self.importer(used) 

1043 for frag in name: 

1044 used += '.' + frag 

1045 try: 

1046 found = getattr(found, frag) 

1047 except AttributeError: 

1048 self.importer(used) 

1049 found = getattr(found, frag) 

1050 return found 

1051 except ImportError: 

1052 e, tb = sys.exc_info()[1:] 

1053 v = ValueError('Cannot resolve %r: %s' % (s, e)) 

1054 v.__cause__, v.__traceback__ = e, tb 

1055 raise v 

1056 

1057 def ext_convert(self, value): 

1058 """Default converter for the ext:// protocol.""" 

1059 return self.resolve(value) 

1060 

1061 def cfg_convert(self, value): 

1062 """Default converter for the cfg:// protocol.""" 

1063 rest = value 

1064 m = self.WORD_PATTERN.match(rest) 

1065 if m is None: 

1066 raise ValueError("Unable to convert %r" % value) 

1067 else: 

1068 rest = rest[m.end():] 

1069 d = self.config[m.groups()[0]] 

1070 while rest: 

1071 m = self.DOT_PATTERN.match(rest) 

1072 if m: 

1073 d = d[m.groups()[0]] 

1074 else: 

1075 m = self.INDEX_PATTERN.match(rest) 

1076 if m: 

1077 idx = m.groups()[0] 

1078 if not self.DIGIT_PATTERN.match(idx): 

1079 d = d[idx] 

1080 else: 

1081 try: 

1082 n = int(idx) # try as number first (most likely) 

1083 d = d[n] 

1084 except TypeError: 

1085 d = d[idx] 

1086 if m: 

1087 rest = rest[m.end():] 

1088 else: 

1089 raise ValueError('Unable to convert ' 

1090 '%r at %r' % (value, rest)) 

1091 # rest should be empty 

1092 return d 

1093 

1094 def convert(self, value): 

1095 """ 

1096 Convert values to an appropriate type. dicts, lists and tuples are 

1097 replaced by their converting alternatives. Strings are checked to 

1098 see if they have a conversion format and are converted if they do. 

1099 """ 

1100 if not isinstance(value, ConvertingDict) and isinstance(value, dict): 

1101 value = ConvertingDict(value) 

1102 value.configurator = self 

1103 elif not isinstance(value, ConvertingList) and isinstance(value, list): 

1104 value = ConvertingList(value) 

1105 value.configurator = self 

1106 elif not isinstance(value, ConvertingTuple) and isinstance(value, tuple): 

1107 value = ConvertingTuple(value) 

1108 value.configurator = self 

1109 elif isinstance(value, string_types): 

1110 m = self.CONVERT_PATTERN.match(value) 

1111 if m: 

1112 d = m.groupdict() 

1113 prefix = d['prefix'] 

1114 converter = self.value_converters.get(prefix, None) 

1115 if converter: 

1116 suffix = d['suffix'] 

1117 converter = getattr(self, converter) 

1118 value = converter(suffix) 

1119 return value 

1120 

1121 def configure_custom(self, config): 

1122 """Configure an object with a user-supplied factory.""" 

1123 c = config.pop('()') 

1124 if not callable(c): 

1125 c = self.resolve(c) 

1126 props = config.pop('.', None) 

1127 # Check for valid identifiers 

1128 kwargs = dict([(k, config[k]) for k in config if valid_ident(k)]) 

1129 result = c(**kwargs) 

1130 if props: 

1131 for name, value in props.items(): 

1132 setattr(result, name, value) 

1133 return result 

1134 

1135 def as_tuple(self, value): 

1136 """Utility function which converts lists to tuples.""" 

1137 if isinstance(value, list): 

1138 value = tuple(value) 

1139 return value