Skip to content

Commit fc757c8

Browse files
committed
ENH: add ADD-OR-UPDATE operation to the CLI tool
1 parent 57b8c76 commit fc757c8

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ save-and-restore --base-url=http://localhost:8080/save-restore --user-name=user
8787
--file-name=eiger_pvs.sav --file-format=autosave
8888
```
8989

90-
Update an existing configuration node named 'eiger_config'. Load the list of PVs
90+
Update the existing configuration node named 'eiger_config'. Load the list of PVs
9191
from the file ``eiger_pvs.sav``:
9292

9393
```bash
@@ -96,6 +96,15 @@ CONFIG UPDATE --config-name /detectors/imaging/eiger_config \
9696
--file-name eiger_pvs.sav --file-format autosave
9797
```
9898

99+
Add new or update the existing configuration node named 'eiger_config'. Load the list of PVs
100+
from the file ``eiger_pvs.sav``:
101+
102+
```bash
103+
save-and-restore --base-url http://localhost:8080/save-restore --user-name=user \
104+
CONFIG ADD-OR-UPDATE --config-name /detectors/imaging/eiger_config \
105+
--file-name eiger_pvs.sav --file-format autosave
106+
```
107+
99108
Print full list of options:
100109

101110
```bash

docs/source/usage.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ the path:
154154
--create-folders=ON CONFIG ADD --config-name=/detectors/imaging/eiger_config \
155155
--file-name=eiger_pvs.sav --file-format=autosave
156156
157-
Update an existing configuration node named 'eiger_config'. Load the list of PVs
157+
Update the existing configuration node named 'eiger_config'. Load the list of PVs
158158
from the file ``eiger_pvs.sav``:
159159

160160
.. code-block:: bash
@@ -163,6 +163,15 @@ from the file ``eiger_pvs.sav``:
163163
CONFIG UPDATE --config-name /detectors/imaging/eiger_config \
164164
--file-name eiger_pvs.sav --file-format autosave
165165
166+
Add new or update the existing configuration node named 'eiger_config'. Load the list of PVs
167+
from the file ``eiger_pvs.sav``:
168+
169+
.. code-block:: bash
170+
171+
save-and-restore --base-url http://localhost:8080/save-restore --user-name=user \
172+
CONFIG ADD-OR-UPDATE --config-name /detectors/imaging/eiger_config \
173+
--file-name eiger_pvs.sav --file-format autosave
174+
166175
Print full list of options:
167176

168177
.. code-block:: bash

src/save_and_restore_api/tools/cli.py

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ def formatter(prog):
131131
" --create-folders=ON CONFIG ADD --config-name=/detectors/imaging/eiger_config \\\n"
132132
" --file-name=eiger_pvs.sav --file-format=autosave\n"
133133
"\n"
134-
" Update an existing configuration node named 'eiger_config'. Load the list of PVs\n"
134+
" Update the existing configuration node named 'eiger_config'. Load the list of PVs\n"
135135
" from the file ``eiger_pvs.sav``:\n"
136136
"\n"
137137
" save-and-restore --base-url http://localhost:8080/save-restore --user-name=user \\\n"
138138
" CONFIG UPDATE --config-name /detectors/imaging/eiger_config \\\n"
139+
" --file-name eiger_pvs.sav --file-format autosave\n"
140+
"\n"
141+
" Add new or update the existing configuration node named 'eiger_config'. Load the list of PVs\n"
142+
" from the file ``eiger_pvs.sav``:\n"
143+
"\n"
144+
" save-and-restore --base-url http://localhost:8080/save-restore --user-name=user \\\n"
145+
" CONFIG ADD-OR-UPDATE --config-name /detectors/imaging/eiger_config \\\n"
139146
" --file-name eiger_pvs.sav --file-format autosave\n",
140147
formatter_class=formatter,
141148
)
@@ -301,6 +308,46 @@ def formatter(prog):
301308
"Default: '%(default)s'.",
302309
)
303310

311+
parser_config_add_or_update = subparser_config_operation.add_parser(
312+
"ADD-OR-UPDATE", help="Add a new or update the existing configuration node."
313+
)
314+
315+
parser_config_add_or_update.add_argument(
316+
"--config-name",
317+
dest="config_name",
318+
type=str,
319+
default=None,
320+
help="Configuration name including folders, e.g. /detectors/imaging/eiger_config",
321+
)
322+
323+
parser_config_add_or_update.add_argument(
324+
"--file-name",
325+
"-f",
326+
dest="file_name",
327+
type=str,
328+
default=None,
329+
help="Name of the file used as a source of PV names.",
330+
)
331+
332+
parser_config_add_or_update.add_argument(
333+
"--file-format",
334+
dest="file_format",
335+
type=str,
336+
choices=["autosave"],
337+
default="autosave",
338+
help="Format of the file specified by '--file-name'. Default: '%(default)s'",
339+
)
340+
341+
parser_config_add_or_update.add_argument(
342+
"--show-data",
343+
dest="show_data",
344+
type=str,
345+
choices=["ON", "OFF"],
346+
default="OFF",
347+
help="Print the loaded config data. The config node information is always printed. "
348+
"Default: '%(default)s'.",
349+
)
350+
304351
class ExitOnError(Exception):
305352
pass
306353

@@ -333,23 +380,7 @@ class ExitOnError(Exception):
333380
parser_config_get.print_help()
334381
raise ExitOnError()
335382

336-
elif args.operation == "ADD":
337-
settings.operation = args.operation
338-
settings.config_name = args.config_name
339-
settings.file_name = args.file_name
340-
settings.file_format = args.file_format
341-
settings.show_data = args.show_data == "ON"
342-
success = True
343-
if not settings.config_name:
344-
logger.error("Required '--config-name' parameter is not specified")
345-
success = False
346-
if not settings.file_name:
347-
logger.error("Required '--file-name' ('-f') parameter is not specified")
348-
success = False
349-
if not success:
350-
parser_config_add.print_help()
351-
raise ExitOnError()
352-
elif args.operation == "UPDATE":
383+
elif args.operation in ("ADD", "UPDATE", "ADD-OR-UPDATE"):
353384
settings.operation = args.operation
354385
settings.config_name = args.config_name
355386
settings.file_name = args.file_name
@@ -653,7 +684,7 @@ def process_config_command(settings):
653684
if settings.show_data:
654685
print(f"Config data:\n{pprint.pformat(config_data)}")
655686

656-
elif settings.operation == "ADD":
687+
elif settings.operation == "ADD" or (settings.operation == "ADD-OR-UPDATE" and not node_uid):
657688
logger.debug("Executing 'CONFIG ADD' operation ...")
658689
if node_uid:
659690
raise RuntimeError(f"Config node {settings.config_name!r} already exists.")
@@ -686,8 +717,8 @@ def process_config_command(settings):
686717
if settings.show_data:
687718
print(f"Config data:\n{pprint.pformat(response['configurationData'])}")
688719

689-
elif settings.operation == "UPDATE":
690-
logger.debug("Executing 'CONFIG UPDATE' operation ...")
720+
elif settings.operation == "UPDATE" or (settings.operation == "ADD-OR-UPDATE" and node_uid):
721+
logger.debug(f"Executing 'CONFIG {settings.operation}' operation ...")
691722
if not node_uid:
692723
raise RuntimeError(f"Config node {settings.config_name!r} does not exist.")
693724

0 commit comments

Comments
 (0)