Skip to content

Commit

Permalink
Merge pull request #95 from michaelbynum/immutable-config
Browse files Browse the repository at this point in the history
Adding an ImmutableConfigValue class
  • Loading branch information
jsiirola committed Apr 9, 2020
2 parents d74cd96 + 9cc57f0 commit 6f02a99
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
57 changes: 57 additions & 0 deletions pyutilib/misc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,63 @@ def _data_collector(self, level, prefix, visibility=None, docMode=False):
yield (level, prefix, self, self)


class ImmutableConfigValue(ConfigValue):
def set_value(self, value):
if self._cast(value) != self._data:
raise RuntimeError(str(self) + ' is currently immutable')
super(ImmutableConfigValue, self).set_value(value)

def reset(self):
try:
super(ImmutableConfigValue, self).set_value(self._default)
except:
if hasattr(self._default, '__call__'):
super(ImmutableConfigValue, self).set_value(self._default())
else:
raise
self._userAccessed = False
self._userSet = False


class MarkImmutable(object):
"""
Mark instances of ConfigValue as immutable.
Parameters
----------
config_value: ConfigValue
The ConfigValue instances that should be marked immutable.
Note that multiple instances of ConfigValue can be passed.
Examples
--------
>>> config = ConfigBlock()
>>> config.declare('a', ConfigValue(default=1, domain=int))
>>> config.declare('b', ConfigValue(default=1, domain=int))
>>> locker = MarkImmutable(config.get('a'), config.get('b'))
Now, config.a and config.b cannot be changed. To make them mutable again,
>>> locker.release_lock()
"""
def __init__(self, *args):
self._locked = list()
try:
for arg in args:
if type(arg) is not ConfigValue:
raise ValueError('Only ConfigValue instances can be marked immutable.')
arg.__class__ = ImmutableConfigValue
self._locked.append(arg)
except:
self.release_lock()
raise

def release_lock(self):
for arg in self._locked:
arg.__class__ = ConfigValue
self._locked = list()


class ConfigList(ConfigBase):
"""Store and manipulate a list of configuration values.
Expand Down
45 changes: 44 additions & 1 deletion pyutilib/misc/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pyutilib.th as unittest

import pyutilib.misc.comparison
from pyutilib.misc.config import ConfigValue, ConfigBlock, ConfigList
from pyutilib.misc.config import ConfigValue, ConfigBlock, ConfigList, MarkImmutable

from six import PY3, StringIO

Expand All @@ -39,6 +39,49 @@ def _display(obj, *args):
obj.display(ostream=test, *args)
return test.getvalue()


class TestImmutableConfigValue(unittest.TestCase):
def test_immutable_config_value(self):
config = ConfigBlock()
config.declare('a', ConfigValue(default=1, domain=int))
config.declare('b', ConfigValue(default=1, domain=int))
config.a = 2
config.b = 3
self.assertEqual(config.a, 2)
self.assertEqual(config.b, 3)
locker = MarkImmutable(config.get('a'), config.get('b'))
with self.assertRaises(Exception):
config.a = 4
with self.assertRaises(Exception):
config.b = 5
config.a = 2
config.b = 3
self.assertEqual(config.a, 2)
self.assertEqual(config.b, 3)
locker.release_lock()
config.a = 4
config.b = 5
self.assertEqual(config.a, 4)
self.assertEqual(config.b, 5)
with self.assertRaises(ValueError):
locker = MarkImmutable(config.get('a'), config.b)
self.assertEqual(type(config.get('a')), ConfigValue)
config.a = 6
self.assertEqual(config.a, 6)

config.declare('c', ConfigValue(default=-1, domain=int))
locker = MarkImmutable(config.get('a'), config.get('b'))
config2 = config({'c': -2})
self.assertEqual(config2.a, 6)
self.assertEqual(config2.b, 5)
self.assertEqual(config2.c, -2)
with self.assertRaises(Exception):
config3 = config({'a': 1})
locker.release_lock()
config3 = config({'a': 1})
self.assertEqual(config3.a, 1)


class Test(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 6f02a99

Please sign in to comment.