def generate_context_topology(topo, name='context'): """Generate a context topology where nodes are components and resources, and edges are dependencies from components to resources, or from resources to the topology. :param str name: topology name. """ # initialize context and topology context = Context() def addElt(elt): """ Add input elt in topology. :param GraphElement elt: elt to add to topology. """ topo.add_elts(elt.id) elt.save(manager) components = context.find({'$in': ['component', 'topo', 'selector']}) for component in components: component_id = context.get_entity_id(component) component_node = TopoNode(entity=component_id) addElt(component_node) ctx, name = context.get_entity_context_and_name(component) ctx['component'] = name resources = context.find('resource', context=ctx) if resources: # link component to all its resources with the same edge edge = TopoEdge(sources=component_node.id, targets=[]) addElt(edge) # add edge in topology for resource in resources: resource_id = context.get_entity_id(resource) resource_node = TopoNode(entity=resource_id) addElt(resource_node) # save resource node # add resource from component edge.targets.append(resource_node.id) res2topo = TopoEdge( sources=resource_node.id, targets=topo.id ) addElt(res2topo) if not edge.targets: # bind topology from component if not sources edge.targets.append(topo.id) addElt(edge) # save edge in all cases else: # if no resources, link the component to the topology edge = TopoEdge(sources=component_node.id, targets=topo.id) addElt(edge) # add edge in topology
class CTXContextRegistry(CTXPropRegistry): """In charge of contextual context properties. """ __datatype__ = 'context' #: default datatype name def __init__(self, *args, **kwargs): super(CTXContextRegistry, self).__init__(*args, **kwargs) self.manager = Context() def _get_documents(self, ids, query): query[Context.DATA_ID] = ids return self.manager.find(_filter=query) def _get(self, ids, query, *args, **kwargs): return self._get_documents(ids=ids, query=query) def _delete(self, ids, query, *args, **kwargs): docs = self._get_documents(ids=ids, query=query) ids = [doc[Context.DATA_ID] for doc in docs] self.manager.remove(ids=ids) return docs def ids(self, query=None): result = set() elts = self.manager.find(_filter=query) for elt in elts: result.add(elt[Context.DATA_ID]) return list(result)