Skip to content

Commit

Permalink
fix for #73 - render object enties as their id value
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Sep 4, 2023
1 parent 34adca0 commit e921794
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/ibek/ioc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def add_ibek_attributes(cls, entity: Entity):
setattr(entity, arg, render_with_utils(entity_dict, value))
return entity

def __str__(self):
# if this entity has an id then its string representation is the value of id
id_name = self.__definition__._get_id_arg()
return getattr(self, id_name) if id_name else super().__str__()


def make_entity_model(definition: Definition, support: Support) -> Type[Entity]:
"""
Expand Down
7 changes: 7 additions & 0 deletions src/ibek/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ class Definition(BaseSettings):
description="Environment variables to set in the boot script", default=()
)

def _get_id_arg(self):
"""Returns the name of the ID argument for this definition, if it exists"""
for arg in self.args:
if isinstance(arg, IdArg):
return arg.name
return None


class Support(BaseSettings):
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/samples/pydantic/st.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ ioc_registerRecordDeviceDriver pdbbase
testValues test_value:AsynPort1.127.0.0.1
testValues test_value:AsynPort2.10.0.0.2

# exampleTestFunction AsynPortIP Name Value
exampleTestFunction IP:10.0.0.2 name:'Consumer of another port' Value:AsynPort2.10.0.0.2
exampleTestFunction IP:10.0.0.2 name:'Another Consumer of the 2nd port' Value:AsynPort2.10.0.0.2
# exampleTestFunction AsynPortIP Name Port Value
exampleTestFunction IP:10.0.0.2 name:'Consumer of another port' port:'AsynPort2' Value:AsynPort2.10.0.0.2
exampleTestFunction IP:10.0.0.2 name:'Another Consumer of the 2nd port' port:'AsynPort2' Value:AsynPort2.10.0.0.2

dbLoadRecords /tmp/ioc.db
iocInit
Expand Down
1 change: 1 addition & 0 deletions tests/samples/pydantic/test.ibek.support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ defs:
args:
AsynPortIP: IP:{{ PORT.IP }}
Name: name:'{{ name }}'
Port: port:'{{ PORT }}' # defaults to the id of PORT, i.e. PORT.name
Value: Value:{{ PORT.test_value }}

0 comments on commit e921794

Please sign in to comment.