15
15
import contextlib
16
16
import collections
17
17
18
- from ._collections import FreezableDefaultDict
18
+ from ._collections import FreezableDefaultDict , Pair
19
19
from ._compat import (
20
20
NullFinder ,
21
21
Protocol ,
@@ -64,26 +64,24 @@ class Sectioned:
64
64
"""
65
65
A simple entry point config parser for performance
66
66
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
70
70
'sec1'
71
- >>> tuple(pair )
71
+ >>> tuple(item.value )
72
72
('a', '1')
73
- >>> sec, pair = next(res)
74
- >>> tuple(pair )
73
+ >>> item = next(res)
74
+ >>> tuple(item.value )
75
75
('b', '2')
76
- >>> sec, pair = next(res)
77
- >>> sec
76
+ >>> item = next(res)
77
+ >>> item.name
78
78
'sec2'
79
- >>> tuple(pair )
79
+ >>> tuple(item.value )
80
80
('a', '2')
81
81
>>> list(res)
82
82
[]
83
83
"""
84
84
85
- Pair = collections .namedtuple ('Pair' , 'name value' )
86
-
87
85
_sample = textwrap .dedent (
88
86
"""
89
87
[sec1]
@@ -97,9 +95,9 @@ class Sectioned:
97
95
).lstrip ()
98
96
99
97
@classmethod
100
- def get_sections (cls , text ):
98
+ def section_pairs (cls , text ):
101
99
return (
102
- ( section .name , cls . parse_value (section .value ))
100
+ section ._replace ( value = Pair . parse (section .value ))
103
101
for section in cls .read (text , filter_ = cls .valid )
104
102
if section .name is not None
105
103
)
@@ -113,16 +111,12 @@ def read(text, filter_=None):
113
111
if section_match :
114
112
name = value .strip ('[]' )
115
113
continue
116
- yield Sectioned . Pair (name , value )
114
+ yield Pair (name , value )
117
115
118
116
@staticmethod
119
117
def valid (line ):
120
118
return line and not line .startswith ('#' )
121
119
122
- @staticmethod
123
- def parse_value (line ):
124
- return map (str .strip , line .split ("=" , 1 ))
125
-
126
120
127
121
class EntryPoint (
128
122
PyPy_repr , collections .namedtuple ('EntryPointBase' , 'name value group' )
@@ -260,8 +254,8 @@ def _from_text(cls, text):
260
254
@staticmethod
261
255
def _parse_groups (text ):
262
256
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 )
265
259
)
266
260
267
261
0 commit comments