Skip to content

Commit bf6a3b1

Browse files
committed
Use Pair in other places and extract it to _collections.
1 parent 5806de1 commit bf6a3b1

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

importlib_metadata/__init__.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import contextlib
1616
import collections
1717

18-
from ._collections import FreezableDefaultDict
18+
from ._collections import FreezableDefaultDict, Pair
1919
from ._compat import (
2020
NullFinder,
2121
Protocol,
@@ -64,26 +64,24 @@ class Sectioned:
6464
"""
6565
A simple entry point config parser for performance
6666
67-
>>> res = Sectioned.get_sections(Sectioned._sample)
68-
>>> sec, pair = next(res)
69-
>>> sec
67+
>>> res = Sectioned.section_pairs(Sectioned._sample)
68+
>>> item = next(res)
69+
>>> item.name
7070
'sec1'
71-
>>> tuple(pair)
71+
>>> tuple(item.value)
7272
('a', '1')
73-
>>> sec, pair = next(res)
74-
>>> tuple(pair)
73+
>>> item = next(res)
74+
>>> tuple(item.value)
7575
('b', '2')
76-
>>> sec, pair = next(res)
77-
>>> sec
76+
>>> item = next(res)
77+
>>> item.name
7878
'sec2'
79-
>>> tuple(pair)
79+
>>> tuple(item.value)
8080
('a', '2')
8181
>>> list(res)
8282
[]
8383
"""
8484

85-
Pair = collections.namedtuple('Pair', 'name value')
86-
8785
_sample = textwrap.dedent(
8886
"""
8987
[sec1]
@@ -97,9 +95,9 @@ class Sectioned:
9795
).lstrip()
9896

9997
@classmethod
100-
def get_sections(cls, text):
98+
def section_pairs(cls, text):
10199
return (
102-
(section.name, cls.parse_value(section.value))
100+
section._replace(value=Pair.parse(section.value))
103101
for section in cls.read(text, filter_=cls.valid)
104102
if section.name is not None
105103
)
@@ -113,16 +111,12 @@ def read(text, filter_=None):
113111
if section_match:
114112
name = value.strip('[]')
115113
continue
116-
yield Sectioned.Pair(name, value)
114+
yield Pair(name, value)
117115

118116
@staticmethod
119117
def valid(line):
120118
return line and not line.startswith('#')
121119

122-
@staticmethod
123-
def parse_value(line):
124-
return map(str.strip, line.split("=", 1))
125-
126120

127121
class EntryPoint(
128122
PyPy_repr, collections.namedtuple('EntryPointBase', 'name value group')
@@ -260,8 +254,8 @@ def _from_text(cls, text):
260254
@staticmethod
261255
def _parse_groups(text):
262256
return (
263-
(name, value, section)
264-
for section, (name, value) in Sectioned.get_sections(text)
257+
(item.value.name, item.value.value, item.name)
258+
for item in Sectioned.section_pairs(text)
265259
)
266260

267261

importlib_metadata/_collections.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ def __missing__(self, key):
2222

2323
def freeze(self):
2424
self._frozen = lambda key: self.default_factory()
25+
26+
27+
class Pair(collections.namedtuple('Pair', 'name value')):
28+
@classmethod
29+
def parse(cls, text):
30+
return cls(*map(str.strip, text.split("=", 1)))

0 commit comments

Comments
 (0)