home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / components.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.9 KB  |  187 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import weakref
  5.  
  6. void_method = lambda self: pass
  7.  
  8. void_func = lambda *args: pass
  9.  
  10. def getRootComponent(component):
  11.     '''
  12.     This is a helper function that tranverses the component tree upwards.
  13.     '''
  14.     while component.parent is not None:
  15.         component = component.parent
  16.     return component
  17.  
  18.  
  19. class ObjectPath(object):
  20.     '''
  21.     An ObjectPath binds the path to a certain attribute to the instance
  22.     of this class. It can have a relative or a absolute path, example:
  23.     class Foo(Component):
  24.         a_dict = {}
  25.         a_list = []
  26.         
  27.         grandparent = ObjectPath ("../..")
  28.         root = ObjectPath ("/", static=True)
  29.         a_list_count = ObjectPath("a_list.__len__()")
  30.     
  31.     You can navigate up the hierarchy if you use the \'..\' fragment, this
  32.     assumes your object has a field named \'parent\'.
  33.     
  34.     You can also call any callable fragment using "()".
  35.     
  36.     If you assign the \'static\' keyword argument to True then the
  37.     value is only retrieved once.
  38.     '''
  39.     
  40.     def __init__(self, path, static = False):
  41.         self.fullPath = path.split('/')
  42.         self.isAbsolute = path.startswith('/')
  43.         self.isStatic = static
  44.  
  45.     
  46.     def transverse(self, full_path, helper_func):
  47.         return obj
  48.  
  49.     
  50.     def __get__(self, obj, type = None):
  51.         if self.isStatic and hasattr(self, 'returnValue'):
  52.             return self.returnValue
  53.         
  54.         if self.isAbsolute:
  55.             obj = getRootComponent(obj)
  56.         
  57.         for path in self.fullPath:
  58.             if path == '.' or path == '':
  59.                 continue
  60.             if path == '..':
  61.                 obj = obj.parent
  62.                 continue
  63.             is_callable = path.endswith('()')
  64.             if is_callable:
  65.                 path = path[:-len('()')]
  66.             
  67.             obj = getattr(obj, path)
  68.             if is_callable:
  69.                 obj = obj()
  70.                 continue
  71.         
  72.         if self.isStatic:
  73.             self.returnValue = obj
  74.         
  75.         return obj
  76.  
  77.  
  78.  
  79. class PropertyWrapper(object):
  80.     """
  81.     PropertyWrapper is usefull to wrap the getter and setter methods of a
  82.     variable that is not accessible through the 'property' protocol.
  83.     It accepts private variables as well. 
  84.     """
  85.     
  86.     def __init__(self, variable, getter = None, setter = None):
  87.         self.variable = variable
  88.         self.realVariable = None
  89.         self.getter = getter
  90.         self.setter = setter
  91.         if self.setter is None:
  92.             del self.__set__
  93.         
  94.         if self.getter is None:
  95.             del self.__get__
  96.         
  97.  
  98.     
  99.     def getVariable(self, obj):
  100.         if self.realVariable is None:
  101.             if self.variable.startswith('__'):
  102.                 self.realVariable = '_' + type(obj).__name__ + self.variable
  103.             else:
  104.                 self.realVariable = self.variable
  105.         
  106.         return getattr(obj, self.realVariable)
  107.  
  108.     
  109.     def __get__(self, obj, type = None):
  110.         obj = self.getVariable(obj)
  111.         return getattr(obj, self.getter)()
  112.  
  113.     
  114.     def __set__(self, obj, value):
  115.         obj = self.getVariable(obj)
  116.         getattr(obj, self.setter)(value)
  117.  
  118.  
  119.  
  120. class LazyComponent(object):
  121.     """
  122.     A 'LazyComponent' is created only when it's needed. It should wrap the
  123.     component that you want to make lazy.
  124.     
  125.     Usage example:
  126.     
  127.     class Bar (Component):
  128.         pass
  129.     
  130.     class Foo (Component):
  131.         bar = LazyComponent (Bar)
  132.     
  133.     When you create an instance of 'Foo', the 'bar' instance will only be
  134.     created when you access it the first time.    
  135.     """
  136.     
  137.     def __init__(self, componentFactory):
  138.         self.componentFactory = componentFactory
  139.  
  140.     
  141.     def __get__(self, obj, type = None):
  142.         if hasattr(self, 'component'):
  143.             return self.component
  144.         
  145.         self.component = self.componentFactory(obj)
  146.         return self.component
  147.  
  148.  
  149.  
  150. class Component(object):
  151.     """
  152.     A Component is an object that is structured in a hierarchical model.
  153.     It is constructed upon runtime from the root to its children. To define
  154.     a Component you have to define a list of subcomponents, these are usually
  155.     classes of this type.
  156.     They define a method called '_init' that is called in the constructor.
  157.     It also contains a '_components' protected variable that holds a list of its
  158.     children components.
  159.     """
  160.     
  161.     def __init__(self, parent = None):
  162.         if not parent is not None or weakref.ref(parent):
  163.             pass
  164.         self._Component__parent = void_func
  165.         self._components = []
  166.         for component in self.components:
  167.             self._components.append(component(self))
  168.         
  169.         for attr, component in self.namedComponents.iteritems():
  170.             setattr(self, attr, component(self))
  171.         
  172.         self._init()
  173.  
  174.     
  175.     def _init(self):
  176.         '''Override this method which is called in the constructor.'''
  177.         pass
  178.  
  179.     
  180.     def getParent(self):
  181.         return self._Component__parent()
  182.  
  183.     parent = property(getParent)
  184.     components = ()
  185.     namedComponents = { }
  186.  
  187.