|
7 | 7 | import email |
8 | 8 | import pathlib |
9 | 9 | import operator |
| 10 | +import textwrap |
10 | 11 | import warnings |
11 | 12 | import functools |
12 | 13 | import itertools |
|
24 | 25 | from ._functools import method_cache |
25 | 26 | from ._itertools import unique_everseen |
26 | 27 |
|
27 | | -from configparser import ConfigParser |
28 | 28 | from contextlib import suppress |
29 | 29 | from importlib import import_module |
30 | 30 | from importlib.abc import MetaPathFinder |
@@ -60,6 +60,60 @@ def name(self): |
60 | 60 | return name |
61 | 61 |
|
62 | 62 |
|
| 63 | +class Sectioned: |
| 64 | + """ |
| 65 | + A simple entry point config parser for performance |
| 66 | +
|
| 67 | + >>> res = Sectioned.get_sections(Sectioned._sample) |
| 68 | + >>> sec, values = next(res) |
| 69 | + >>> sec |
| 70 | + 'sec1' |
| 71 | + >>> [(key, value) for key, value in values] |
| 72 | + [('a', '1'), ('b', '2')] |
| 73 | + >>> sec, values = next(res) |
| 74 | + >>> sec |
| 75 | + 'sec2' |
| 76 | + >>> [(key, value) for key, value in values] |
| 77 | + [('a', '2')] |
| 78 | + >>> list(res) |
| 79 | + [] |
| 80 | + """ |
| 81 | + |
| 82 | + _sample = textwrap.dedent( |
| 83 | + """ |
| 84 | + [sec1] |
| 85 | + a = 1 |
| 86 | + b = 2 |
| 87 | +
|
| 88 | + [sec2] |
| 89 | + a = 2 |
| 90 | + """ |
| 91 | + ).lstrip() |
| 92 | + |
| 93 | + def __init__(self): |
| 94 | + self.section = None |
| 95 | + |
| 96 | + def __call__(self, line): |
| 97 | + if line.startswith('[') and line.endswith(']'): |
| 98 | + # new section |
| 99 | + self.section = line.strip('[]') |
| 100 | + return |
| 101 | + return self.section |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def get_sections(cls, text): |
| 105 | + lines = filter(None, map(str.strip, text.splitlines())) |
| 106 | + return ( |
| 107 | + (section, map(cls.parse_value, values)) |
| 108 | + for section, values in itertools.groupby(lines, cls()) |
| 109 | + if section is not None |
| 110 | + ) |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def parse_value(line): |
| 114 | + return map(str.strip, line.split("=", 1)) |
| 115 | + |
| 116 | + |
63 | 117 | class EntryPoint( |
64 | 118 | PyPy_repr, collections.namedtuple('EntryPointBase', 'name value group') |
65 | 119 | ): |
@@ -118,22 +172,6 @@ def extras(self): |
118 | 172 | match = self.pattern.match(self.value) |
119 | 173 | return list(re.finditer(r'\w+', match.group('extras') or '')) |
120 | 174 |
|
121 | | - @classmethod |
122 | | - def _from_config(cls, config): |
123 | | - return ( |
124 | | - cls(name, value, group) |
125 | | - for group in config.sections() |
126 | | - for name, value in config.items(group) |
127 | | - ) |
128 | | - |
129 | | - @classmethod |
130 | | - def _from_text(cls, text): |
131 | | - config = ConfigParser(delimiters='=') |
132 | | - # case sensitive: https://stackoverflow.com/q/1611799/812183 |
133 | | - config.optionxform = str |
134 | | - config.read_string(text) |
135 | | - return cls._from_config(config) |
136 | | - |
137 | 175 | def _for(self, dist): |
138 | 176 | self.dist = dist |
139 | 177 | return self |
@@ -203,7 +241,19 @@ def groups(self): |
203 | 241 |
|
204 | 242 | @classmethod |
205 | 243 | def _from_text_for(cls, text, dist): |
206 | | - return cls(ep._for(dist) for ep in EntryPoint._from_text(text)) |
| 244 | + return cls(ep._for(dist) for ep in cls._from_text(text)) |
| 245 | + |
| 246 | + @classmethod |
| 247 | + def _from_text(cls, text): |
| 248 | + return itertools.starmap(EntryPoint, cls._parse_groups(text or '')) |
| 249 | + |
| 250 | + @staticmethod |
| 251 | + def _parse_groups(text): |
| 252 | + return ( |
| 253 | + (name, value, section) |
| 254 | + for section, values in Sectioned.get_sections(text) |
| 255 | + for name, value in values |
| 256 | + ) |
207 | 257 |
|
208 | 258 |
|
209 | 259 | def flake8_bypass(func): |
|
0 commit comments