Add editable() method to registry.Json.

This commit is contained in:
Valentin Lorentz 2012-12-08 21:13:54 +01:00
parent d8bd1f6f92
commit d9f7e0e818
2 changed files with 24 additions and 0 deletions

View File

@ -686,4 +686,18 @@ class Json(String):
def __call__(self):
return json.loads(super(Json, self).__call__())
class _Context:
def __init__(self, var):
self._var = var
def __enter__(self):
self._dict = self._var()
return self._dict
def __exit__(self, *args):
self._var.setValue(self._dict)
def editable(self):
"""Return an editable dict usable within a 'with' statement and
committed to the configuration variable at the end."""
return self._Context(self)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -138,6 +138,16 @@ class ValuesTestCase(SupyTestCase):
self.assertEqual(v(), data)
self.assertIsNot(v(), data)
with v.editable() as dict_:
dict_['supy'] = 'bot'
del dict_['qux']
self.assertNotIn('supy', v())
self.assertIn('qux', v())
self.assertIn('supy', v())
self.assertEqual(v()['supy'], 'bot')
self.assertIsNot(v()['supy'], 'bot')
self.assertNotIn('qux', v())
def testNormalizedString(self):
v = registry.NormalizedString("""foo
bar baz