def set_agent(self, agent: Agent): LOG.info(f"Set agent `{agent.public_key}`") address = Namespaces.get_address( agent.public_key, subnamespace=Namespaces.AGENT ) agents = AgentContainer(entries=[agent]) self._context.set_state({address: agents.SerializeToString()})
def test_set_batch(self, state, entity_batch): mock = state._context.set_state state.set_entity_batch(entity_batch) address = Namespaces.get_address( entity_batch.id, subnamespace=Namespaces.ENTITY_BATCH ) mock.assert_called_with({address: entity_batch.SerializeToString()})
class PackageHandler(HandlerBase): prefix = Namespaces.get_prefix(Namespaces.PACKAGE) @staticmethod def process(changes: "StateChange"): LOG.info(f"Process changes with `PackageHandler`: {changes.address}") package = Package() package.ParseFromString(changes.value) LOG.debug(f"Package: {package.id} ({package.type})")
def test_set_agent(self, state, agent): mock = state._context.set_state state.set_agent(agent) address = Namespaces.get_address( agent.public_key, subnamespace=Namespaces.AGENT ) ac = AgentContainer(entries=[agent]) mock.assert_called_with({address: ac.SerializeToString()})
def test_get_entity(self, state, entity): mock = state._context.get_state address = Namespaces.get_address(entity.id, subnamespace=Namespaces.ENTITY) entry = TpStateEntry(address=address, data=entity.SerializeToString()) mock.return_value = [entry] returned_entity = state.get_entity(entity.id) mock.assert_called_with([address]) assert returned_entity == entity
class EntityHandler(HandlerBase): prefix = Namespaces.get_prefix(Namespaces.ENTITY) @staticmethod def process(changes: "StateChange"): LOG.info(f"Process changes with `EntityHandler`: {changes.address}") entity = Entity() entity.ParseFromString(changes.value) LOG.debug(f"Entity: {entity.id} ({entity.commodity_type})")
class AgentHandler(HandlerBase): prefix = Namespaces.get_prefix(Namespaces.AGENT) @staticmethod def process(changes: "StateChange"): LOG.info(f"Process changes with `AgentHandler`: {changes.address}") container = AgentContainer() container.ParseFromString(changes.value) agent = container.entries[0] LOG.debug(f"Agent: {agent.email}")
class ReplantationHandler(HandlerBase): prefix = Namespaces.get_prefix(Namespaces.REPLANTATION) @staticmethod def process(changes: "StateChange"): LOG.info( f"Process changes with `ReplantationHandler`: {changes.address}") replantation = Replantation() replantation.ParseFromString(changes.value) LOG.debug(f"Replantation: {replantation.id}")
def get_package(self, package_id: str): address = Namespaces.get_address(package_id, subnamespace=Namespaces.PACKAGE) package_list = self._context.get_state([address]) if not package_list: LOG.warning("No packages found.") return package = Package() package.ParseFromString(package_list[0].data) return package
def test_get_agent(self, state, agent): mock = state._context.get_state address = Namespaces.get_address( agent.public_key, subnamespace=Namespaces.AGENT ) ac = AgentContainer(entries=[agent]).SerializeToString() entry = TpStateEntry(address=address, data=ac) mock.return_value = [entry] returned_agent = state.get_agent(agent.public_key) mock.assert_called_with([address]) assert returned_agent == agent
def get_agent(self, public_key: str): LOG.info(f"Get agent `{public_key}`") address = Namespaces.get_address(public_key, subnamespace=Namespaces.AGENT) agents = self._context.get_state([address]) if not agents: LOG.warning("No agents found.") return container = AgentContainer() container.ParseFromString(agents[0].data) if len(container.entries) > 1: LOG.warning("Found more than one agent. Return first") elif len(container.entries) == 0: LOG.warning("No agents found in container.") return return container.entries.pop(0)
def test_set_entity(self, state, entity): mock = state._context.set_state state.set_entity(entity) address = Namespaces.get_address(entity.id, subnamespace=Namespaces.ENTITY) mock.assert_called_with({address: entity.SerializeToString()})
def set_replantation(self, replantation: Replantation): address = Namespaces.get_address(str(replantation.id), subnamespace=Namespaces.REPLANTATION) self._context.set_state({address: replantation.SerializeToString()})
def set_package(self, package: Package): address = Namespaces.get_address(package.id, subnamespace=Namespaces.PACKAGE) self._context.set_state({address: package.SerializeToString()})