diff options
author | Isis Lovecruft <isis@torproject.org> | 2013-01-31 01:50:02 +0000 |
---|---|---|
committer | Isis Lovecruft <isis@torproject.org> | 2013-01-31 01:50:02 +0000 |
commit | 9a338dfb7ada40bb13c1e3105ffec62b08a2eaf5 (patch) | |
tree | a3ca6a7191f8b6f3e3d9ab7fb2c02e81a510e434 /src/leap | |
parent | 1bed2439923405998725abbfc4527433822bcf0e (diff) |
Add storage dictionary class (for config files) to mx/util/__init__.py.
Diffstat (limited to 'src/leap')
-rw-r--r-- | src/leap/util/__init__.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/leap/util/__init__.py b/src/leap/util/__init__.py index e69de29..35e1297 100644 --- a/src/leap/util/__init__.py +++ b/src/leap/util/__init__.py @@ -0,0 +1,50 @@ +# -*- encoding: utf-8 -*- +""" + mx/util/__init__.py + ------------------- + Initialization file for leap_mx utilities. Some miscellaneous things are + stored here also. +""" + + +class Storage(dict): + """ + A Storage object is like a dictionary except `obj.foo` can be used + in addition to `obj['foo']`. + + >>> o = Storage(a=1) + >>> o.a + 1 + >>> o['a'] + 1 + >>> o.a = 2 + >>> o['a'] + 2 + >>> del o.a + >>> o.a + None + """ + def __getattr__(self, key): + try: + return self[key] + except KeyError, k: + return None + + def __setattr__(self, key, value): + self[key] = value + + def __delattr__(self, key): + try: + del self[key] + except KeyError, k: + raise AttributeError, k + + def __repr__(self): + return '<Storage ' + dict.__repr__(self) + '>' + + def __getstate__(self): + return dict(self) + + def __setstate__(self, value): + for (k, v) in value.items(): + self[k] = v |