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
.__lastID
= 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
.__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
])))
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
)