@@ -15,6 +15,7 @@ import sys
15
15
import threading
16
16
import types
17
17
import warnings
18
+ from configparser import ConfigParser as IniConfigParser
18
19
19
20
try :
20
21
import contextvars
41
42
except ImportError :
42
43
_is_coroutine = True
43
44
44
- try :
45
- import ConfigParser as iniconfigparser
46
- except ImportError :
47
- import configparser as iniconfigparser
48
-
49
45
try :
50
46
import yaml
51
47
except ImportError :
@@ -102,7 +98,7 @@ config_env_marker_pattern = re.compile(
102
98
r " \$ {( ?P<name> [^ }^{: ]+ ) ( ?P<separator> :? ) ( ?P<default> . *? ) }" ,
103
99
)
104
100
105
- def _resolve_config_env_markers (config_content , envs_required = False ):
101
+ cdef str _resolve_config_env_markers(config_content: str , envs_required: bool ):
106
102
""" Replace environment variable markers with their values."""
107
103
findings = list (config_env_marker_pattern.finditer(config_content))
108
104
@@ -121,28 +117,19 @@ def _resolve_config_env_markers(config_content, envs_required=False):
121
117
return config_content
122
118
123
119
124
- if sys.version_info[0 ] == 3 :
125
- def _parse_ini_file (filepath , envs_required = False ):
126
- parser = iniconfigparser.ConfigParser()
127
- with open (filepath) as config_file:
128
- config_string = _resolve_config_env_markers(
129
- config_file.read(),
130
- envs_required = envs_required,
131
- )
132
- parser.read_string(config_string)
133
- return parser
134
- else :
135
- import StringIO
120
+ cdef object _parse_ini_file(filepath, envs_required: bool | None ):
121
+ parser = IniConfigParser()
122
+
123
+ with open (filepath) as config_file:
124
+ config_string = config_file.read()
136
125
137
- def _parse_ini_file (filepath , envs_required = False ):
138
- parser = iniconfigparser.ConfigParser()
139
- with open (filepath) as config_file:
126
+ if envs_required is not None :
140
127
config_string = _resolve_config_env_markers(
141
- config_file.read() ,
128
+ config_string ,
142
129
envs_required = envs_required,
143
130
)
144
- parser.readfp(StringIO.StringIO( config_string) )
145
- return parser
131
+ parser.read_string( config_string)
132
+ return parser
146
133
147
134
148
135
if yaml:
@@ -1717,7 +1704,7 @@ cdef class ConfigurationOption(Provider):
1717
1704
try :
1718
1705
parser = _parse_ini_file(
1719
1706
filepath,
1720
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1707
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1721
1708
)
1722
1709
except IOError as exception:
1723
1710
if required is not False \
@@ -1776,10 +1763,11 @@ cdef class ConfigurationOption(Provider):
1776
1763
raise
1777
1764
return
1778
1765
1779
- config_content = _resolve_config_env_markers(
1780
- config_content,
1781
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1782
- )
1766
+ if envs_required is not None :
1767
+ config_content = _resolve_config_env_markers(
1768
+ config_content,
1769
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1770
+ )
1783
1771
config = yaml.load(config_content, loader)
1784
1772
1785
1773
current_config = self .__call__ ()
@@ -1814,10 +1802,11 @@ cdef class ConfigurationOption(Provider):
1814
1802
raise
1815
1803
return
1816
1804
1817
- config_content = _resolve_config_env_markers(
1818
- config_content,
1819
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1820
- )
1805
+ if envs_required is not None :
1806
+ config_content = _resolve_config_env_markers(
1807
+ config_content,
1808
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
1809
+ )
1821
1810
config = json.loads(config_content)
1822
1811
1823
1812
current_config = self .__call__ ()
@@ -2270,7 +2259,7 @@ cdef class Configuration(Object):
2270
2259
try :
2271
2260
parser = _parse_ini_file(
2272
2261
filepath,
2273
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2262
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2274
2263
)
2275
2264
except IOError as exception:
2276
2265
if required is not False \
@@ -2329,10 +2318,11 @@ cdef class Configuration(Object):
2329
2318
raise
2330
2319
return
2331
2320
2332
- config_content = _resolve_config_env_markers(
2333
- config_content,
2334
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2335
- )
2321
+ if envs_required is not None :
2322
+ config_content = _resolve_config_env_markers(
2323
+ config_content,
2324
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2325
+ )
2336
2326
config = yaml.load(config_content, loader)
2337
2327
2338
2328
current_config = self .__call__ ()
@@ -2367,10 +2357,11 @@ cdef class Configuration(Object):
2367
2357
raise
2368
2358
return
2369
2359
2370
- config_content = _resolve_config_env_markers(
2371
- config_content,
2372
- envs_required = envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2373
- )
2360
+ if envs_required is not None :
2361
+ config_content = _resolve_config_env_markers(
2362
+ config_content,
2363
+ envs_required if envs_required is not UNDEFINED else self ._is_strict_mode_enabled(),
2364
+ )
2374
2365
config = json.loads(config_content)
2375
2366
2376
2367
current_config = self .__call__ ()
0 commit comments