home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / properties.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  5.0 KB  |  117 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from checkbox.attribute import Attribute
  5. from checkbox.variables import BoolVariable, StringVariable, PathVariable, IntVariable, FloatVariable, ListVariable, VariableFactory, Variable, get_variable
  6.  
  7. class Property(object):
  8.     
  9.     def __init__(self, variable_class = Variable, variable_kwargs = { }):
  10.         self._variable_class = variable_class
  11.         self._variable_kwargs = variable_kwargs
  12.  
  13.     
  14.     def __get__(self, obj, cls = None):
  15.         if obj is None:
  16.             return self._get_attribute(cls)
  17.         if cls is None:
  18.             cls = type(obj)
  19.         
  20.         attribute = self._get_attribute(cls)
  21.         variable = get_variable(obj, attribute)
  22.         return variable.get()
  23.  
  24.     
  25.     def __set__(self, obj, value):
  26.         cls = type(obj)
  27.         attribute = self._get_attribute(cls)
  28.         variable = get_variable(obj, attribute)
  29.         variable.set(value)
  30.  
  31.     
  32.     def _detect_name(self, used_cls):
  33.         self_id = id(self)
  34.         for cls in used_cls.__mro__:
  35.             for attr, prop in cls.__dict__.items():
  36.                 if id(prop) == self_id:
  37.                     return attr
  38.             
  39.         
  40.         raise RuntimeError('Property used in an unknown class')
  41.  
  42.     
  43.     def _get_attribute(self, cls):
  44.         
  45.         try:
  46.             attribute = cls.__dict__['_checkbox_attributes'].get(self)
  47.         except KeyError:
  48.             cls._checkbox_attributes = { }
  49.             attribute = None
  50.  
  51.         if attribute is None:
  52.             name = self._detect_name(cls)
  53.             attribute = PropertyAttribute(self, cls, name, self._variable_class, self._variable_kwargs)
  54.             cls._checkbox_attributes[self] = attribute
  55.         
  56.         return attribute
  57.  
  58.  
  59.  
  60. class PropertyAttribute(Attribute):
  61.     
  62.     def __init__(self, prop, cls, name, variable_class, variable_kwargs):
  63.         super(PropertyAttribute, self).__init__(name, cls, VariableFactory(variable_class, attribute = self, **variable_kwargs))
  64.         self.cls = cls
  65.         for attr in [
  66.             '__get__',
  67.             '__set__']:
  68.             setattr(self, attr, getattr(prop, attr))
  69.         
  70.  
  71.  
  72.  
  73. class PropertyType(Property):
  74.     
  75.     def __init__(self, **kwargs):
  76.         kwargs['value'] = kwargs.pop('default', None)
  77.         kwargs['value_factory'] = kwargs.pop('default_factory', None)
  78.         super(PropertyType, self).__init__(self.variable_class, kwargs)
  79.  
  80.  
  81.  
  82. class Bool(PropertyType):
  83.     variable_class = BoolVariable
  84.  
  85.  
  86. class String(PropertyType):
  87.     variable_class = StringVariable
  88.  
  89.  
  90. class Path(PropertyType):
  91.     variable_class = PathVariable
  92.  
  93.  
  94. class Int(PropertyType):
  95.     variable_class = IntVariable
  96.  
  97.  
  98. class Float(PropertyType):
  99.     variable_class = FloatVariable
  100.  
  101.  
  102. class List(PropertyType):
  103.     variable_class = ListVariable
  104.     
  105.     def __init__(self, **kwargs):
  106.         if 'default' in kwargs:
  107.             raise ValueError("'default' not allowed for List. Use 'default_factory' instead.")
  108.         'default' in kwargs
  109.         type = kwargs.pop('type', None)
  110.         if type is None:
  111.             type = Property()
  112.         
  113.         kwargs['item_factory'] = VariableFactory(type._variable_class, **type._variable_kwargs)
  114.         super(List, self).__init__(**kwargs)
  115.  
  116.  
  117.