Skip to content

Commit

Permalink
Merge pull request #774 from dougthor42/773-fix-custom-sim-file
Browse files Browse the repository at this point in the history
Add support for custom simulated file backends in shell
  • Loading branch information
MatthieuDartiailh committed Oct 18, 2023
2 parents 088f591 + 9a3056c commit a9507e5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ PyVISA Changelog
- `p.u.get_shared_library_arch` now returns `p.u.PEMachineType` instead of a string
- `p.u.get_arch` now returns a list of `p.u.ArchitectureType`
- update `Resource` context manager type annotation to preserve derived type PR # 740
- added support for using custom simulation files when using the `sim` backend
with `pyvisa-shell`. PR #774

1.13.0 (22-12-2022)
-------------------
Expand Down
4 changes: 4 additions & 0 deletions docs/source/introduction/shell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ You can invoke::
to use python-sim as backend instead of ni backend.
This can be used for example for testing of python-sim configuration.

You can include a specific file to use for the simulated backend::

pyvisa-shell -b file.yaml@sim

You can invoke::

pyvisa-shell -b py
Expand Down
17 changes: 16 additions & 1 deletion pyvisa/cmd_line_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:license: MIT, see LICENSE for more details.
"""
import argparse
from typing import Optional


Expand Down Expand Up @@ -50,13 +51,27 @@ def visa_main(command: Optional[str] = None) -> None:
elif args.command == "shell":
from pyvisa import shell

shell.main("@" + args.backend if args.backend else "")
backend = _create_backend_str(args)
shell.main(backend)
else:
raise ValueError(
f"Unknown command {args.command}. Valid values are: info and shell"
)


def _create_backend_str(args: argparse.Namespace) -> str:
"""Create the backend string from the CLI arguments."""
if not args.backend:
return ""

# User already entered things correctly like "@py" or "file.yaml@sim"
if "@" in args.backend:
return args.backend

# Keep the current functionality
return "@" + args.backend


def visa_shell() -> None:
"""Run the VISA shell CLI program."""
visa_main("shell")
Expand Down
18 changes: 17 additions & 1 deletion pyvisa/testsuite/test_cmd_line_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"""Test the behavior of the command line tools.
"""
import argparse
import sys
from subprocess import PIPE, Popen, run

import pytest

from pyvisa import util
from pyvisa import cmd_line_tools, util

from . import BaseTestCase, require_visa_lib

Expand Down Expand Up @@ -41,3 +42,18 @@ def test_visa_shell(self):
with Popen(["pyvisa-shell"], stdin=PIPE, stdout=PIPE) as p:
stdout, stderr = p.communicate(b"exit")
assert stdout.count(b"Welcome to the VISA shell") == 1


@pytest.mark.parametrize(
"args, want",
[
(argparse.Namespace(backend=None), ""),
(argparse.Namespace(backend="py"), "@py"),
(argparse.Namespace(backend="foo.yaml@sim"), "foo.yaml@sim"),
(argparse.Namespace(backend="/foo/bar/baz.yaml@sim"), "/foo/bar/baz.yaml@sim"),
(argparse.Namespace(backend="@sim"), "@sim"),
],
)
def test__create_backend_str(args: argparse.Namespace, want: str) -> None:
got = cmd_line_tools._create_backend_str(args)
assert got == want

0 comments on commit a9507e5

Please sign in to comment.