feat(client): spec-style move commands (umv samx 1, umvr, mv, mvr) in the IPython shell
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ea5bd864f
commit
be45445870
3 files changed
+88
-1
No files matched your search
@@ -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)
|
||||
@@ -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())
|
||||
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.")
|
||||
@@ -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]
|
||||
Reference in new issue
Block a user