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,17 +64,27 @@ 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, values = next(res)
69
- >>> sec
67
+ >>> for item in Sectioned.read(Sectioned._sample):
68
+ ... print(item)
69
+ Pair(name='sec1', value='# comments ignored')
70
+ Pair(name='sec1', value='a = 1')
71
+ Pair(name='sec1', value='b = 2')
72
+ Pair(name='sec2', value='a = 2')
73
+
74
+ >>> res = Sectioned.section_pairs(Sectioned._sample)
75
+ >>> item = next(res)
76
+ >>> item.name
70
77
'sec1'
71
- >>> [(key, value) for key, value in values]
72
- [('a', '1'), ('b', '2')]
73
- >>> sec, values = next(res)
74
- >>> sec
78
+ >>> item.value
79
+ Pair(name='a', value='1')
80
+ >>> item = next(res)
81
+ >>> item.value
82
+ Pair(name='b', value='2')
83
+ >>> item = next(res)
84
+ >>> item.name
75
85
'sec2'
76
- >>> [(key, value) for key, value in values]
77
- [( 'a', '2')]
86
+ >>> item. value
87
+ Pair(name= 'a', value= '2')
78
88
>>> list(res)
79
89
[]
80
90
"""
@@ -91,32 +101,28 @@ class Sectioned:
91
101
"""
92
102
).lstrip ()
93
103
94
- def __init__ (self ):
95
- self .section = None
96
-
97
- def __call__ (self , line ):
98
- if line .startswith ('[' ) and line .endswith (']' ):
99
- # new section
100
- self .section = line .strip ('[]' )
101
- return
102
- return self .section
103
-
104
104
@classmethod
105
- def get_sections (cls , text ):
106
- lines = filter (cls .valid , map (str .strip , text .splitlines ()))
105
+ def section_pairs (cls , text ):
107
106
return (
108
- ( section , map ( cls . parse_value , values ))
109
- for section , values in itertools . groupby ( lines , cls () )
110
- if section is not None
107
+ section . _replace ( value = Pair . parse ( section . value ))
108
+ for section in cls . read ( text , filter_ = cls . valid )
109
+ if section . name is not None
111
110
)
112
111
113
112
@staticmethod
114
- def valid (line ):
115
- return line and not line .startswith ('#' )
113
+ def read (text , filter_ = None ):
114
+ lines = filter (filter_ , map (str .strip , text .splitlines ()))
115
+ name = None
116
+ for value in lines :
117
+ section_match = value .startswith ('[' ) and value .endswith (']' )
118
+ if section_match :
119
+ name = value .strip ('[]' )
120
+ continue
121
+ yield Pair (name , value )
116
122
117
123
@staticmethod
118
- def parse_value (line ):
119
- return map ( str . strip , line .split ( "=" , 1 ) )
124
+ def valid (line ):
125
+ return line and not line .startswith ( '#' )
120
126
121
127
122
128
class EntryPoint (
@@ -255,9 +261,8 @@ def _from_text(cls, text):
255
261
@staticmethod
256
262
def _parse_groups (text ):
257
263
return (
258
- (name , value , section )
259
- for section , values in Sectioned .get_sections (text )
260
- for name , value in values
264
+ (item .value .name , item .value .value , item .name )
265
+ for item in Sectioned .section_pairs (text )
261
266
)
262
267
263
268
@@ -573,24 +578,7 @@ def _read_egg_info_reqs(self):
573
578
574
579
@classmethod
575
580
def _deps_from_requires_text (cls , source ):
576
- section_pairs = cls ._read_sections (source .splitlines ())
577
- sections = {
578
- section : list (map (operator .itemgetter ('line' ), results ))
579
- for section , results in itertools .groupby (
580
- section_pairs , operator .itemgetter ('section' )
581
- )
582
- }
583
- return cls ._convert_egg_info_reqs_to_simple_reqs (sections )
584
-
585
- @staticmethod
586
- def _read_sections (lines ):
587
- section = None
588
- for line in filter (None , lines ):
589
- section_match = re .match (r'\[(.*)\]$' , line )
590
- if section_match :
591
- section = section_match .group (1 )
592
- continue
593
- yield locals ()
581
+ return cls ._convert_egg_info_reqs_to_simple_reqs (Sectioned .read (source ))
594
582
595
583
@staticmethod
596
584
def _convert_egg_info_reqs_to_simple_reqs (sections ):
@@ -615,9 +603,8 @@ def parse_condition(section):
615
603
conditions = list (filter (None , [markers , make_condition (extra )]))
616
604
return '; ' + ' and ' .join (conditions ) if conditions else ''
617
605
618
- for section , deps in sections .items ():
619
- for dep in deps :
620
- yield dep + parse_condition (section )
606
+ for section in sections :
607
+ yield section .value + parse_condition (section .name )
621
608
622
609
623
610
class DistributionFinder (MetaPathFinder ):
0 commit comments