home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __all__ = [
- 'Struct']
- import types
- import pprint
- from IPython.genutils import list2dict2
-
- class Struct:
- __protected = 'copy dict dictcopy get has_attr has_key items keys merge popitem setdefault update values __make_dict __dict_invert '.split()
-
- def __init__(self, dict = None, **kw):
- self.__dict__['__allownew'] = True
- if dict is None:
- dict = { }
-
- if isinstance(dict, Struct):
- dict = dict.dict()
- elif dict and type(dict) is not types.DictType:
- raise TypeError, 'Initialize with a dictionary or key=val pairs.'
-
- dict.update(kw)
- for k, v in dict.items():
- self[k] = v
-
-
-
- def __setitem__(self, key, value):
- if key in Struct._Struct__protected:
- raise KeyError, 'Key ' + `key` + ' is a protected key of class Struct.'
- key in Struct._Struct__protected
- if not self['__allownew'] and key not in self.__dict__:
- raise KeyError("Can't create unknown attribute %s - Check for typos, or use allow_new_attr to create new attributes!" % key)
- key not in self.__dict__
- self.__dict__[key] = value
-
-
- def __setattr__(self, key, value):
- self.__setitem__(key, value)
-
-
- def __str__(self):
- return 'Struct(' + pprint.pformat(self.__dict__) + ')'
-
-
- def __repr__(self):
- return self.__str__()
-
-
- def __getitem__(self, key):
- return self.__dict__[key]
-
-
- def __contains__(self, key):
- return key in self.__dict__
-
-
- def __iadd__(self, other):
- self.merge(other)
- return self
-
-
- def __add__(self, other):
- Sout = self.copy()
- Sout.merge(other)
- return Sout
-
-
- def __sub__(self, other):
- Sout = self.copy()
- Sout -= other
- return Sout
-
-
- def __isub__(self, other):
- for k in other.keys():
- if self.has_key(k):
- del self.__dict__[k]
- continue
-
-
-
- def __make_dict(self, __loc_data__, **kw):
- if __loc_data__ == None:
- dict = { }
- elif type(__loc_data__) is types.DictType:
- dict = __loc_data__
- elif isinstance(__loc_data__, Struct):
- dict = __loc_data__.__dict__
- else:
- raise TypeError, 'Update with a dict, a Struct or key=val pairs.'
- if __loc_data__ == None:
- dict.update(kw)
-
- return dict
-
-
- def __dict_invert(self, dict):
- outdict = { }
- for k, lst in dict.items():
- if type(lst) is types.StringType:
- lst = lst.split()
-
- for entry in lst:
- outdict[entry] = k
-
-
- return outdict
-
-
- def clear(self):
- self.__dict__.clear()
-
-
- def copy(self):
- return Struct(self.__dict__.copy())
-
-
- def dict(self):
- return self.__dict__
-
-
- def dictcopy(self):
- return self.__dict__.copy()
-
-
- def popitem(self):
- return self.__dict__.popitem()
-
-
- def update(self, __loc_data__ = None, **kw):
- newdict = Struct._Struct__make_dict(self, __loc_data__, **kw)
- for k, v in newdict.iteritems():
- self[k] = v
-
-
-
- def merge(self, __loc_data__ = None, _Struct__conflict_solve = None, **kw):
- data_dict = Struct._Struct__make_dict(self, __loc_data__, **kw)
-
- preserve = lambda old, new: old
-
- update = lambda old, new: new
-
- add = lambda old, new: old + new
-
- add_flip = lambda old, new: new + old
-
- add_s = lambda old, new: old + ' ' + new
- conflict_solve = list2dict2(self.keys(), default = preserve)
- if _Struct__conflict_solve:
- inv_conflict_solve_user = _Struct__conflict_solve.copy()
- for name, func in [
- ('preserve', preserve),
- ('update', update),
- ('add', add),
- ('add_flip', add_flip),
- ('add_s', add_s)]:
- if name in inv_conflict_solve_user.keys():
- inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
- del inv_conflict_solve_user[name]
- continue
-
- conflict_solve.update(Struct._Struct__dict_invert(self, inv_conflict_solve_user))
-
- for key in data_dict:
- if key not in self:
- self[key] = data_dict[key]
- continue
- self[key] = conflict_solve[key](self[key], data_dict[key])
-
-
-
- def has_key(self, key):
- return self.__dict__.has_key(key)
-
-
- def hasattr(self, key):
- return self.__dict__.has_key(key)
-
-
- def items(self):
- return self.__dict__.items()
-
-
- def keys(self):
- return self.__dict__.keys()
-
-
- def values(self, keys = None):
- if not keys:
- return self.__dict__.values()
- ret = []
- for k in keys:
- ret.append(self[k])
-
- return ret
-
-
- def get(self, attr, val = None):
-
- try:
- return self[attr]
- except KeyError:
- return val
-
-
-
- def setdefault(self, attr, val = None):
- if not self.has_key(attr):
- self[attr] = val
-
- return self.get(attr, val)
-
-
- def allow_new_attr(self, allow = True):
- self['__allownew'] = allow
-
-
-