Skip to content

Commit 8815a2d

Browse files
committed
Here's an initial implementation for astropy#48. However, it's not quite working yet--when a config is written to a file I don't want it to save the value that was set by an environment variable. This could be implemented through the save_config function. But if a user calls the .write() method directly on the ConfigObj object it won't work correctly. I could be fine with that as long as we make sure the user always uses our API...
1 parent a280a31 commit 8815a2d

2 files changed

Lines changed: 122 additions & 63 deletions

File tree

‎astropy/config/configs.py‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"""
1111

1212
from __future__ import division
13-
from ..extern.configobj import configobj,validate
13+
14+
import os
15+
16+
from ..extern.configobj import configobj, validate
1417

1518
__all__ = ['ConfigurationItem','InvalidConfigurationItemWarning','get_config',
1619
'save_config','reload_config']
@@ -55,6 +58,12 @@ class ConfigurationItem(object):
5558
remaining items determine the section. If None, the package will be
5659
inferred from the package within whiich this object's initializer is
5760
called.
61+
envvar : str or None
62+
The name of an environment variable that may contain the value of this
63+
item. If the environment variable is set, its value will override this
64+
parameter's default value, as well as any value in the configuration
65+
file. The value in the environment variable is subject to the same type
66+
validation as values in the configuration file.
5867
5968
Raises
6069
------
@@ -87,7 +96,7 @@ class ConfigurationItem(object):
8796
_validator = validate.Validator()
8897

8998
def __init__(self, name, defaultvalue='', description=None, cfgtype=None,
90-
module=None):
99+
module=None, envvar=None):
91100
from warnings import warn
92101
from ..utils import find_current_module
93102

@@ -103,10 +112,11 @@ def __init__(self, name, defaultvalue='', description=None, cfgtype=None,
103112
self.name = name
104113
self.module = module
105114
self.description = description
115+
self.envvar = envvar
106116

107-
#now determine cfgtype if it is not given
117+
# now determine cfgtype if it is not given
108118
if cfgtype is None:
109-
if isinstance(defaultvalue,list):
119+
if isinstance(defaultvalue, list):
110120
#it is an options list
111121
dvstr = [str(v) for v in defaultvalue]
112122
cfgtype = 'option(' + ', '.join(dvstr) + ')'
@@ -141,7 +151,7 @@ def __init__(self, name, defaultvalue='', description=None, cfgtype=None,
141151
else:
142152
raise
143153

144-
def set(self,value):
154+
def set(self, value):
145155
""" Sets the current value of this `ConfigurationItem`.
146156
147157
This also updates the comments that give the description and type
@@ -167,7 +177,7 @@ def set(self,value):
167177
value = self._validate_val(value)
168178
except validate.ValidateError as e:
169179
msg = 'Provided value for configuration item {0} not valid: {1}'
170-
raise TypeError(msg.format(self.name,e.args[0]))
180+
raise TypeError(msg.format(self.name, e.args[0]))
171181

172182
sec = get_config(self.module)
173183

@@ -199,7 +209,13 @@ def save(self, value=None):
199209
"""
200210

201211
try:
202-
value = self() if value is None else self._validate_val(value)
212+
if value is None:
213+
if hasattr(self, '_orig_value'):
214+
value = self._orig_value
215+
else:
216+
value = self()
217+
else:
218+
self._validate_val(value)
203219
except validate.ValidateError as e:
204220
msg = 'Provided value for configuration item {0} not valid: {1}'
205221
raise TypeError(msg.format(self.name, e.args[0]))
@@ -247,7 +263,6 @@ def reload(self):
247263

248264
baseobj[self.name] = newobj[self.name]
249265

250-
251266
def __call__(self):
252267
""" Returns the value of this `ConfigurationItem`
253268
@@ -263,26 +278,36 @@ def __call__(self):
263278
If the configuration value as stored is not this item's type.
264279
"""
265280

266-
# get the value from the relevant `configobj.ConfigObj` object
281+
# get the value from the relevant `configobj.ConfigObj` object or from
282+
# an environment variable if set
267283
sec = get_config(self.module)
268-
if self.name not in sec:
269-
self.set(self.defaultvalue)
270-
val = sec[self.name]
271-
284+
if self.envvar is not None and self.envvar in os.environ:
285+
val = os.environ[self.envvar]
286+
if self.name in sec:
287+
self._orig_value = sec[self.name]
288+
self.set(val)
289+
else:
290+
if self.name not in sec:
291+
self.set(self.defaultvalue)
292+
val = sec[self.name]
272293
try:
273294
return self._validate_val(val)
274295
except validate.ValidateError as e:
275-
raise TypeError('Configuration value not valid:' + e.args[0])
296+
msg = 'Configuration value not valid:' + e.args[0]
297+
if self.envvar is not None:
298+
msg += '\nUsed value from environment variable ' + self.envvar
299+
raise TypeError(msg)
276300

277301
def _validate_val(self, val):
278302
""" Validates the provided value based on cfgtype and returns the
279303
type-cast value
280304
281305
throws the underlying configobj exception if it fails
282306
"""
283-
#note that this will normally use the *class* attribute `_validator`,
284-
#but if some arcane reason is needed for making a special one for an
285-
#instance or sub-class, it will be used
307+
308+
# note that this will normally use the *class* attribute `_validator`,
309+
# but if some arcane reason is needed for making a special one for an
310+
# instance or sub-class, it will be used
286311
return self._validator.check(self.cfgtype, val)
287312

288313
def _generate_comments(self):
@@ -323,16 +348,17 @@ def get_config(packageormod=None, reload=False):
323348
be determined.
324349
325350
"""
351+
326352
from os.path import join
327353
from .paths import get_config_dir
328354
from ..utils import find_current_module
329355

330356
if packageormod is None:
331357
packageormod = find_current_module(2)
332358
if packageormod is None:
333-
msg1 = 'Cannot automatically determine get_config module, '
334-
msg2 = 'because it is not called from inside a valid module'
335-
raise RuntimeError(msg1 + msg2)
359+
RuntimeError(
360+
'Cannot automatically determine get_config module, '
361+
'because it is not called from inside a valid module')
336362
else:
337363
packageormod = packageormod.__name__
338364

@@ -372,6 +398,7 @@ def save_config(packageormod=None):
372398
packageormod : str or None
373399
The package or module name - see `get_config` for details.
374400
"""
401+
375402
sec = get_config(packageormod)
376403
#look for the section that is its own parent - that's the base object
377404
while sec.parent is not sec:
Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
1-
# Licensed under a 3-clause BSD style license - see LICENSE.rst
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

33
def test_paths():
44
from ..paths import get_config_dir, get_cache_dir
5-
5+
66
assert 'astropy' in get_config_dir()
77
assert 'astropy' in get_cache_dir()
8-
8+
9+
910
def test_config_file():
1011
from ..configs import get_config,reload_config,save_config
1112
from os.path import exists
12-
13+
1314
apycfg = get_config('astropy')
1415
assert apycfg.filename.endswith('astropy.cfg')
15-
16+
1617
cfgsec = get_config('astropy.config')
17-
assert cfgsec.depth==1
18-
assert cfgsec.name=='config'
18+
assert cfgsec.depth == 1
19+
assert cfgsec.name == 'config'
1920
assert cfgsec.parent.filename.endswith('astropy.cfg')
20-
21+
2122
reload_config('astropy')
22-
23+
2324
#saving shouldn't change the file, because reload should have made sure it
2425
#is based on the current file. But don't do it if there's no file
2526
if exists(apycfg.filename):
2627
save_config('astropy')
2728

29+
2830
def test_configitem(tmpdir):
2931
from ..configs import ConfigurationItem,get_config
3032
from shutil import copy
31-
32-
ci = ConfigurationItem('tstnm',34,'this is a Description')
3333

34-
assert ci.module=='astropy.config.tests.test_configs'
35-
assert ci()==34
36-
assert ci.description=='this is a Description'
37-
34+
ci = ConfigurationItem('tstnm', 34, 'this is a Description')
35+
36+
assert ci.module == 'astropy.config.tests.test_configs'
37+
assert ci() == 34
38+
assert ci.description == 'this is a Description'
39+
3840
sec = get_config(ci.module)
3941
assert sec['tstnm'] == 34
4042
assert sec.comments['tstnm'][0] == 'this is a Description'
4143
assert sec.comments['tstnm'][1] == 'integer'
42-
44+
4345
ci.description = 'updated Descr'
4446
ci.set(32)
45-
assert ci()==32
47+
assert ci() == 32
4648
assert sec.comments['tstnm'][0] == 'updated Descr'
47-
49+
4850
#now try saving
4951
apycfg = sec
5052
while apycfg.parent is not apycfg:
@@ -54,92 +56,122 @@ def test_configitem(tmpdir):
5456
lns = f.read().split('\n')
5557
assert 'tstnm = 32' in lns
5658
assert '# updated Descr' in lns
57-
59+
5860
oldfn = apycfg.filename
5961
try:
6062
tmpdir.join('astropy.cfg').copy(tmpdir.join('astropy.cfg2'))
6163
apycfg.filename = str(tmpdir.join('astropy.cfg2').realpath())
62-
64+
6365
ci.set(30)
6466
ci.save()
65-
67+
6668
with open(str(apycfg.filename)) as f:
6769
lns = f.readlines()
6870
assert '[config.tests.test_configs]\n' in lns
6971
assert 'tstnm = 30\n' in lns
70-
72+
7173
ci.save(31)
72-
74+
7375
with open(str(apycfg.filename)) as f:
7476
lns = f.readlines()
7577
assert '[config.tests.test_configs]\n' in lns
7678
assert 'tstnm = 31\n' in lns
77-
79+
7880
#also try to save one that doesn't yet exist
7981
apycfg.filename = str(tmpdir.join('astropy.cfg3').realpath())
8082
ci.save()
81-
83+
8284
with open(str(apycfg.filename)) as f:
8385
lns = f.readlines()
8486
assert '[config.tests.test_configs]\n' in lns
8587
assert 'tstnm = 30\n' in lns
86-
88+
8789
finally:
8890
apycfg.filename = oldfn
89-
91+
9092
#also try to save one that doesn't yet exist
91-
93+
9294
def test_configitem_types():
9395
from ..configs import ConfigurationItem
9496
from pytest import raises
95-
97+
9698
ci1 = ConfigurationItem('tstnm1',34)
9799
assert isinstance(ci1(),int)
98-
100+
99101
ci2 = ConfigurationItem('tstnm2',34.3)
100102
assert isinstance(ci2(),float)
101-
103+
102104
ci3 = ConfigurationItem('tstnm3',True)
103105
assert isinstance(ci3(),bool)
104-
106+
105107
ci4 = ConfigurationItem('tstnm4','astring')
106108
assert isinstance(ci4(),str)
107-
109+
108110
with raises(TypeError):
109111
ci1.set(34.3)
110112
ci2.set(12) #this would should succeed as up-casting
111113
with raises(TypeError):
112114
ci3.set('fasd')
113115
with raises(TypeError):
114116
ci4.set(546.245)
115-
116-
117+
118+
117119
def test_configitem_options(tmpdir):
118-
from ..configs import ConfigurationItem,get_config
120+
from ..configs import ConfigurationItem, get_config
119121
from pytest import raises
120-
122+
121123
cio = ConfigurationItem('tstnmo',['op1','op2','op3'])
122124
sec = get_config(cio.module)
123-
125+
124126
assert isinstance(cio(),str)
125127
assert cio() == 'op1'
126128
assert sec['tstnmo'] == 'op1'
127-
128-
129+
130+
129131
cio.set('op2')
130132
with raises(TypeError):
131133
cio.set('op5')
132134
assert sec['tstnmo'] == 'op2'
133-
135+
134136
#now try saving
135137
apycfg = sec
136138
while apycfg.parent is not apycfg:
137139
apycfg = apycfg.parent
138140
f = tmpdir.join('astropy.cfg')
139141
apycfg.write(f)
140142
lns = f.read().split('\n')
141-
143+
142144
assert '# option(op1, op2, op3)' in lns
143145
assert 'tstnmo = op2' in lns
144146

145-
147+
148+
def test_configitem_envvar(tmpdir):
149+
import os
150+
from ..configs import ConfigurationItem, get_config
151+
152+
cio = ConfigurationItem('testnm5', defaultvalue=12345,
153+
envvar='ASTROPY_TESTNM5')
154+
155+
os.environ['ASTROPY_TESTNM5'] = '54321'
156+
section = get_config(cio.module)
157+
158+
assert isinstance(cio(), int)
159+
assert cio() == 54321
160+
assert section['testnm5'] == 54321
161+
162+
# Test that saving does *not* save the value from the environment variable
163+
apycfg = section
164+
while apycfg.parent is not apycfg:
165+
apycfg = apycfg.parent
166+
f = tmpdir.join('astropy.cfg')
167+
apycfg.write(f)
168+
lns = f.read().split('\n')
169+
170+
assert 'testnm5 = 12345' in lns
171+
# But the existing value hasn't changed, right?
172+
assert cio() == 54321
173+
174+
# Make sure an error is raised if an environment variable is invalid
175+
os.environ['ASTROPY_TESTNM5'] = 'abcde'
176+
with raises(TypeError):
177+
cio()

0 commit comments

Comments
 (0)