Skip to content

Commit

Permalink
add __utils__ conversion for support and ioc yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Jun 13, 2024
1 parent 3be4085 commit 6af56c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
8 changes: 3 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
{
// To use this debug configuration you must have access to dls-python
// (python 2.7) and downgrade vscode Python extension to 2021.9.1246542782
"name": "builder2ibek",
"name": "ibek2to3",
"type": "python",
"python": "/dls_sw/prod/tools/RHEL7-x86_64/defaults/bin/dls-python",
"request": "launch",
"program": "builder2ibek.support.py",
"program": "convert/ibek2to3.py",
"console": "integratedTerminal",
"args": [
"/dls_sw/prod/R3.14.12.7/support/quadEM/9-4dls1",
"../quadEM.ibek.support.yaml"
"tests/samples/support/listarg.ibek.support.yaml"
],
"justMyCode": false
},
Expand Down
30 changes: 25 additions & 5 deletions convert/ibek2to3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
"""
Converts .ibek.support.yaml files from the v2.0 format to the v3.0 format.
Can also convert ioc.yaml files as the changes re __utils__ are the same.
Changes:
- args are now params
Expand Down Expand Up @@ -35,8 +36,6 @@
import typer
from ruamel.yaml import YAML, CommentedMap

eg_file = Path(__file__).parent.parent / "tests/samples/support/ipac.ibek.support.yaml"


class ConvertedAlready(Exception):
pass
Expand All @@ -46,7 +45,7 @@ def main(files: list[Path]):
"""
Read a list of files in and process each one
"""
for f in files or [eg_file]:
for f in files:
print(f"Processing {f} ...")
process_file(f)

Expand All @@ -69,9 +68,18 @@ def tidy_up(yaml):
]:
# insert a blank line before the field unless it already has one
yaml = re.sub(r"^[ \t]*(\n%s)" % field, "\n\\g<1>", yaml)
yaml = re.sub(r"__utils__", "_global", yaml)
yaml = re.sub(r"_ctx_", "_global", yaml)
yaml = re.sub(r"\.counter", ".incrementor", yaml)
yaml = re.sub(r"\.get_var", ".get", yaml)
yaml = re.sub(r"\.setvar", ".incrementor", yaml)
return yaml


def report(message: str):
print(f">>>>>>> {message}")


def process_file(file: Path):
"""
process a single file by reading its yaml - transforming it and writing it back
Expand All @@ -81,9 +89,21 @@ def process_file(file: Path):
data = yaml.load(f)

try:
convert(data)
if data is None:
report("empty file !!")
return

if "module" in data:
report("module yaml")
convert(data)
elif "ioc_name" in data:
report("ioc yaml")
else:
report("not a supported yaml file !!")
return

except ConvertedAlready:
print(f">>>>> {file} has already been converted")
report("already converted")
else:
with open(file, "w") as f:
yaml.default_flow_style = False
Expand Down

0 comments on commit 6af56c9

Please sign in to comment.