From ef68245d413073dbf97618ba72ed4b8d7c35eccb Mon Sep 17 00:00:00 2001 From: Gavin Ying Date: Thu, 12 Nov 2026 23:03:58 +0800 Subject: [PATCH] refactor: -f/--config is the single configuration flag Remove the --tm alias and the --thing-model/--thing-description kind-assertion flags added with the CSV restoration: --config detects v1 CSV, WoT TM/TD JSON and modiohub: Workspace references from the content, so one flag covers every source and docs, examples and web commands all speak --config. The removed spellings are rejected as unrecognized arguments; load_devices keeps its optional kind assertion as an internal API. Usage errors keep the one-line exit-code-2 style; --full-read still rejects CSV configurations. --- README.md | 7 ++-- docs/preview-notes.md | 2 +- docs/site/configure.rst | 12 +++---- docs/site/usage.rst | 3 +- docs/site/v1-compatibility.rst | 4 +-- docs/validation.rst | 12 +++---- modpoll/arg_parser.py | 25 ------------- modpoll/config_loader.py | 8 ++--- modpoll/modbus_task.py | 17 +-------- modpoll/verification.py | 8 +---- tests/test_cli_errors.py | 64 +++++++++++----------------------- tests/test_csv_config.py | 4 +-- tests/test_login.py | 9 ++++- tests/test_verification.py | 2 +- tests/test_wot_config.py | 17 --------- 15 files changed, 56 insertions(+), 138 deletions(-) diff --git a/README.md b/README.md index b68b4c0..d458386 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,7 @@ The configuration loader detects the format from the content and reports invalid model fields before polling. Each model describes one device; use multiple `--config` paths for multiple devices. Give each model a unique `title`, which is the output device name. Existing v1 CSV configurations load through the same -`--config` argument; use `--thing-model` or -`--thing-description`/`--td` to assert the JSON document kind explicitly. +`--config` argument; no separate flags are needed. Physical RS485 polling has also been verified by the maintainer with an Eastron meter (2026-10-28). See [validation](docs/validation.rst) for the evidence scope and @@ -138,12 +137,12 @@ signing in with either tool signs in both. `MODIOHUB_TOKEN` in the environment r `MODIOHUB_URL` selects another server, and `login` and `logout` take `--server URL` for the same. `modpoll logout` forgets the login on this computer; revoke the session in your ModioHub settings to end it on the server. -Once signed in, `--tm modiohub:@` (`-f` works too; the version is a number or `latest`) reads one +Once signed in, `--config modiohub:@` (the version is a number or `latest`) reads one of your custom Thing Models. A shared one needs no login. A full read of it writes a report that names the Workspace model, and `--upload` shows the report and, after you confirm (or with `--yes`), uploads it: ```sh -modpoll --tm modiohub:k3x9f2m7q4a8n5bd@latest --tcp 192.168.1.20 --upload +modpoll --config modiohub:k3x9f2m7q4a8n5bd@latest --tcp 192.168.1.20 --upload ``` ### MQTT data and write behavior diff --git a/docs/preview-notes.md b/docs/preview-notes.md index 6ff2b96..7d8637e 100644 --- a/docs/preview-notes.md +++ b/docs/preview-notes.md @@ -7,7 +7,7 @@ Modpoll 2.0.0a7 pins modpoll-wot 0.5.0a1 and connects to ModioHub: verification report (schema 3), which holds no address, Unit ID, serial number or value. - `modpoll login` signs in with your browser and `modpoll logout` forgets the login. The 7-day tool session is kept in `~/.config/modiohub/credentials.json` and is shared with modsim 2.0.0a9. -- `--tm modiohub:@` reads a custom Thing Model from your ModioHub Workspace, and `--upload` shows +- `--config modiohub:@` reads a custom Thing Model from your ModioHub Workspace, and `--upload` shows the report of a full read and uploads it after you confirm. ## Modpoll 2.0.0a6 preview diff --git a/docs/site/configure.rst b/docs/site/configure.rst index c035a42..9ddc3c4 100644 --- a/docs/site/configure.rst +++ b/docs/site/configure.rst @@ -2,16 +2,15 @@ CSV and WoT TM/TD configuration =============================== The 2.x development branch accepts both the v1 CSV configuration format and -JSON configurations using WoT TM/TD 1.1. ``-f``/``--config`` (alias ``--tm``) +JSON configurations using WoT TM/TD 1.1. ``-f``/``--config`` takes either format and detects it from the content, not the file name or URL suffix: a first non-whitespace byte of ``{`` or ``[`` selects JSON; anything else is parsed as v1 CSV. The same argument also accepts a ``modiohub:@`` reference to a custom Thing Model in your ModioHub Workspace (run ``modpoll login`` first unless the version is shared); a reference is always a Thing Model. -``--thing-model`` and ``--thing-description``/``--td`` assert -the JSON document kind explicitly and reject content of the other kind (or a -CSV file) with a configuration error; they take local paths or URLs only. +``--config`` is the only configuration argument; there are no format-specific +flags. For JSON input, a top-level ``@type`` containing ``tm:ThingModel`` (a string or an array entry) selects TM validation; otherwise the document is validated as @@ -34,9 +33,8 @@ Each file describes one device. Its ``title`` is the output device name; ``--unit-id`` overrides the instance Unit ID (range 1–254). Without it, each file uses the Unit ID declared consistently in its Forms, falling back to 1. Different files can therefore address different Unit IDs in one process. -Multiple files may be supplied per flag, and the three flags may be combined; -files load in ``--config``, ``--thing-model``, ``--thing-description`` order. -Device names must be unique across all files. +Multiple files may be supplied to ``--config``; device names must be unique +across all files. The existing transport CLI arguments explicitly select the connection endpoint. A concrete TD's form Unit IDs, addresses and quantities must agree with the model. diff --git a/docs/site/usage.rst b/docs/site/usage.rst index 2b2a48c..d55b2d8 100644 --- a/docs/site/usage.rst +++ b/docs/site/usage.rst @@ -74,8 +74,7 @@ Configuration File The configuration file (`--config`) defines the devices, pollers, and points to be read. It can be a WoT TM/TD JSON file or a v1 CSV file; the format is -detected from the content. Use `--thing-model` or -`--thing-description`/`--td` to assert the JSON document kind explicitly. +detected from the content, no other configuration flag is needed. For register references (i.e., Holding or Input registers) with a ``dtype`` of ``bool``, you can specify a single bit to be extracted from the 16-bit register. This is done by appending ``:bit`` to the address, where ``bit`` is an integer from 0 to 15. diff --git a/docs/site/v1-compatibility.rst b/docs/site/v1-compatibility.rst index fe1506d..51b55ff 100644 --- a/docs/site/v1-compatibility.rst +++ b/docs/site/v1-compatibility.rst @@ -2,8 +2,8 @@ v1 compatibility and M3 acceptance =================================== The v2 change adds the executable TM/TD profile alongside the retained v1 CSV -loader. ``--config`` detects the format from the content; -``--thing-model``/``--thing-description`` assert the JSON document kind. The +loader. ``--config`` is the single configuration argument and detects the +format from the content. The existing transport, decoder, output and raw write paths remain. This table compares the pinned v1 baseline ``0b42099`` with the v2 implementation; it is not a claim that all WoT documents or every operating system has been diff --git a/docs/validation.rst b/docs/validation.rst index a7aba61..5d332b2 100644 --- a/docs/validation.rst +++ b/docs/validation.rst @@ -142,12 +142,12 @@ v1 CSV compatibility restoration -------------------------------- On 2026-11-12 the v1 CSV loader was restored behind content detection: -``-f``/``--config`` (alias ``--tm``) accepts v1 CSV and WoT TM/TD JSON side by -side, and the new -``--thing-model``/``--thing-description`` flags assert the JSON document kind -(``--tm`` had just been taken by the POLL-02b alias for ``--config``, which -also accepts ``modiohub:`` Workspace references, so the asserting flag is the -long ``--thing-model`` only). A full read stays Thing-Model-only: CSV +``-f``/``--config`` accepts v1 CSV and WoT TM/TD JSON side by +side; ``--config`` is the only configuration argument (the ``--tm`` alias and +the ``--thing-model``/``--thing-description`` kind-assertion flags existed +only between the restoration merge and this cleanup, and are rejected as +unrecognized to keep one obvious way to load a configuration). A full read +stays Thing-Model-only: CSV configurations are rejected with ``--full-read``. After merging the POLL-02a/02b release into the branch, ``make check`` passed and the offline suite passed 243 tests with 58 diff --git a/modpoll/arg_parser.py b/modpoll/arg_parser.py index 3f170c6..d8aede5 100644 --- a/modpoll/arg_parser.py +++ b/modpoll/arg_parser.py @@ -25,7 +25,6 @@ def get_parser(): parser.add_argument( "-f", "--config", - "--tm", dest="config", default=None, help=( @@ -37,30 +36,6 @@ def get_parser(): ), nargs="+", ) - parser.add_argument( - "--thing-model", - dest="thing_model", - default=[], - help=( - "Local path or URL of a WoT Thing Model JSON configuration " - "(tm:ThingModel); asserts the document kind instead of detecting it. " - "Not for modiohub: Workspace references; use --config/--tm for those." - ), - nargs="+", - metavar="PATH_OR_URL", - ) - parser.add_argument( - "--thing-description", - "--td", - dest="thing_description", - default=[], - help=( - "Local path or URL of a WoT Thing Description JSON configuration; " - "asserts the document kind instead of detecting it" - ), - nargs="+", - metavar="PATH_OR_URL", - ) parser.add_argument( "--device", metavar="MANUFACTURER/MODEL[@COMMIT]", diff --git a/modpoll/config_loader.py b/modpoll/config_loader.py index bc92ba5..66dc056 100644 --- a/modpoll/config_loader.py +++ b/modpoll/config_loader.py @@ -3,8 +3,8 @@ ``--config`` accepts the v1 CSV format and WoT TM/TD JSON side by side; the format is detected from the content, not the file name or URL suffix. A first non-whitespace byte of ``{`` or ``[`` selects JSON; anything else is parsed as -v1 CSV. ``--thing-model`` and ``--thing-description`` assert the JSON document -kind instead of relying on detection. +v1 CSV. ``kind`` lets a caller assert the JSON document kind; the CLI relies +on detection instead. """ from modpoll_wot import AcquisitionError, loads_model @@ -60,9 +60,9 @@ def load_devices( ) return create_devices(model), model if kind is not None: - flag = "--thing-model" if kind == "tm" else "--thing-description" + document = "Thing Model" if kind == "tm" else "Thing Description" raise ConfigUsageError( - f"{location}: {flag} expects a TM/TD JSON document, " + f"{location}: expected a {document} JSON document, " "but the content is not JSON" ) if parameters is not None: diff --git a/modpoll/modbus_task.py b/modpoll/modbus_task.py index 37264fa..45ca565 100644 --- a/modpoll/modbus_task.py +++ b/modpoll/modbus_task.py @@ -537,12 +537,10 @@ def __init__( instance_port: Optional[int] = None, instance_parameters=None, precision: Optional[int] = None, - config_kind: Optional[str] = None, model_bytes: Optional[bytes] = None, ): self.modbus_client = modbus_client self.config_file = config_file - self.config_kind = config_kind self.unit_id = unit_id self.instance_host = instance_host self.instance_port = instance_port @@ -578,7 +576,6 @@ def load_config(self) -> bool: self.model_bytes = read_payload(self.config_file, timeout=self.timeout) self.deviceList, self.model = load_devices( self.config_file, - kind=self.config_kind, payload=self.model_bytes, timeout=self.timeout, unit_id=self.unit_id, @@ -878,18 +875,7 @@ def setup_modbus_handlers(args, mqtt_handler: Optional[MqttHandler] = None): raise ValueError("TM parameters require the profile 0.2 interpreter") parameters = modpoll_wot.parse_parameters(args.param) try: - config_specs = ( - [(location, None) for location in (args.config or [])] - + [ - (location, "tm") - for location in (getattr(args, "thing_model", None) or []) - ] - + [ - (location, "td") - for location in (getattr(args, "thing_description", None) or []) - ] - ) - for config_file, config_kind in config_specs: + for config_file in args.config or []: current_handler = ModbusHandler( None, config_file, @@ -906,7 +892,6 @@ def setup_modbus_handlers(args, mqtt_handler: Optional[MqttHandler] = None): instance_port=args.tcp_port, instance_parameters=parameters, precision=parse_precision(getattr(args, "precision", None)), - config_kind=config_kind, model_bytes=models.get(config_file), ) if current_handler.load_config(): diff --git a/modpoll/verification.py b/modpoll/verification.py index de968b6..19b7719 100644 --- a/modpoll/verification.py +++ b/modpoll/verification.py @@ -43,14 +43,10 @@ def validate_args(args): """Reject option combinations a full read cannot honour, before anything is connected.""" args.full_read = args.full_read or bool(args.report) or args.upload sources = list(args.config or []) - sources += list(getattr(args, "thing_model", None) or []) - sources += list(getattr(args, "thing_description", None) or []) if args.device and sources: raise ValueError("Use either --device or a configuration source, not both") if not args.device and not sources: - raise ValueError( - "One of -f/--tm, --thing-model, --thing-description or --device is required" - ) + raise ValueError("One of --config/-f or --device is required") if args.device: parse_device(args.device) if not args.full_read: @@ -58,8 +54,6 @@ def validate_args(args): if getattr(args, option): raise ValueError(f"--{option} needs --full-read or --report") return - if args.thing_description: - raise ValueError("A full read reads a Thing Model, not a Thing Description") if args.mqtt_host: raise ValueError("A full read does not publish to MQTT") if args.udp: diff --git a/tests/test_cli_errors.py b/tests/test_cli_errors.py index 7e8f9d8..6a30b60 100644 --- a/tests/test_cli_errors.py +++ b/tests/test_cli_errors.py @@ -36,57 +36,35 @@ def test_configuration_errors_exit_2_without_a_traceback(args, message): def test_missing_configuration_exits_2(): result = run("--once") assert result.returncode == 2 - assert ( - "One of -f/--tm, --thing-model, --thing-description or --device is required" - in result.stderr + result.stdout + assert "One of --config/-f or --device is required" in ( + result.stderr + result.stdout ) assert "Traceback" not in result.stderr + result.stdout -@pytest.mark.parametrize( - "flag, config, extra", - [ - ("--tm", "examples/modsim.tm.json", []), - ("--thing-model", "examples/modsim.tm.json", []), +def test_config_accepts_csv_tm_and_td_by_content_detection(): + cases = [ + (["--config", "examples/modsim.csv"], "No communication method specified"), + (["-f", "examples/modsim.tm.json"], "No communication method specified"), # A bound TD supplies its own TCP endpoint, so --udp exercises the # unbound-TM guard instead of opening a connection. - ("--td", "examples/modsim.generated.td.json", ["--udp", "127.0.0.1"]), ( - "--thing-description", - "examples/modsim.generated.td.json", - ["--udp", "127.0.0.1"], + ["--config", "examples/modsim.generated.td.json", "--udp", "127.0.0.1"], + "unbound TM", ), - ], -) -def test_thing_model_and_description_flags_accept_json(flag, config, extra): - result = run(flag, config, "--once", *extra) - output = result.stderr + result.stdout - assert result.returncode == 2 - assert ( - "No communication method specified" in output or "unbound TM" in output - ), output - - -@pytest.mark.parametrize( - "flag, config", - [ - # --tm is the --config alias (local path, URL or modiohub reference); - # v1 CSV is detected from the content there. - ("--tm", "examples/modsim.csv"), - ("-f", "examples/modsim.csv"), - ], -) -def test_config_alias_accepts_csv_by_content_detection(flag, config): - result = run(flag, config, "--once") - assert result.returncode == 2 - assert "No communication method specified" in result.stderr + result.stdout - - -def test_thing_model_flag_rejects_csv_content(): - result = run("--thing-model", "examples/modsim.csv", "--tcp", "127.0.0.1", "--once") - assert result.returncode == 2 - assert "expects a TM/TD JSON document" in result.stderr + result.stdout - assert "Traceback" not in result.stderr + result.stdout + ] + for argv, message in cases: + result = run(*argv, "--once") + output = result.stderr + result.stdout + assert result.returncode == 2, output + assert message in output, output + + +def test_removed_flag_aliases_are_rejected(): + for flag in ("--tm", "--thing-model", "--thing-description", "--td"): + result = run(flag, "examples/modsim.tm.json", "--once") + assert result.returncode == 2, flag + assert "unrecognized arguments" in result.stderr + result.stdout def test_csv_rejects_typed_parameters(): diff --git a/tests/test_csv_config.py b/tests/test_csv_config.py index 6bd5ad6..7ad1084 100644 --- a/tests/test_csv_config.py +++ b/tests/test_csv_config.py @@ -109,12 +109,12 @@ def test_format_is_detected_from_content_not_filename(tmp_path): assert model is not None -def test_thing_model_and_description_assert_the_document_kind(): +def test_document_kind_assertion_at_loader_level(): with pytest.raises(ValueError, match="document kind disagrees"): load_devices(str(MODSIM_TD), kind="tm", timeout=1) with pytest.raises(ValueError, match="document kind disagrees"): load_devices(str(MODSIM_TM), kind="td", timeout=1) - with pytest.raises(ValueError, match="expects a TM/TD JSON document"): + with pytest.raises(ValueError, match="expected a Thing Model JSON document"): load_devices(str(MODSIM_CSV), kind="tm", timeout=1) (_,), model = load_devices(str(MODSIM_TM), kind="tm", timeout=1) assert model is not None diff --git a/tests/test_login.py b/tests/test_login.py index b0a0e84..96448f4 100644 --- a/tests/test_login.py +++ b/tests/test_login.py @@ -230,7 +230,14 @@ def test_full_read_of_a_workspace_model_names_the_workspace_as_its_source( with pytest.raises(SystemExit): run_app( monkeypatch, - ["--tm", f"modiohub:{ID}@2", "--tcp", "x", "--report", str(report_path)], + [ + "--config", + f"modiohub:{ID}@2", + "--tcp", + "x", + "--report", + str(report_path), + ], FakeClient(), ) report = json.loads(report_path.read_text()) diff --git a/tests/test_verification.py b/tests/test_verification.py index 485a6bf..e01625f 100644 --- a/tests/test_verification.py +++ b/tests/test_verification.py @@ -232,7 +232,7 @@ def test_a_point_that_was_not_read_cannot_be_judged(monkeypatch, tmp_path): ), ( [], - "One of -f/--tm, --thing-model, --thing-description or --device is required", + "One of --config/-f or --device is required", ), (["--device", "Bad/Id"], "--device must be"), (["-f", "x.json", "--report", "r.json", "--tcp", "x"], "needs --product"), diff --git a/tests/test_wot_config.py b/tests/test_wot_config.py index 045266d..ad67941 100644 --- a/tests/test_wot_config.py +++ b/tests/test_wot_config.py @@ -21,23 +21,6 @@ def test_csv_content_loads_as_v1_configuration(tmp_path): assert handler.model is None -def test_thing_model_flag_rejects_wrong_document_kind(tmp_path): - path = tmp_path / "model.td.json" - path.write_text( - json.dumps( - { - "@context": ["https://www.w3.org/2022/wot/td/v1.1"], - "@type": "tm:ThingDescription", - "title": "meter", - } - ) - ) - handler = ModbusHandler(object(), str(path), config_kind="tm") - with pytest.raises(ValueError, match="document kind disagrees"): - handler.load_config() - assert handler.deviceList == [] - - def test_url_model_loader_keeps_timeout_and_streams(monkeypatch): from modpoll import wot_config