root/trunk/RBSkinning/RBSkinning/SkinContext.py

Revision 609, 4.5 kB (checked in by sholloway, 6 years ago)

Changes to simplify dockhost/dockcontainer relationships

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 from __future__ import generators
27
28 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #~ Class
30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
32 class SkinContext(object):
33     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34     #~ Special
35     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36
37     def __init__(self, NextContext, Data={}):
38         self._next = NextContext
39         self.__dict__.update(Data)
40
41     def __getattribute__(self, name):
42         if not name:
43             raise AttributeError, 'name cannot be empty'
44         elif name in ('__dict__', '_next'):
45             return object.__getattribute__(self, name)
46         elif name == 'ctx':
47             return self
48         elif name == 'nextctx':
49             return self._next
50         elif name == 'rootctx':
51             return self._RootContext()
52         elif name[0] != '_' or name in ('__root__', '__skinner__'):
53             ctx = self
54             while ctx and (name not in ctx.__dict__):
55                 ctx = ctx._next
56             if ctx: return ctx.__dict__[name]
57         return object.__getattribute__(self, name)
58
59     ## Redundant
60     #def __setattr__(self, name, value):
61     #    return object.__setattr__(self, name, value)
62
63     def __delattr__(self, name):
64         if name not in self.__dict__ and self._next:
65             return self._next.__delattr__(name)
66         else:
67             return object.__delattr__(self, name)
68
69     def __getitem__(self, idx):
70         result = self
71         for i in xrange(0, abs(idx)):
72             result = result._next
73         return result
74
75     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76     #~ Public Methods
77     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78
79     def getnamedvar(self, DottedNameList, SliceEnd=None):
80         """Like getattr, but can deal with dotted names"""
81         # Split the dotted name
82         names = DottedNameList.split('.')
83         if SliceEnd is not None:
84             names, remaining = names[:SliceEnd], names[SliceEnd:]
85         # Find the attr by reducing with getattr
86         result = self
87         for each in names:
88             result = getattr(result, each)
89         # Return the appropriate result
90         if SliceEnd is None: return result
91         else: return result, remaining
92
93     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94     #~ Protected Methods
95     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96
97     def _update(self, Data):
98         self.__dict__.update(Data)
99
100     def _OwnerContext(self, name, returnLast=0):
101         curr = self
102         while name not in curr.__dict__:
103             if not curr._next:
104                 if not returnLast:
105                     curr = curr._next
106                 break
107             curr = curr._next
108         return curr
109     OwnerOf = _OwnerContext
110        
111     def _GetContext(self, *args, **kw):
112         lst = [self]
113         while lst[-1]:
114             lst.append(lst[-1]._next)
115         lst.pop()
116         return lst.__getitem__(*args,**kw)
117        
118     def _RootContext(self):
119         curr = self
120         while curr._next: curr = curr._next
121         return curr
122     RootContext = _RootContext
123     Root = _RootContext
124    
125     def itercontext(self):
126         curr = self
127         while curr:
128             yield curr
129             curr = curr._next
130
131
132 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133 #~ Testing
134 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135
136 if __name__=='__main__':
137     print "Testing..."
138     ctxA = SkinContext(None)
139     ctxB = SkinContext(ctxA)
140     ctxA.a = 'This is ctxA.a'
141     ctxA.c = 'This is ctxA.c'
142     ctxB.b = 'This is ctxB.b'
143     ctxB.c = 'This is ctxB.c'
144
145     print ctxA.a
146     print ctxA.c
147
148     print ctxB.a
149     print ctxB.b
150     print ctxB.c
151
152     print "Test complete."
153
Note: See TracBrowser for help on using the browser.