Skip to content

Commit

Permalink
add type text to pre_init,post_init
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Jul 6, 2023
1 parent 0085a59 commit 8f2aace
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ibek-defs
12 changes: 7 additions & 5 deletions src/ibek/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Functions for rendering lines in the boot script using Jinja2
"""

from typing import Callable, List, Optional, Sequence, Union
from typing import Callable, List, Optional, Union

from jinja2 import Template

from .ioc import IOC, Entity
from .support import Comment, Function, When
from .support import Comment, Function, Script, Text, When


class Render:
Expand Down Expand Up @@ -79,9 +79,7 @@ def render_function(self, instance: Entity, function: Function) -> str:

return text

def render_script(
self, instance: Entity, script_items: Sequence[Union[Function, Comment]]
) -> Optional[str]:
def render_script(self, instance: Entity, script_items: Script) -> Optional[str]:
script = ""

for item in script_items:
Expand All @@ -90,6 +88,10 @@ def render_script(
script += self.render_text(
instance, comments, item.when, suffix="comment"
)
elif isinstance(item, Text):
script += self.render_text(
instance, item.value, item.when, suffix="text"
)
elif isinstance(item, Function):
script += self.render_function(instance, item)

Expand Down
22 changes: 19 additions & 3 deletions src/ibek/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class Function:
@dataclass
class Comment:
"""
A script snippet that should for the first occurrence only
A script snippet that will have '# ' prepended to every line
for insertion into the startup script
"""

type: Literal["comment"] = "comment"
Expand All @@ -152,6 +153,18 @@ class Comment:
value: A[str, desc("A comment to add into the startup script")] = ""


@dataclass
class Text:
"""
A script snippet to insert into the startup script
"""

type: Literal["text"] = "text"
# TODO will be an enum
when: A[str, desc("One of first / every / last")] = "every"
value: A[str, desc("raw text to add to the startup script")] = ""


@dataclass
class Value:
"""A calculated string value for a definition"""
Expand All @@ -164,6 +177,9 @@ def __str__(self):
return self.value


Script = Sequence[Union[Function, Comment, Text]]


@dataclass
class Definition:
"""
Expand All @@ -176,11 +192,11 @@ class Definition:
values: A[Sequence[Value], desc("The values IOC instance should supply")] = ()
databases: A[Sequence[Database], desc("Databases to instantiate")] = ()
pre_init: A[
Sequence[Union[Function, Comment]],
Script,
desc("Startup script snippets to add before iocInit()"),
] = ()
post_init: A[
Sequence[Union[Function, Comment]],
Script,
desc("Startup script snippets to add post iocInit(), such as dbpf"),
] = ()
env_vars: A[
Expand Down
1 change: 1 addition & 0 deletions tests/samples/boot_scripts/test.ioc.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ epicsEnvSet EPICS_CA_SERVER_PORT 7064
dbLoadDatabase dbd/ioc.dbd
ioc_registerRecordDeviceDriver pdbbase

dbLoadRecords("config/ioc.db")

dbLoadRecords /tmp/ioc.db
iocInit
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/example-ibek-config/ioc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ entities:
- type: devIocStats.IocAdminSoft
ioc: test-ibek-ioc

# - type: epics.StartupCommand
# command: dbLoadRecords("config/ioc.db")
- type: epics.StartupCommand
command: dbLoadRecords("config/ioc.db")
40 changes: 40 additions & 0 deletions tests/samples/schemas/all.ibek.support.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,46 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.StartupCommand",
"default": "epics.StartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.PostStartupCommand",
"default": "epics.PostStartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down
40 changes: 40 additions & 0 deletions tests/samples/schemas/bl45p-mo-ioc-04.ibek.entities.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.StartupCommand",
"default": "epics.StartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.PostStartupCommand",
"default": "epics.PostStartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down
40 changes: 40 additions & 0 deletions tests/samples/schemas/epics.ibek.support.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.StartupCommand",
"default": "epics.StartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"entity_enabled": {
"type": "boolean",
"default": true
},
"command": {
"type": "string",
"description": "command string",
"default": ""
},
"type": {
"type": "string",
"const": "epics.PostStartupCommand",
"default": "epics.PostStartupCommand"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down
27 changes: 27 additions & 0 deletions tests/samples/schemas/ibek.defs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@
},
{
"$ref": "#/definitions/Comment"
},
{
"$ref": "#/definitions/Text"
}
]
},
Expand All @@ -288,6 +291,9 @@
},
{
"$ref": "#/definitions/Comment"
},
{
"$ref": "#/definitions/Text"
}
]
},
Expand Down Expand Up @@ -386,6 +392,27 @@
}
},
"additionalProperties": false
},
"Text": {
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "text",
"default": "text"
},
"when": {
"type": "string",
"description": "One of first / every / last",
"default": "every"
},
"value": {
"type": "string",
"description": "raw text to add to the startup script",
"default": ""
}
},
"additionalProperties": false
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
Expand Down

0 comments on commit 8f2aace

Please sign in to comment.