Skip to content

Commit

Permalink
convert jinja non strings back to their type
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Jun 1, 2024
1 parent ec460d8 commit 93b5f59
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 638 deletions.
3 changes: 2 additions & 1 deletion src/ibek/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def _make_entity_model(
def add_arg(name, typ, description, default):
if default is None:
default = PydanticUndefined
if typ in [str, object]:
# cheesy check for enum, can this be improved?
if typ in [str, object] or "enum" in str(typ):
args[name] = (Annotated[typ, Field(description=description)], default)
else:
args[name] = (
Expand Down
15 changes: 14 additions & 1 deletion src/ibek/ioc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import json
from enum import Enum
from typing import Any, Dict, List, Sequence

Expand Down Expand Up @@ -71,7 +72,19 @@ def add_ibek_attributes(self):
if isinstance(value, str):
# Jinja expansion of any of the Entity's string args/values
value = UTILS.render(entity_dict, value)
setattr(self, arg, str(value))
# this is a cheesy test - any better ideas please let me know
if "Union" in str(model_field.annotation):
# Args that were non strings and have been rendered by Jinja
# must be coerced back into their original type
try:
# shame I need the replace - why are jinja an json disagreeing?
value = json.loads(value.replace("'", '"'))
except:
print(
f"ERROR: fail to decode {value} as a {model_field.annotation}"
)
raise
setattr(self, arg, value)
# update the entity_dict with the rendered value
entity_dict[arg] = value

Expand Down
2 changes: 1 addition & 1 deletion tests/samples/iocs/ipac-test.ibek.ioc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# yaml-language-server: $schema=file:///tmp/schema.json
# yaml-language-server: $schema=../schemas/ipac-test.ibek.schema.json

ioc_name: ipac-test

Expand Down
3 changes: 2 additions & 1 deletion tests/samples/iocs/listarg.ibek.ioc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ entities:
friends: ["Rimmer", "Holly", "Cat", "Kryten"]

- entity_type: listtest.lister
friends: '{{ ["Rimmer"] + ["tt"] | list }}'
friends: |-
{{ ["Rimmer"] + ["tt"] | list }}
20 changes: 3 additions & 17 deletions tests/samples/outputs/listarg/st.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,9 @@ Lister does not like to hear "Smoke me a kipper, I'll be back for breakfast" has
- Kryten
First friend is Rimmer
Lister does not like to hear "Smoke me a kipper, I'll be back for breakfast" has friends:
- [
- '
- R
- i
- m
- m
- e
- r
- '
- ,
-
- '
- t
- t
- '
- ]
First friend is [
- Rimmer
- tt
First friend is Rimmer

dbLoadRecords /epics/runtime/ioc.db
iocInit
Expand Down
85 changes: 15 additions & 70 deletions tests/samples/schemas/gauges.ibek.ioc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,104 +101,49 @@
"title": "Baud"
},
"parity": {
"anyOf": [
"allOf": [
{
"description": "jinja that renders to <enum 'parity'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
{
"allOf": [
{
"$ref": "#/$defs/parity"
}
],
"description": "Parity"
"$ref": "#/$defs/parity"
}
],
"default": "none",
"description": "union of <enum 'parity'> and jinja representation of {typ}",
"title": "Parity"
"description": "Parity"
},
"crtscts": {
"anyOf": [
"allOf": [
{
"description": "jinja that renders to <enum 'crtscts'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
{
"allOf": [
{
"$ref": "#/$defs/crtscts"
}
],
"description": "Set hardware flow control on"
"$ref": "#/$defs/crtscts"
}
],
"default": "N",
"description": "union of <enum 'crtscts'> and jinja representation of {typ}",
"title": "Crtscts"
"description": "Set hardware flow control on"
},
"stop": {
"anyOf": [
"allOf": [
{
"description": "jinja that renders to <enum 'stop'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
{
"allOf": [
{
"$ref": "#/$defs/stop"
}
],
"description": "Stop Bits"
"$ref": "#/$defs/stop"
}
],
"default": "1",
"description": "union of <enum 'stop'> and jinja representation of {typ}",
"title": "Stop"
"description": "Stop Bits"
},
"disconnectOnReadTimeout": {
"anyOf": [
{
"description": "jinja that renders to <enum 'disconnectOnReadTimeout'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/disconnectOnReadTimeout"
}
],
"description": "Disconnect when a read times out"
"$ref": "#/$defs/disconnectOnReadTimeout"
}
],
"default": "Y",
"description": "union of <enum 'disconnectOnReadTimeout'> and jinja representation of {typ}",
"title": "Disconnectonreadtimeout"
"description": "Disconnect when a read times out"
},
"bits": {
"anyOf": [
{
"description": "jinja that renders to <enum 'bits'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/bits"
}
],
"description": "Bits"
"$ref": "#/$defs/bits"
}
],
"default": "8",
"description": "union of <enum 'bits'> and jinja representation of {typ}",
"title": "Bits"
"description": "Bits"
}
},
"required": [
Expand Down
102 changes: 18 additions & 84 deletions tests/samples/schemas/motorSim.ibek.ioc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,104 +109,49 @@
"title": "Baud"
},
"parity": {
"anyOf": [
{
"description": "jinja that renders to <enum 'parity'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/parity"
}
],
"description": "Parity"
"$ref": "#/$defs/parity"
}
],
"default": "none",
"description": "union of <enum 'parity'> and jinja representation of {typ}",
"title": "Parity"
"description": "Parity"
},
"crtscts": {
"anyOf": [
"allOf": [
{
"description": "jinja that renders to <enum 'crtscts'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
{
"allOf": [
{
"$ref": "#/$defs/crtscts"
}
],
"description": "Set hardware flow control on"
"$ref": "#/$defs/crtscts"
}
],
"default": "N",
"description": "union of <enum 'crtscts'> and jinja representation of {typ}",
"title": "Crtscts"
"description": "Set hardware flow control on"
},
"stop": {
"anyOf": [
{
"description": "jinja that renders to <enum 'stop'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/stop"
}
],
"description": "Stop Bits"
"$ref": "#/$defs/stop"
}
],
"default": "1",
"description": "union of <enum 'stop'> and jinja representation of {typ}",
"title": "Stop"
"description": "Stop Bits"
},
"disconnectOnReadTimeout": {
"anyOf": [
{
"description": "jinja that renders to <enum 'disconnectOnReadTimeout'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/disconnectOnReadTimeout"
}
],
"description": "Disconnect when a read times out"
"$ref": "#/$defs/disconnectOnReadTimeout"
}
],
"default": "Y",
"description": "union of <enum 'disconnectOnReadTimeout'> and jinja representation of {typ}",
"title": "Disconnectonreadtimeout"
"description": "Disconnect when a read times out"
},
"bits": {
"anyOf": [
"allOf": [
{
"description": "jinja that renders to <enum 'bits'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
{
"allOf": [
{
"$ref": "#/$defs/bits"
}
],
"description": "Bits"
"$ref": "#/$defs/bits"
}
],
"default": "8",
"description": "union of <enum 'bits'> and jinja representation of {typ}",
"title": "Bits"
"description": "Bits"
}
},
"required": [
Expand Down Expand Up @@ -342,24 +287,13 @@
"type": "string"
},
"DIR": {
"anyOf": [
{
"description": "jinja that renders to <enum 'DIR'>",
"pattern": ".*\\{\\{.*\\}\\}.*",
"type": "string"
},
"allOf": [
{
"allOf": [
{
"$ref": "#/$defs/DIR"
}
],
"description": "The direction of the axis"
"$ref": "#/$defs/DIR"
}
],
"default": 0,
"description": "union of <enum 'DIR'> and jinja representation of {typ}",
"title": "Dir"
"description": "The direction of the axis"
},
"VELO": {
"anyOf": [
Expand Down
Loading

0 comments on commit 93b5f59

Please sign in to comment.