Skip to content
Snippets Groups Projects
Context.py 1.42 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import Dict
from ...engines._DatabaseEngine import _DatabaseEngine
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from ..entity._RootEntity import _RootEntity
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from ..entity.EntityCollection import EntityCollection
from .Keys import KEY_CONTEXT, KEY_TOPOLOGIES
from .Topology import Topology

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
VALIDATORS = {}  # no attributes accepted
TRANSCODERS = {} # no transcoding applied to attributes
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class Context(_RootEntity):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(self, context_uuid : str, database_engine : _DatabaseEngine):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        super().__init__(database_engine, context_uuid, KEY_CONTEXT, VALIDATORS, TRANSCODERS)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._topologies = EntityCollection(self, KEY_TOPOLOGIES)

    @property
    def parent(self) -> 'Context': return self

    @property
    def context(self) -> 'Context': return self

    @property
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def context_uuid(self) -> str: return self._entity_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    @property
    def topologies(self) -> EntityCollection: return self._topologies

    def topology(self, topology_uuid : str) -> Topology: return Topology(topology_uuid, self)

    def create(self) -> 'Context': return self

    def delete(self):
        for topology_uuid in self.topologies.get(): self.topology(topology_uuid).delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.attributes.delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_id(self) -> Dict:
        return {
            'contextUuid': {'uuid': self.context_uuid},
        }

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump(self) -> Dict:
        return {topology_uuid : self.topology(topology_uuid).dump() for topology_uuid in self.topologies.get()}