Skip to content

Commit

Permalink
fixed ruff findings
Browse files Browse the repository at this point in the history
  • Loading branch information
deichmab-draeger committed Nov 20, 2024
1 parent c5d5d80 commit 64ca8c1
Show file tree
Hide file tree
Showing 30 changed files with 365 additions and 256 deletions.
25 changes: 1 addition & 24 deletions src/sdc11073/consumer/serviceclients/contextservice.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""The module contains the implementation of the BICEPS context service."""
from __future__ import annotations

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -28,29 +29,6 @@ class ContextServiceClient(HostedServiceClient):
notifications = (DispatchKey(Actions.EpisodicContextReport, msg_qnames.EpisodicContextReport),
DispatchKey(Actions.PeriodicContextReport, msg_qnames.PeriodicContextReport))

# def mk_proposed_context_object(self, descriptor_handle: str,
# handle: str | None = None) -> AbstractMultiStateProtocol:
# """Create a state that can be used in set_context_state operation.
#
# :param descriptor_handle: the descriptor for which a state shall be created or updated
# :param handle: if None, a new object with default values is created (INSERT operation).
# Else a copy of an existing state with this handle is returned.
# :return: a context state instance
# """
# data_model = self._sdc_definitions.data_model
# mdib = self._mdib_wref()
# if mdib is None:
# raise ApiUsageError('no mdib information')
# context_descriptor_container = mdib.descriptions.handle.get_one(descriptor_handle)
# if handle is None:
# cls = data_model.get_state_container_class(context_descriptor_container.STATE_QNAME)
# obj = cls(descriptor_container=context_descriptor_container)
# obj.Handle = descriptor_handle # this indicates that this is a new context state
# else:
# _obj = mdib.context_states.handle.get_one(handle)
# obj = _obj.mk_copy()
# return obj

def mk_proposed_context_object(self, descriptor_handle: str,
handle: str | None = None) -> AbstractMultiStateProtocol:
"""Create a state that can be used in set_context_state operation.
Expand All @@ -71,7 +49,6 @@ def mk_proposed_context_object(self, descriptor_handle: str,
obj.Handle = descriptor_handle # this indicates that this is a new context state
else:
_obj = context_entity.states[handle]
# _obj = mdib.context_states.handle.get_one(handle)
obj = _obj.mk_copy()
return obj

Expand Down
49 changes: 34 additions & 15 deletions src/sdc11073/entity_mdib/entities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Implementation of entities for EntityProviderMdib."""
from __future__ import annotations

import copy
Expand All @@ -6,17 +7,17 @@

from lxml.etree import QName

from sdc11073.namespaces import QN_TYPE
from sdc11073.namespaces import text_to_qname
from sdc11073.namespaces import QN_TYPE, text_to_qname
from sdc11073.xml_types import pm_qnames
from sdc11073.xml_types.pm_types import CodedValue

if TYPE_CHECKING:
from sdc11073.mdib.descriptorcontainers import AbstractDescriptorContainer
from sdc11073.mdib.statecontainers import AbstractMultiStateContainer, AbstractStateContainer
from sdc11073.xml_utils import LxmlElement

from .entity_consumermdib import EntityConsumerMdib
from .entity_providermdib import EntityProviderMdib
from sdc11073.xml_utils import LxmlElement

# Many types are fixed in schema. This table maps from tag in Element to its type
_static_type_lookup = {
Expand Down Expand Up @@ -45,12 +46,11 @@ def get_xsi_type(element: LxmlElement) -> QName:

if xsi_type_str:
return text_to_qname(xsi_type_str, element.nsmap)
else:
_xsi_type = QName(element.tag)
try:
return _static_type_lookup[_xsi_type]
except KeyError:
raise KeyError(str(_xsi_type))
_xsi_type = QName(element.tag)
try:
return _static_type_lookup[_xsi_type]
except KeyError as err:
raise KeyError(str(_xsi_type)) from err


class _XmlEntityBase:
Expand All @@ -73,7 +73,7 @@ def descriptor(self) -> LxmlElement:
return self._descriptor

@descriptor.setter
def descriptor(self, new_descriptor):
def descriptor(self, new_descriptor: LxmlElement):
self._descriptor = new_descriptor
type_node = self.descriptor.find(pm_qnames.Type)
if type_node is not None:
Expand All @@ -99,6 +99,7 @@ def __init__(self,

@property
def is_multi_state(self) -> bool:
"""Return False because this is not a multi state entity."""
return False

def mk_entity(self, mdib: EntityConsumerMdib) -> ConsumerEntity:
Expand All @@ -120,6 +121,7 @@ def __init__(self,

@property
def is_multi_state(self) -> bool:
"""Return True because this is a multi state entity."""
return True

def mk_entity(self, mdib: EntityConsumerMdib) -> ConsumerMultiStateEntity:
Expand All @@ -138,7 +140,7 @@ def __init__(self,

cls = mdib.sdc_definitions.data_model.get_descriptor_container_class(source.node_type)
if cls is None:
raise ValueError(f'do not know how to make container from {str(source.node_type)}')
raise ValueError(f'do not know how to make container from {source.node_type!s}')
handle = source.descriptor.get('Handle')
self.descriptor: AbstractDescriptorContainer = cls(handle, parent_handle=source.parent_handle)
self.descriptor.update_from_node(source.descriptor)
Expand All @@ -147,14 +149,17 @@ def __init__(self,

@property
def handle(self) -> str:
"""Return the handle of the descriptor."""
return self.descriptor.Handle

@property
def parent_handle(self) -> str | None:
"""Return the parent handle of the descriptor."""
return self.descriptor.parent_handle

@property
def node_type(self) -> QName:
"""Return the node type of the descriptor."""
return self.descriptor.NODETYPE

def __str__(self):
Expand All @@ -175,6 +180,7 @@ def __init__(self,
self.state.update_from_node(source.state)

def update(self):
"""Update the entity from current data in mdib."""
xml_entity = self._mdib.internal_entities.get(self.handle)
if xml_entity is None:
raise ValueError('entity no longer exists in mdib')
Expand All @@ -200,7 +206,7 @@ def __init__(self,
self.states[handle] = state_container

def update(self):
"""Update all containers."""
"""Update the entity from current data in mdib."""
xml_entity = self._mdib.internal_entities.get(self.handle)
if xml_entity is None:
raise ValueError('entity no longer exists in mdib')
Expand Down Expand Up @@ -230,11 +236,12 @@ def update(self):
self.states.pop(handle)

def new_state(self, state_handle: str | None = None) -> AbstractMultiStateContainer:
"""create a new state.
"""Create a new state.
The new state has handle of descriptor container as handle.
If this new state is used as a proposed context state in SetContextState operation, this means a new
state shall be created on providers side."""
state shall be created on providers side.
"""
if state_handle in self.states:
raise ValueError(
f'State handle {state_handle} already exists in {self.__class__.__name__}, handle = {self.handle}')
Expand All @@ -259,18 +266,22 @@ def __init__(self, descriptor: AbstractDescriptorContainer):

@property
def handle(self) -> str:
"""Return the handle of the descriptor."""
return self.descriptor.Handle

@property
def parent_handle(self) -> str | None:
"""Return the parent handle of the descriptor."""
return self.descriptor.parent_handle

@property
def source_mds(self) -> str:
"""Return the source mds of the descriptor."""
return self.descriptor.source_mds

@property
def node_type(self) -> QName:
"""Return the node type of the descriptor."""
return self.descriptor.NODETYPE

def __str__(self):
Expand All @@ -290,6 +301,7 @@ def __init__(self,

@property
def state(self) -> AbstractStateContainer | None:
"""Return the state member of the entity."""
return self._state

@state.setter
Expand All @@ -299,6 +311,7 @@ def state(self, new_state: AbstractStateContainer):

@property
def is_multi_state(self) -> bool:
"""Return False because this is not a multi state entity."""
return False

def mk_entity(self, mdib: EntityProviderMdib) -> ProviderEntity:
Expand All @@ -317,6 +330,7 @@ def __init__(self,

@property
def is_multi_state(self) -> bool:
"""Return True because this is a multi state entity."""
return True

def mk_entity(self, mdib: EntityProviderMdib) -> ProviderMultiStateEntity:
Expand All @@ -336,14 +350,17 @@ def __init__(self,

@property
def handle(self) -> str:
"""Return the handle of the descriptor."""
return self.descriptor.Handle

@property
def parent_handle(self) -> str | None:
"""Return the parent handle of the descriptor."""
return self.descriptor.parent_handle

@property
def node_type(self) -> QName:
"""Return the node type of the descriptor."""
return self.descriptor.NODETYPE

def __str__(self):
Expand All @@ -363,6 +380,7 @@ def __init__(self,

@property
def is_multi_state(self) -> bool:
"""Return False because this is not a multi state entity."""
return False

def update(self):
Expand All @@ -385,6 +403,7 @@ def __init__(self,

@property
def is_multi_state(self) -> bool:
"""Return True because this is a multi state entity."""
return True

def update(self):
Expand All @@ -405,7 +424,7 @@ def update(self):
self.states.pop(handle)

def new_state(self, state_handle: str | None = None) -> AbstractMultiStateContainer:
"""create a new state."""
"""Create a new state."""
if state_handle in self.states:
raise ValueError(
f'State handle {state_handle} already exists in {self.__class__.__name__}, handle = {self.handle}')
Expand Down
Loading

0 comments on commit 64ca8c1

Please sign in to comment.