]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/dependencymgr.py
Added the ActiveGrid IDE as a sample application
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / dependencymgr.py
1 #----------------------------------------------------------------------------
2 # Name: dependencymgr.py
3 # Purpose: Dependency Manager
4 #
5 # Author: Jeff Norton
6 #
7 # Created: 01/28/05
8 # CVS-ID: $Id$
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 #----------------------------------------------------------------------------
11
12 DM_NO_ID = 0
13 DM_ID_ATTR = "_DependencyMgr__ID"
14
15 ##class ManageableObject(object):
16 ##
17 ## def __init__(self):
18 ## self.__id = DM_NO_ID
19 ##
20 ## def __repr__(self):
21 ## return "<ManageableObject id = %s>" % self.__id
22 ##
23 ## def __getID(self):
24 ## return self.__id
25 ##
26 ## def __setID(self, value):
27 ## if (self.__id != DM_NO_ID):
28 ## raise DependencyMgrException("Cannot set the dependency ID on object %s to \"%s\" because it already has one (\"%s\")." % (repr(self), value, self.__id))
29 ## self.__id = value
30 ##
31 ## _DependencyMgr__ID = property(__getID, __setID)
32
33 class DependencyMgr(object):
34
35 def __init__(self):
36 self.clear()
37
38 def clear(self):
39 self.__dependencies = {}
40 self.__lastID = DM_NO_ID
41
42 def addDependency(self, parent, child):
43 pid = self._initObjectID(parent)
44 try:
45 parentCollection = self.__dependencies[pid]
46 except KeyError:
47 parentCollection = self._newDependencyCollection()
48 self.__dependencies[pid] = parentCollection
49 if (child not in parentCollection):
50 parentCollection.append(child)
51
52 def removeDependency(self, parent, child):
53 pid = self._getObjectID(parent)
54 if (pid != DM_NO_ID):
55 try:
56 parentCollection = self.__dependencies[pid]
57 parentCollection.remove(child)
58 if (len(parentCollection) == 0):
59 del self.__dependencies[pid]
60 except KeyError, ValueError:
61 pass
62
63 def clearDependencies(self, parent):
64 "Returns a list of objects or an empty list if no dependencies exist as for getDependencies, and then removes the dependency list."
65 pid = self._getObjectID(parent)
66 try:
67 deps = self.__dependencies[pid]
68 del self.__dependencies[pid]
69 return deps
70 except KeyError:
71 return []
72
73 def hasDependency(self, parent):
74 "Returns a boolean"
75 return (self._getObjectID(parent) in self.__dependencies)
76
77 def getDependencies(self, parent):
78 "Returns a list of objects or an empty list if no dependencies exist."
79 try:
80 return self.__dependencies[self._getObjectID(parent)]
81 except KeyError:
82 return []
83
84 def dumpState(self, out):
85 "Writes the state of the dependency manager (as reported by getState) to out"
86 for line in self.getState():
87 print >> out, line
88
89 def getState(self):
90 "Returns the state of the dependency manager including all managed objects as a list of strings"
91 out = []
92 out.append("DependencyMgr %s has %i parent objects, last id assigned is %i" % (repr(self), len(self.__dependencies), self.__lastID))
93 for key, val in self.__dependencies.iteritems():
94 out.append("Object %s has dependents: %s " % (repr(key), ", ".join([repr(d) for d in val])))
95 return out
96
97 def _initObjectID(self, obj):
98 try:
99 id = getattr(obj, DM_ID_ATTR)
100 except AttributeError:
101 id = DM_NO_ID
102 if (id == DM_NO_ID):
103 id = self._newID()
104 setattr(obj, DM_ID_ATTR, id)
105 return id
106
107 def _getObjectID(self, obj):
108 try:
109 id = getattr(obj, DM_ID_ATTR)
110 except AttributeError:
111 id = DM_NO_ID
112 return id
113
114 def _newID(self):
115 self.__lastID += 1
116 return self.__lastID
117
118 def _newDependencyCollection(self):
119 return []
120
121 globalDM = DependencyMgr()
122
123 def addDependency(parent, child):
124 getGlobalDM().addDependency(parent, child)
125
126 def removeDependency(parent, child):
127 getGlobalDM().removeDependency(parent, child)
128
129 def clearDependencies(parent):
130 return getGlobalDM().clearDependencies(parent)
131
132 def hasDependency(parent):
133 return getGlobalDM().hasDependency(parent)
134
135 def getDependencies(parent):
136 return getGlobalDM().getDependencies(parent)
137
138 def getState():
139 return getGlobalDM().getState()
140
141 def dumpState(out):
142 getGlobalDM().dumpState(out)
143
144 def getGlobalDM():
145 return globalDM