Skip to content

Commit

Permalink
make args and defines lists into dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Jun 12, 2024
1 parent 7d3bf68 commit 595665a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 101 deletions.
Empty file added convert/ibek2to3.py
Empty file.
5 changes: 1 addition & 4 deletions src/ibek/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __repr__(self):
class Value(BaseSettings):
"""A calculated string value for a definition"""

name: str = Field(description="Name of the value that the IOC instance will expose")
description: str = Field(
description="Description of what the value will be used for"
)
Expand All @@ -50,9 +49,7 @@ class Arg(BaseSettings):
"""Base class for all Argument Types"""

type: str
name: str = Field(
description="Name of the argument that the IOC instance should pass"
)

description: str = Field(
description="Description of what the argument will be used for"
)
Expand Down
6 changes: 3 additions & 3 deletions src/ibek/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ class EntityDefinition(BaseSettings):
)
# declare Arg as Union of its subclasses for Pydantic to be able to deserialize

args: Sequence[discriminated] = Field( # type: ignore
args: dict[str, discriminated] = Field( # type: ignore
description="The arguments IOC instance should supply",
default=(),
)
post_defines: Sequence[Value] = Field(
post_defines: dict[str, Value] = Field(
description="Calculated values to use as additional arguments "
"With Jinja evaluation after all Args",
default=(),
)
pre_defines: Sequence[Value] = Field(
pre_defines: dict[str, Value] = Field(
description="Calculated values to use as additional arguments "
"With Jinja evaluation before all Args",
default=(),
Expand Down
13 changes: 6 additions & 7 deletions src/ibek/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def _make_entity_model(
Create an Entity Model from a Definition instance and a Support instance.
"""

def add_defines(s: Sequence[Value]) -> None:
def add_defines(s: dict[str, Value]) -> None:
# add in the pre_defines or post_defines as Args in the Entity
for value in s:
for name, value in s.items():
typ = getattr(builtins, str(value.type))
add_arg(value.name, typ, value.description, value.value)
add_arg(name, typ, value.description, value.value)

def add_arg(name, typ, description, default):
if default is None:
Expand Down Expand Up @@ -105,7 +105,7 @@ def add_arg(name, typ, description, default):
add_defines(definition.pre_defines)

# add in each of the arguments as a Field in the Entity
for arg in definition.args:
for name, arg in definition.args.items():
type: Any

if isinstance(arg, ObjectArg):
Expand All @@ -123,14 +123,13 @@ def add_arg(name, typ, description, default):
for k, v in arg.values.items():
enum_swapped[str(v) if v else str(k)] = k
# TODO review enums especially with respect to Pydantic 2.7.1
val_enum = EnumVal(arg.name, enum_swapped) # type: ignore
val_enum = EnumVal(name, enum_swapped) # type: ignore
type = val_enum

else:
# arg.type is str, int, float, etc.
type = getattr(builtins, arg.type)
# TODO look into why arg.name requires type ignore
add_arg(arg.name, type, arg.description, getattr(arg, "default")) # type: ignore
add_arg(name, type, arg.description, getattr(arg, "default"))

# add in the calculated values Jinja Templates as Fields in the Entity
add_defines(definition.post_defines)
Expand Down
10 changes: 6 additions & 4 deletions tests/samples/outputs/listarg/st.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ dbLoadDatabase dbd/ioc.dbd
ioc_registerRecordDeviceDriver pdbbase



Lister, age 3000029, who does not like to hear "Smoke me a kipper, I'll be back for breakfast" has friends:
Lister, age 3000029, who does not like to hear
"Smoke me a kipper, I'll be back for breakfast"
has friends-
o Rimmer
o Cat
o Kryten
Expand All @@ -16,8 +17,9 @@ First friend is Rimmer
Time vortex friends are ['Cat', 'Cat', 'Cat', 'Holly', 'Holly', 'Holly', 'Kryten', 'Kryten', 'Kryten', 'Rimmer', 'Rimmer', 'Rimmer']
Fist time vortex friend is Cat


Lister, age 42, who does not like to hear "Smoke me a kipper, I'll be back for breakfast" has friends:
Lister, age 42, who does not like to hear
"Smoke me a kipper, I'll be back for breakfast"
has friends-
o Rimmer
o Cat
o Kryten
Expand Down
74 changes: 13 additions & 61 deletions tests/samples/schemas/ibek.support.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -42,7 +37,6 @@
}
},
"required": [
"name",
"description"
],
"title": "BoolArg",
Expand Down Expand Up @@ -133,9 +127,7 @@
"type": "string"
},
"args": {
"default": [],
"description": "The arguments IOC instance should supply",
"items": {
"additionalProperties": {
"description": "union of arg types",
"discriminator": {
"mapping": {
Expand Down Expand Up @@ -173,26 +165,28 @@
}
]
},
"default": [],
"description": "The arguments IOC instance should supply",
"title": "Args",
"type": "array"
"type": "object"
},
"post_defines": {
"default": [],
"description": "Calculated values to use as additional arguments With Jinja evaluation after all Args",
"items": {
"additionalProperties": {
"$ref": "#/$defs/Value"
},
"default": [],
"description": "Calculated values to use as additional arguments With Jinja evaluation after all Args",
"title": "Post Defines",
"type": "array"
"type": "object"
},
"pre_defines": {
"default": [],
"description": "Calculated values to use as additional arguments With Jinja evaluation before all Args",
"items": {
"additionalProperties": {
"$ref": "#/$defs/Value"
},
"default": [],
"description": "Calculated values to use as additional arguments With Jinja evaluation before all Args",
"title": "Pre Defines",
"type": "array"
"type": "object"
},
"databases": {
"default": [],
Expand Down Expand Up @@ -343,11 +337,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -370,7 +359,6 @@
}
},
"required": [
"name",
"description",
"values"
],
Expand Down Expand Up @@ -412,11 +400,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -441,7 +424,6 @@
}
},
"required": [
"name",
"description"
],
"title": "FloatArg",
Expand All @@ -460,11 +442,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -484,7 +461,6 @@
}
},
"required": [
"name",
"description"
],
"title": "IdArg",
Expand All @@ -503,11 +479,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -532,7 +503,6 @@
}
},
"required": [
"name",
"description"
],
"title": "IntArg",
Expand All @@ -551,11 +521,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -576,7 +541,6 @@
}
},
"required": [
"name",
"description"
],
"title": "ObjectArg",
Expand All @@ -595,11 +559,6 @@
"title": "Type",
"type": "string"
},
"name": {
"description": "Name of the argument that the IOC instance should pass",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the argument will be used for",
"title": "Description",
Expand All @@ -619,7 +578,6 @@
}
},
"required": [
"name",
"description"
],
"title": "StrArg",
Expand Down Expand Up @@ -680,11 +638,6 @@
"additionalProperties": false,
"description": "A calculated string value for a definition",
"properties": {
"name": {
"description": "Name of the value that the IOC instance will expose",
"title": "Name",
"type": "string"
},
"description": {
"description": "Description of what the value will be used for",
"title": "Description",
Expand All @@ -705,7 +658,6 @@
}
},
"required": [
"name",
"description",
"value"
],
Expand Down Expand Up @@ -764,4 +716,4 @@
],
"title": "Support",
"type": "object"
}
}
Loading

0 comments on commit 595665a

Please sign in to comment.