~ubuntu-branches/ubuntu/gutsy/serpentine/gutsy

« back to all changes in this revision

Viewing changes to serpentine/components.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-08-16 21:15:55 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070816211555-t0h0nugk2kn1tb61
Tags: 0.9-0ubuntu1
* New upstream version
* debian/control:
  - updated XS-Python-Version value

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
        component = component.parent
34
34
    return component
35
35
 
36
 
class ObjectPath (object):
37
 
    """
38
 
    An ObjectPath binds the path to a certain attribute to the instance
39
 
    of this class. It can have a relative or a absolute path, example:
40
 
    class Foo(Component):
41
 
        a_dict = {}
42
 
        a_list = []
43
 
        
44
 
        grandparent = ObjectPath ("../..")
45
 
        root = ObjectPath ("/", static=True)
46
 
        a_list_count = ObjectPath("a_list.__len__()")
47
 
    
48
 
    You can navigate up the hierarchy if you use the '..' fragment, this
49
 
    assumes your object has a field named 'parent'.
50
 
    
51
 
    You can also call any callable fragment using "()".
52
 
    
53
 
    If you assign the 'static' keyword argument to True then the
54
 
    value is only retrieved once.
55
 
    """
56
 
    def __init__ (self, path, static = False):
57
 
        self.fullPath = path.split("/")
58
 
        self.isAbsolute = path.startswith("/")
59
 
        self.isStatic = static
60
 
    
61
 
    def transverse (self, full_path, helper_func):
62
 
        
63
 
        return obj
64
 
        
65
 
    def __get__ (self, obj, type = None):
66
 
 
67
 
        if self.isStatic and hasattr(self, "returnValue"):
68
 
            return self.returnValue
69
 
            
70
 
        if self.isAbsolute:
71
 
            obj = getRootComponent(obj)
72
 
            
73
 
        for path in self.fullPath:
74
 
            if path == "." or path == "":
75
 
                pass
76
 
            elif path == "..":
77
 
                obj = obj.parent
78
 
            else:
79
 
                is_callable = path.endswith("()")
80
 
                if is_callable:
81
 
                    path = path[:-len ("()")]
82
 
                
83
 
                obj = getattr(obj, path)
84
 
                if is_callable:
85
 
                    obj = obj()
86
 
        
87
 
        if self.isStatic:
88
 
            self.returnValue = obj
89
 
            
90
 
        return obj
91
 
 
92
 
class PropertyWrapper (object):
93
 
    """
94
 
    PropertyWrapper is usefull to wrap the getter and setter methods of a
95
 
    variable that is not accessible through the 'property' protocol.
96
 
    It accepts private variables as well. 
97
 
    """
98
 
    def __init__ (self, variable, getter = None, setter = None):
99
 
        self.variable = variable
100
 
        self.realVariable = None
101
 
        self.getter = getter
102
 
        self.setter = setter
103
 
        
104
 
        if self.setter is None:
105
 
            del self.__set__
106
 
        
107
 
        if self.getter is None:
108
 
            del self.__get__
109
 
        
110
 
    def getVariable (self, obj):
111
 
        if self.realVariable is None:
112
 
            if self.variable.startswith("__"):
113
 
                self.realVariable = "_" + type(obj).__name__ + self.variable
114
 
            else:
115
 
                self.realVariable = self.variable
116
 
        return getattr (obj, self.realVariable)
117
 
    
118
 
    def __get__ (self, obj, type = None):
119
 
        obj = self.getVariable (obj)
120
 
        return getattr (obj, self.getter)()
121
 
    
122
 
    def __set__ (self, obj, value):
123
 
        obj = self.getVariable (obj)
124
 
        getattr (obj, self.setter) (value)
125
 
 
126
 
class LazyComponent (object):
127
 
    """
128
 
    A 'LazyComponent' is created only when it's needed. It should wrap the
129
 
    component that you want to make lazy.
130
 
    
131
 
    Usage example:
132
 
    
133
 
    class Bar (Component):
134
 
        pass
135
 
    
136
 
    class Foo (Component):
137
 
        bar = LazyComponent (Bar)
138
 
    
139
 
    When you create an instance of 'Foo', the 'bar' instance will only be
140
 
    created when you access it the first time.    
141
 
    """
142
 
    
143
 
    def __init__ (self, componentFactory):
144
 
        self.componentFactory = componentFactory
145
 
        
146
 
    def __get__ (self, obj, type = None):
147
 
        if hasattr (self, "component"):
148
 
            return self.component
149
 
        
150
 
        self.component = self.componentFactory (obj)
151
 
        return self.component
 
36
 
152
37
 
153
38
class Component (object):
154
39
    """