comparison planemo/lib/python3.7/site-packages/importlib_metadata/_compat.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 from __future__ import absolute_import, unicode_literals
2
3 import io
4 import abc
5 import sys
6 import email
7
8
9 if sys.version_info > (3,): # pragma: nocover
10 import builtins
11 from configparser import ConfigParser
12 import contextlib
13 FileNotFoundError = builtins.FileNotFoundError
14 IsADirectoryError = builtins.IsADirectoryError
15 NotADirectoryError = builtins.NotADirectoryError
16 PermissionError = builtins.PermissionError
17 map = builtins.map
18 from itertools import filterfalse
19 else: # pragma: nocover
20 from backports.configparser import ConfigParser
21 from itertools import imap as map # type: ignore
22 from itertools import ifilterfalse as filterfalse
23 import contextlib2 as contextlib
24 FileNotFoundError = IOError, OSError
25 IsADirectoryError = IOError, OSError
26 NotADirectoryError = IOError, OSError
27 PermissionError = IOError, OSError
28
29 str = type('')
30
31 suppress = contextlib.suppress
32
33 if sys.version_info > (3, 5): # pragma: nocover
34 import pathlib
35 else: # pragma: nocover
36 import pathlib2 as pathlib
37
38 try:
39 ModuleNotFoundError = builtins.FileNotFoundError
40 except (NameError, AttributeError): # pragma: nocover
41 ModuleNotFoundError = ImportError # type: ignore
42
43
44 if sys.version_info >= (3,): # pragma: nocover
45 from importlib.abc import MetaPathFinder
46 else: # pragma: nocover
47 class MetaPathFinder(object):
48 __metaclass__ = abc.ABCMeta
49
50
51 __metaclass__ = type
52 __all__ = [
53 'install', 'NullFinder', 'MetaPathFinder', 'ModuleNotFoundError',
54 'pathlib', 'ConfigParser', 'map', 'suppress', 'FileNotFoundError',
55 'NotADirectoryError', 'email_message_from_string',
56 ]
57
58
59 def install(cls):
60 """
61 Class decorator for installation on sys.meta_path.
62
63 Adds the backport DistributionFinder to sys.meta_path and
64 attempts to disable the finder functionality of the stdlib
65 DistributionFinder.
66 """
67 sys.meta_path.append(cls())
68 disable_stdlib_finder()
69 return cls
70
71
72 def disable_stdlib_finder():
73 """
74 Give the backport primacy for discovering path-based distributions
75 by monkey-patching the stdlib O_O.
76
77 See #91 for more background for rationale on this sketchy
78 behavior.
79 """
80 def matches(finder):
81 return (
82 getattr(finder, '__module__', None) == '_frozen_importlib_external'
83 and hasattr(finder, 'find_distributions')
84 )
85 for finder in filter(matches, sys.meta_path): # pragma: nocover
86 del finder.find_distributions
87
88
89 class NullFinder:
90 """
91 A "Finder" (aka "MetaClassFinder") that never finds any modules,
92 but may find distributions.
93 """
94 @staticmethod
95 def find_spec(*args, **kwargs):
96 return None
97
98 # In Python 2, the import system requires finders
99 # to have a find_module() method, but this usage
100 # is deprecated in Python 3 in favor of find_spec().
101 # For the purposes of this finder (i.e. being present
102 # on sys.meta_path but having no other import
103 # system functionality), the two methods are identical.
104 find_module = find_spec
105
106
107 def py2_message_from_string(text): # nocoverpy3
108 # Work around https://bugs.python.org/issue25545 where
109 # email.message_from_string cannot handle Unicode on Python 2.
110 io_buffer = io.StringIO(text)
111 return email.message_from_file(io_buffer)
112
113
114 email_message_from_string = (
115 py2_message_from_string
116 if sys.version_info < (3,) else
117 email.message_from_string
118 )
119
120
121 class PyPy_repr:
122 """
123 Override repr for EntryPoint objects on PyPy to avoid __iter__ access.
124 Ref #97, #102.
125 """
126 affected = hasattr(sys, 'pypy_version_info')
127
128 def __compat_repr__(self): # pragma: nocover
129 def make_param(name):
130 value = getattr(self, name)
131 return '{name}={value!r}'.format(**locals())
132 params = ', '.join(map(make_param, self._fields))
133 return 'EntryPoint({params})'.format(**locals())
134
135 if affected: # pragma: nocover
136 __repr__ = __compat_repr__
137 del affected
138
139
140 # from itertools recipes
141 def unique_everseen(iterable): # pragma: nocover
142 "List unique elements, preserving order. Remember all elements ever seen."
143 seen = set()
144 seen_add = seen.add
145
146 for element in filterfalse(seen.__contains__, iterable):
147 seen_add(element)
148 yield element
149
150
151 unique_ordered = (
152 unique_everseen if sys.version_info < (3, 7) else dict.fromkeys)