@@ -52,7 +52,6 @@ def read(self, filenames):
52
52
except OSError :
53
53
return []
54
54
if tomllib is not None :
55
- toml_text = substitute_variables (toml_text , os .environ )
56
55
try :
57
56
self .data = tomllib .loads (toml_text )
58
57
except tomllib .TOMLDecodeError as err :
@@ -101,9 +100,16 @@ def _get(self, section, option):
101
100
if data is None :
102
101
raise configparser .NoSectionError (section )
103
102
try :
104
- return name , data [option ]
103
+ value = data [option ]
105
104
except KeyError as exc :
106
105
raise configparser .NoOptionError (option , name ) from exc
106
+ return name , value
107
+
108
+ def _get_simple (self , section , option ):
109
+ name , value = self ._get (section , option )
110
+ if isinstance (value , str ):
111
+ value = substitute_variables (value , os .environ )
112
+ return name , value
107
113
108
114
def has_option (self , section , option ):
109
115
_ , data = self ._get_section (section )
@@ -126,29 +132,40 @@ def get_section(self, section):
126
132
return data
127
133
128
134
def get (self , section , option ):
129
- _ , value = self ._get (section , option )
135
+ _ , value = self ._get_simple (section , option )
130
136
return value
131
137
132
- def _check_type (self , section , option , value , type_ , type_desc ):
133
- if not isinstance (value , type_ ):
134
- raise ValueError (
135
- 'Option {!r} in section {!r} is not {}: {!r}'
136
- .format (option , section , type_desc , value )
137
- )
138
+ def _check_type (self , section , option , value , type_ , converter , type_desc ):
139
+ if isinstance (value , type_ ):
140
+ return value
141
+ if isinstance (value , str ) and converter is not None :
142
+ try :
143
+ return converter (value )
144
+ except Exception :
145
+ raise ValueError (
146
+ f"Option [{ section } ]{ option } couldn't convert to { type_desc } : { value !r} "
147
+ ) from None
148
+ raise ValueError (
149
+ f"Option [{ section } ]{ option } is not { type_desc } : { value !r} "
150
+ )
138
151
139
152
def getboolean (self , section , option ):
140
- name , value = self ._get (section , option )
141
- self . _check_type ( name , option , value , bool , "a boolean" )
142
- return value
153
+ name , value = self ._get_simple (section , option )
154
+ bool_strings = { "true" : True , "false" : False }
155
+ return self . _check_type ( name , option , value , bool , bool_strings . __getitem__ , "a boolean" )
143
156
144
- def getlist (self , section , option ):
157
+ def _getlist (self , section , option ):
145
158
name , values = self ._get (section , option )
146
- self ._check_type (name , option , values , list , "a list" )
159
+ values = self ._check_type (name , option , values , list , None , "a list" )
160
+ values = [substitute_variables (value , os .environ ) for value in values ]
161
+ return name , values
162
+
163
+ def getlist (self , section , option ):
164
+ _ , values = self ._getlist (section , option )
147
165
return values
148
166
149
167
def getregexlist (self , section , option ):
150
- name , values = self ._get (section , option )
151
- self ._check_type (name , option , values , list , "a list" )
168
+ name , values = self ._getlist (section , option )
152
169
for value in values :
153
170
value = value .strip ()
154
171
try :
@@ -158,13 +175,11 @@ def getregexlist(self, section, option):
158
175
return values
159
176
160
177
def getint (self , section , option ):
161
- name , value = self ._get (section , option )
162
- self ._check_type (name , option , value , int , "an integer" )
163
- return value
178
+ name , value = self ._get_simple (section , option )
179
+ return self ._check_type (name , option , value , int , int , "an integer" )
164
180
165
181
def getfloat (self , section , option ):
166
- name , value = self ._get (section , option )
182
+ name , value = self ._get_simple (section , option )
167
183
if isinstance (value , int ):
168
184
value = float (value )
169
- self ._check_type (name , option , value , float , "a float" )
170
- return value
185
+ return self ._check_type (name , option , value , float , float , "a float" )
0 commit comments