def __init__(self) -> None: super().__init__() self.root = GraphRoot(self.cloud, {}) self.graph = Graph() self.graph.add_node(self.root, label=self.root.id, resource_type=self.root.resource_type)
class GraphContainer: """A context containing a Graph() This can be passed to various code parts like e.g. a WebServer() allowing replacement and updating of the graph without losing its context. """ GRAPH_ROOT = GraphRoot('cloudkeeper', {}) def __init__(self, cache_graph=True) -> None: self._graph = None self._observers = [] self.__lock = threading.Lock() self.graph = Graph() resource_attr = get_resource_attributes(self.GRAPH_ROOT) self.graph.add_node(self.GRAPH_ROOT, label=self.GRAPH_ROOT.id, **resource_attr) if cache_graph: self.cache = GraphCache() self.cache.update_cache(Event(EventType.STARTUP, self.graph)) add_event_listener(EventType.COLLECT_FINISH, self.cache.update_cache) add_event_listener(EventType.CLEANUP_FINISH, self.cache.update_cache) else: self.cache = None def __del__(self): if self.cache is not None: remove_event_listener(EventType.CLEANUP_FINISH, self.cache.update_cache) remove_event_listener(EventType.COLLECT_FINISH, self.cache.update_cache) @property def graph(self): return self._graph @graph.setter def graph(self, value): self._graph = value def add(self, graph) -> None: """Add another graph to the existing one""" with self.__lock: self.graph = networkx.compose(self.graph, graph) @staticmethod def add_args(arg_parser: ArgumentParser) -> None: """Add args to the arg parser This adds the GraphContainer()'s own args. """ arg_parser.add_argument('--tag-as-metrics-label', help='Tag to use as metrics label', dest='metrics_tag_as_label', type=str, default=None, nargs='+') @property def pickle(self): if self.cache and self.cache.pickle: return self.cache.pickle else: return graph2pickle(self.graph) @property def json(self): if self.cache and self.cache.json: return self.cache.json else: return graph2json(self.graph) @property def text(self): if self.cache and self.cache.text: return self.cache.text else: return graph2text(self.graph) @property def graphml(self): if self.cache and self.cache.graphml: return self.cache.graphml else: return graph2graphml(self.graph) @property def gexf(self): if self.cache and self.cache.gexf: return self.cache.gexf else: return graph2gexf(self.graph) @property def pajek(self): if self.cache and self.cache.pajek: return self.cache.pajek else: return graph2pajek(self.graph) @property def metrics(self): if self.cache and self.cache.metrics: return self.cache.metrics else: return graph2metrics(self.graph)