diff --git a/csaxs_bec/bec_ipython_client/plugins/tool_box/spec_commands.py b/csaxs_bec/bec_ipython_client/plugins/tool_box/spec_commands.py new file mode 100644 index 00000000..563a2302 --- /dev/null +++ b/csaxs_bec/bec_ipython_client/plugins/tool_box/spec_commands.py @@ -0,0 +1,41 @@ +"""spec-style commands for the BEC IPython shell (currently: umv, umvr, mv, mvr). + + umv samx 1 samy 2 -> umv(dev.samx, 1, dev.samy, 2) + umvr samx -0.5 -> umvr(dev.samx, -0.5) + mv / mvr likewise; "dev.samx" is accepted too, commas are optional. + +Only lines that start with the command followed by whitespace and an even +number of words, every odd word being a device name, are rewritten. Anything +else (umv(...), umv = ..., umv?, expressions with spaces) passes unchanged. +""" + +import re + +_CMD = re.compile(r"^(\s*)(umvr?|mvr?)\s+(?![(=,.?])(.+?)\s*$") +_NAME = re.compile(r"^(dev\.)?[A-Za-z_][A-Za-z0-9_]*$") + + +def shell_moves(lines): + # only single-line input: never touch function bodies, loops or strings + if len(lines) != 1: + return lines + m = _CMD.match(lines[0]) + if not m: + return lines + indent, cmd, rest = m.groups() + words = rest.replace(",", " ").split() + if len(words) < 2 or len(words) % 2: + return lines + args = [] + for name, pos in zip(words[::2], words[1::2]): + if not _NAME.match(name) or pos.startswith(("=", ")")): + return lines + args += [name if name.startswith("dev.") else f"dev.{name}", pos] + return [f"{indent}{cmd}({', '.join(args)})\n"] + + +def register(ip): + """Install the transformer once (safe to call again on re-import).""" + tr = ip.input_transformers_cleanup + if not any(getattr(t, "__name__", "") == "shell_moves" for t in tr): + tr.append(shell_moves) diff --git a/csaxs_bec/bec_ipython_client/startup/post_startup.py b/csaxs_bec/bec_ipython_client/startup/post_startup.py index 3414d35b..14f18294 100644 --- a/csaxs_bec/bec_ipython_client/startup/post_startup.py +++ b/csaxs_bec/bec_ipython_client/startup/post_startup.py @@ -150,4 +150,11 @@ if _args.session.lower() == "lamni" or _args.session.lower() == "flomni" or _arg # plus the session's own states (OMNY: omny_vacuum) from csaxs_bec.bec_ipython_client.startup.beamline_states import set_default_beamline_states - set_default_beamline_states(bec, session=_args.session.lower()) \ No newline at end of file + set_default_beamline_states(bec, session=_args.session.lower()) +# SPEC-STYLE COMMANDS (umv samx 1, umvr samx -0.5, mv, mvr) +try: + from csaxs_bec.bec_ipython_client.plugins.tool_box.spec_commands import register as _register_spec_commands + + _register_spec_commands(bec._ip) +except Exception: + logger.warning("Failed to register spec-style commands.") diff --git a/tests/tests_bec_ipython_client/test_spec_commands.py b/tests/tests_bec_ipython_client/test_spec_commands.py new file mode 100644 index 00000000..6c598a1c --- /dev/null +++ b/tests/tests_bec_ipython_client/test_spec_commands.py @@ -0,0 +1,39 @@ +import pytest + +from csaxs_bec.bec_ipython_client.plugins.tool_box.spec_commands import register, shell_moves + + +@pytest.mark.parametrize( + "src, want", + [ + ("umv samx 1 samy 2\n", "umv(dev.samx, 1, dev.samy, 2)\n"), + ("umvr samx -0.5\n", "umvr(dev.samx, -0.5)\n"), + ("mv dev.samx 3, samy pos+1\n", "mv(dev.samx, 3, dev.samy, pos+1)\n"), + ("mvr lsamrot 1e-3 \n", "mvr(dev.lsamrot, 1e-3)\n"), + ("umv(dev.samx, 1)\n", None), + ("umv (dev.samx, 1)\n", None), + ("umv = 3\n", None), + ("umv?\n", None), + ("umv samx\n", None), + ("umv samx 1 + 2\n", None), + ("umv 1 samx\n", None), + ("mvx a b\n", None), + ], +) +def test_shell_moves(src, want): + assert shell_moves([src]) == [want or src] + + +def test_shell_moves_multiline_untouched(): + lines = ["for i in range(3):\n", " umv samx i\n"] + assert shell_moves(lines) == lines + + +def test_register_is_idempotent(): + class _IP: + input_transformers_cleanup = [] + + ip = _IP() + register(ip) + register(ip) + assert ip.input_transformers_cleanup == [shell_moves]