]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/dependencymgr.py
1 #----------------------------------------------------------------------------
2 # Name: dependencymgr.py
3 # Purpose: Dependency Manager
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 #----------------------------------------------------------------------------
13 DM_ID_ATTR
= "_DependencyMgr__ID"
15 ##class ManageableObject(object):
17 ## def __init__(self):
18 ## self.__id = DM_NO_ID
20 ## def __repr__(self):
21 ## return "<ManageableObject id = %s>" % self.__id
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))
31 ## _DependencyMgr__ID = property(__getID, __setID)
33 class DependencyMgr ( object ):
39 self
.__ dependencies
= {}
40 self
.__l astID
= DM_NO_ID
42 def addDependency ( self
, parent
, child
):
43 pid
= self
._ initObjectID
( parent
)
45 parentCollection
= self
.__ dependencies
[ pid
]
47 parentCollection
= self
._ newDependencyCollection
()
48 self
.__ dependencies
[ pid
] = parentCollection
49 if ( child
not in parentCollection
):
50 parentCollection
. append ( child
)
52 def removeDependency ( self
, parent
, child
):
53 pid
= self
._ getObjectID
( parent
)
56 parentCollection
= self
.__ dependencies
[ pid
]
57 parentCollection
. remove ( child
)
58 if ( len ( parentCollection
) == 0 ):
59 del self
.__ dependencies
[ pid
]
60 except KeyError , ValueError :
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
)
67 deps
= self
.__ dependencies
[ pid
]
68 del self
.__ dependencies
[ pid
]
73 def hasDependency ( self
, parent
):
75 return ( self
._ getObjectID
( parent
) in self
.__ dependencies
)
77 def getDependencies ( self
, parent
):
78 "Returns a list of objects or an empty list if no dependencies exist."
80 return self
.__ dependencies
[ self
._ getObjectID
( parent
)]
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 ():
90 "Returns the state of the dependency manager including all managed objects as a list of strings"
92 out
. append ( "DependencyMgr %s has %i parent objects, last id assigned is %i " % ( repr ( self
), len ( self
.__ dependencies
), self
.__l astID
))
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
])))
97 def _initObjectID ( self
, obj
):
99 id = getattr ( obj
, DM_ID_ATTR
)
100 except AttributeError :
104 setattr ( obj
, DM_ID_ATTR
, id )
107 def _getObjectID ( self
, obj
):
109 id = getattr ( obj
, DM_ID_ATTR
)
110 except AttributeError :
118 def _newDependencyCollection ( self
):
121 globalDM
= DependencyMgr ()
123 def addDependency ( parent
, child
):
124 getGlobalDM (). addDependency ( parent
, child
)
126 def removeDependency ( parent
, child
):
127 getGlobalDM (). removeDependency ( parent
, child
)
129 def clearDependencies ( parent
):
130 return getGlobalDM (). clearDependencies ( parent
)
132 def hasDependency ( parent
):
133 return getGlobalDM (). hasDependency ( parent
)
135 def getDependencies ( parent
):
136 return getGlobalDM (). getDependencies ( parent
)
139 return getGlobalDM (). getState ()
142 getGlobalDM (). dumpState ( out
)