Changeset 2920 for trunk/packages/invirt-base/python/invirt
- Timestamp:
- Jan 22, 2010, 9:45:09 AM (15 years ago)
- Location:
- trunk/packages/invirt-base/python/invirt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/invirt-base/python/invirt/common.py
r2592 r2920 11 11 class struct(dict): 12 12 'A simple namespace object.' 13 def __init__(self, d = {}, __prefix = None, **kwargs):13 def __init__(self, d = {}, __prefix = None, __default=None, **kwargs): 14 14 super(struct, self).__init__(d) 15 15 self.__prefix = __prefix 16 self.__default = __default 16 17 self.update(kwargs) 17 18 def __getattr__(self, key): … … 19 20 return self[key] 20 21 except KeyError: 21 # XX ideally these would point a frame higher on the stack. 22 prefix = self.__prefix 23 if prefix is not None: 24 raise InvirtConfigError('missing configuration variable %s%s' 25 % (prefix, key)) 22 if self.__default is None: 23 # XX ideally these would point a frame higher on the stack. 24 prefix = self.__prefix 25 if prefix is not None: 26 raise InvirtConfigError('missing configuration variable ' 27 '%s%s' % (prefix, key)) 28 else: 29 raise AttributeError("anonymous struct has no member '%s'" 30 % (key,)) 26 31 else: 27 raise AttributeError("anonymous struct has no member '%s'" 28 % (key,)) 32 return struct({}, '', self.__default) 29 33 30 def dicts2struct(x, prefix = None ):34 def dicts2struct(x, prefix = None, default = None): 31 35 """ 32 36 Given a tree of lists/dicts, perform a deep traversal to transform all the … … 38 42 def newprefix(k): return prefix 39 43 if type(x) == dict: 40 return struct(((k, dicts2struct(v, newprefix(k) ))44 return struct(((k, dicts2struct(v, newprefix(k), default)) 41 45 for k,v in x.iteritems()), 42 prefix) 46 prefix, 47 default) 43 48 elif type(x) == list: 44 return [dicts2struct(v, newprefix(i)) for i, v in enumerate(x)] 49 return [dicts2struct(v, newprefix(i), default) 50 for i, v in enumerate(x)] 45 51 elif x is None: 46 return struct({}, prefix )52 return struct({}, prefix, default) 47 53 else: 48 54 return x -
trunk/packages/invirt-base/python/invirt/config.py
r1934 r2920 142 142 dicts = load() 143 143 structs = dicts2struct(dicts, '') 144 safestructs = dicts2struct(dicts, '', '') 144 145 145 146 # vim:et:sw=4:ts=4
Note: See TracChangeset
for help on using the changeset viewer.