home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1830 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.6 KB  |  221 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Struct']
  6. import types
  7. import pprint
  8. from IPython.genutils import list2dict2
  9.  
  10. class Struct:
  11.     __protected = 'copy dict dictcopy get has_attr has_key items keys merge popitem setdefault update values __make_dict __dict_invert '.split()
  12.     
  13.     def __init__(self, dict = None, **kw):
  14.         self.__dict__['__allownew'] = True
  15.         if dict is None:
  16.             dict = { }
  17.         
  18.         if isinstance(dict, Struct):
  19.             dict = dict.dict()
  20.         elif dict and type(dict) is not types.DictType:
  21.             raise TypeError, 'Initialize with a dictionary or key=val pairs.'
  22.         
  23.         dict.update(kw)
  24.         for k, v in dict.items():
  25.             self[k] = v
  26.         
  27.  
  28.     
  29.     def __setitem__(self, key, value):
  30.         if key in Struct._Struct__protected:
  31.             raise KeyError, 'Key ' + `key` + ' is a protected key of class Struct.'
  32.         key in Struct._Struct__protected
  33.         if not self['__allownew'] and key not in self.__dict__:
  34.             raise KeyError("Can't create unknown attribute %s - Check for typos, or use allow_new_attr to create new attributes!" % key)
  35.         key not in self.__dict__
  36.         self.__dict__[key] = value
  37.  
  38.     
  39.     def __setattr__(self, key, value):
  40.         self.__setitem__(key, value)
  41.  
  42.     
  43.     def __str__(self):
  44.         return 'Struct(' + pprint.pformat(self.__dict__) + ')'
  45.  
  46.     
  47.     def __repr__(self):
  48.         return self.__str__()
  49.  
  50.     
  51.     def __getitem__(self, key):
  52.         return self.__dict__[key]
  53.  
  54.     
  55.     def __contains__(self, key):
  56.         return key in self.__dict__
  57.  
  58.     
  59.     def __iadd__(self, other):
  60.         self.merge(other)
  61.         return self
  62.  
  63.     
  64.     def __add__(self, other):
  65.         Sout = self.copy()
  66.         Sout.merge(other)
  67.         return Sout
  68.  
  69.     
  70.     def __sub__(self, other):
  71.         Sout = self.copy()
  72.         Sout -= other
  73.         return Sout
  74.  
  75.     
  76.     def __isub__(self, other):
  77.         for k in other.keys():
  78.             if self.has_key(k):
  79.                 del self.__dict__[k]
  80.                 continue
  81.         
  82.  
  83.     
  84.     def __make_dict(self, __loc_data__, **kw):
  85.         if __loc_data__ == None:
  86.             dict = { }
  87.         elif type(__loc_data__) is types.DictType:
  88.             dict = __loc_data__
  89.         elif isinstance(__loc_data__, Struct):
  90.             dict = __loc_data__.__dict__
  91.         else:
  92.             raise TypeError, 'Update with a dict, a Struct or key=val pairs.'
  93.         if __loc_data__ == None:
  94.             dict.update(kw)
  95.         
  96.         return dict
  97.  
  98.     
  99.     def __dict_invert(self, dict):
  100.         outdict = { }
  101.         for k, lst in dict.items():
  102.             if type(lst) is types.StringType:
  103.                 lst = lst.split()
  104.             
  105.             for entry in lst:
  106.                 outdict[entry] = k
  107.             
  108.         
  109.         return outdict
  110.  
  111.     
  112.     def clear(self):
  113.         self.__dict__.clear()
  114.  
  115.     
  116.     def copy(self):
  117.         return Struct(self.__dict__.copy())
  118.  
  119.     
  120.     def dict(self):
  121.         return self.__dict__
  122.  
  123.     
  124.     def dictcopy(self):
  125.         return self.__dict__.copy()
  126.  
  127.     
  128.     def popitem(self):
  129.         return self.__dict__.popitem()
  130.  
  131.     
  132.     def update(self, __loc_data__ = None, **kw):
  133.         newdict = Struct._Struct__make_dict(self, __loc_data__, **kw)
  134.         for k, v in newdict.iteritems():
  135.             self[k] = v
  136.         
  137.  
  138.     
  139.     def merge(self, __loc_data__ = None, _Struct__conflict_solve = None, **kw):
  140.         data_dict = Struct._Struct__make_dict(self, __loc_data__, **kw)
  141.         
  142.         preserve = lambda old, new: old
  143.         
  144.         update = lambda old, new: new
  145.         
  146.         add = lambda old, new: old + new
  147.         
  148.         add_flip = lambda old, new: new + old
  149.         
  150.         add_s = lambda old, new: old + ' ' + new
  151.         conflict_solve = list2dict2(self.keys(), default = preserve)
  152.         if _Struct__conflict_solve:
  153.             inv_conflict_solve_user = _Struct__conflict_solve.copy()
  154.             for name, func in [
  155.                 ('preserve', preserve),
  156.                 ('update', update),
  157.                 ('add', add),
  158.                 ('add_flip', add_flip),
  159.                 ('add_s', add_s)]:
  160.                 if name in inv_conflict_solve_user.keys():
  161.                     inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
  162.                     del inv_conflict_solve_user[name]
  163.                     continue
  164.             
  165.             conflict_solve.update(Struct._Struct__dict_invert(self, inv_conflict_solve_user))
  166.         
  167.         for key in data_dict:
  168.             if key not in self:
  169.                 self[key] = data_dict[key]
  170.                 continue
  171.             self[key] = conflict_solve[key](self[key], data_dict[key])
  172.         
  173.  
  174.     
  175.     def has_key(self, key):
  176.         return self.__dict__.has_key(key)
  177.  
  178.     
  179.     def hasattr(self, key):
  180.         return self.__dict__.has_key(key)
  181.  
  182.     
  183.     def items(self):
  184.         return self.__dict__.items()
  185.  
  186.     
  187.     def keys(self):
  188.         return self.__dict__.keys()
  189.  
  190.     
  191.     def values(self, keys = None):
  192.         if not keys:
  193.             return self.__dict__.values()
  194.         ret = []
  195.         for k in keys:
  196.             ret.append(self[k])
  197.         
  198.         return ret
  199.  
  200.     
  201.     def get(self, attr, val = None):
  202.         
  203.         try:
  204.             return self[attr]
  205.         except KeyError:
  206.             return val
  207.  
  208.  
  209.     
  210.     def setdefault(self, attr, val = None):
  211.         if not self.has_key(attr):
  212.             self[attr] = val
  213.         
  214.         return self.get(attr, val)
  215.  
  216.     
  217.     def allow_new_attr(self, allow = True):
  218.         self['__allownew'] = allow
  219.  
  220.  
  221.