From 295a14091871860e779a5062e92160150badb0cc Mon Sep 17 00:00:00 2001 From: Nicholas Felt Date: Mon, 16 Oct 2023 12:46:42 -0700 Subject: [PATCH] Added a command-line script to print the available VISA resources. (#26) --- CHANGELOG.md | 8 ++++++++ README.rst | 14 ++++++++++++++ docs/basic_usage.md | 12 ++++++++++++ pyproject.toml | 3 +++ src/tm_devices/__init__.py | 3 ++- src/tm_devices/device_manager.py | 8 ++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b05fd9..1335c5ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ project adheres to [Semantic Versioning](https://semver.org). ______________________________________________________________________ +## Unreleased + +### Added + +- A command-line script to list all available VISA resources, `list-visa-resources` + +______________________________________________________________________ + ## v0.1.14 (2023-10-03) ### Added diff --git a/README.rst b/README.rst index e5929e4d..3152b06c 100644 --- a/README.rst +++ b/README.rst @@ -133,6 +133,20 @@ Installation Basic Usage ----------- +Print Available VISA Devices +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: console + + $ list-visa-resources + [ + "TCPIP0::192.168.0.100::inst0::INSTR", + "ASRL4::INSTR" + ] + +Basic Script +~~~~~~~~~~~~ + .. code-block:: python from tm_devices import DeviceManager diff --git a/docs/basic_usage.md b/docs/basic_usage.md index f29ff509..fcfaf591 100644 --- a/docs/basic_usage.md +++ b/docs/basic_usage.md @@ -3,6 +3,18 @@ A collection of examples showing the basics of how to use `tm_devices` in a project. +## List available VISA devices + +This will print the available VISA devices to the console when run from a shell terminal. + +```console +> list-visa-resources +[ + "TCPIP0::192.168.0.100::inst0::INSTR", + "ASRL4::INSTR" +] +``` + ## Adding devices ```{literalinclude} ../examples/miscellaneous/adding_devices.py diff --git a/pyproject.toml b/pyproject.toml index c9f285ba..fa4b10eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -143,6 +143,9 @@ types-requests = ">=2.28.8" urllib3 = ">=1.26.14" wheel = ">=0.37.1" +[tool.poetry.scripts] +list-visa-resources = "tm_devices:print_available_visa_devices" + [tool.poetry.urls] "Bug Tracker" = "https://github.com/tektronix/tm_devices/issues" "Changelog" = "https://tm_devices.readthedocs.io/en/stable/CHANGELOG.html" diff --git a/src/tm_devices/__init__.py b/src/tm_devices/__init__.py index f18ca098..4dfca694 100644 --- a/src/tm_devices/__init__.py +++ b/src/tm_devices/__init__.py @@ -12,7 +12,7 @@ """ from importlib.metadata import version -from tm_devices.device_manager import DeviceManager +from tm_devices.device_manager import DeviceManager, print_available_visa_devices from tm_devices.helpers.constants_and_dataclasses import ( PACKAGE_NAME, PYVISA_PY_BACKEND, @@ -25,6 +25,7 @@ __all__ = [ "DeviceManager", + "print_available_visa_devices", "PYVISA_PY_BACKEND", "SupportedModels", "SYSTEM_DEFAULT_VISA_BACKEND", diff --git a/src/tm_devices/device_manager.py b/src/tm_devices/device_manager.py index 8a9388dd..d3845d81 100644 --- a/src/tm_devices/device_manager.py +++ b/src/tm_devices/device_manager.py @@ -4,6 +4,7 @@ import contextlib import inspect +import json import os import pathlib import socket @@ -1427,3 +1428,10 @@ def __set_options(self, verbose: bool) -> None: else: self.__visa_library = "" self.verbose_visa = bool(self.__config.options.verbose_visa) + + +def print_available_visa_devices() -> None: # pragma: no cover + """Print all available VISA devices to the console.""" + with contextlib.redirect_stdout(None), contextlib.redirect_stderr(None), DeviceManager() as dm: + available_devices = dm.get_available_devices() + print(json.dumps(available_devices["local"], indent=2))