naboo-py 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- naboo_py-0.1.0/.github/workflows/publish.yaml +67 -0
- naboo_py-0.1.0/.gitignore +227 -0
- naboo_py-0.1.0/.vscode/settings.json +3 -0
- naboo_py-0.1.0/PKG-INFO +27 -0
- naboo_py-0.1.0/README.md +3 -0
- naboo_py-0.1.0/pyproject.toml +49 -0
- naboo_py-0.1.0/src/naboo/__init__.py +4 -0
- naboo_py-0.1.0/src/naboo/client.py +93 -0
- naboo_py-0.1.0/tests/__init__.py +0 -0
- naboo_py-0.1.0/tests/test_client.py +192 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: astral-sh/setup-uv@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- run: uv pip install -e ".[dev]"
|
|
21
|
+
- run: uv run pytest -v
|
|
22
|
+
|
|
23
|
+
build:
|
|
24
|
+
needs: test
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
steps:
|
|
27
|
+
- uses: actions/checkout@v4
|
|
28
|
+
- uses: actions/setup-python@v5
|
|
29
|
+
with:
|
|
30
|
+
python-version: "3.12"
|
|
31
|
+
- run: python -m pip install --upgrade pip build twine
|
|
32
|
+
- run: python -m build
|
|
33
|
+
- run: twine check dist/*
|
|
34
|
+
- uses: actions/upload-artifact@v4
|
|
35
|
+
with:
|
|
36
|
+
name: dist
|
|
37
|
+
path: dist/
|
|
38
|
+
|
|
39
|
+
publish-testpypi:
|
|
40
|
+
if: github.event_name == 'workflow_dispatch'
|
|
41
|
+
needs: build
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
environment: testpypi
|
|
44
|
+
permissions:
|
|
45
|
+
id-token: write
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/download-artifact@v4
|
|
48
|
+
with:
|
|
49
|
+
name: dist
|
|
50
|
+
path: dist/
|
|
51
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
52
|
+
with:
|
|
53
|
+
repository-url: https://test.pypi.org/legacy/
|
|
54
|
+
|
|
55
|
+
publish-pypi:
|
|
56
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
57
|
+
needs: build
|
|
58
|
+
runs-on: ubuntu-latest
|
|
59
|
+
environment: pypi
|
|
60
|
+
permissions:
|
|
61
|
+
id-token: write
|
|
62
|
+
steps:
|
|
63
|
+
- uses: actions/download-artifact@v4
|
|
64
|
+
with:
|
|
65
|
+
name: dist
|
|
66
|
+
path: dist/
|
|
67
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
*.lcov
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
db.sqlite3-wal
|
|
65
|
+
db.sqlite3-shm
|
|
66
|
+
|
|
67
|
+
# Flask stuff:
|
|
68
|
+
instance/
|
|
69
|
+
.webassets-cache
|
|
70
|
+
|
|
71
|
+
# Scrapy stuff:
|
|
72
|
+
.scrapy
|
|
73
|
+
|
|
74
|
+
# Sphinx documentation
|
|
75
|
+
docs/_build/
|
|
76
|
+
|
|
77
|
+
# PyBuilder
|
|
78
|
+
.pybuilder/
|
|
79
|
+
target/
|
|
80
|
+
|
|
81
|
+
# Jupyter Notebook
|
|
82
|
+
.ipynb_checkpoints
|
|
83
|
+
|
|
84
|
+
# IPython
|
|
85
|
+
profile_default/
|
|
86
|
+
ipython_config.py
|
|
87
|
+
|
|
88
|
+
# pyenv
|
|
89
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
90
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
91
|
+
# .python-version
|
|
92
|
+
|
|
93
|
+
# pipenv
|
|
94
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
95
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
96
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
97
|
+
# install all needed dependencies.
|
|
98
|
+
# Pipfile.lock
|
|
99
|
+
|
|
100
|
+
# uv
|
|
101
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
102
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
103
|
+
# commonly ignored for libraries.
|
|
104
|
+
# uv.lock
|
|
105
|
+
|
|
106
|
+
# poetry
|
|
107
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
108
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
109
|
+
# commonly ignored for libraries.
|
|
110
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
111
|
+
# poetry.lock
|
|
112
|
+
# poetry.toml
|
|
113
|
+
|
|
114
|
+
# pdm
|
|
115
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
116
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
117
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
118
|
+
# pdm.lock
|
|
119
|
+
# pdm.toml
|
|
120
|
+
.pdm-python
|
|
121
|
+
.pdm-build/
|
|
122
|
+
|
|
123
|
+
# pixi
|
|
124
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
125
|
+
# pixi.lock
|
|
126
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
127
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
128
|
+
.pixi/*
|
|
129
|
+
!.pixi/config.toml
|
|
130
|
+
|
|
131
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
132
|
+
__pypackages__/
|
|
133
|
+
|
|
134
|
+
# Celery stuff
|
|
135
|
+
celerybeat-schedule*
|
|
136
|
+
celerybeat.pid
|
|
137
|
+
|
|
138
|
+
# Redis
|
|
139
|
+
*.rdb
|
|
140
|
+
*.aof
|
|
141
|
+
*.pid
|
|
142
|
+
|
|
143
|
+
# RabbitMQ
|
|
144
|
+
mnesia/
|
|
145
|
+
rabbitmq/
|
|
146
|
+
rabbitmq-data/
|
|
147
|
+
|
|
148
|
+
# ActiveMQ
|
|
149
|
+
activemq-data/
|
|
150
|
+
|
|
151
|
+
# SageMath parsed files
|
|
152
|
+
*.sage.py
|
|
153
|
+
|
|
154
|
+
# Environments
|
|
155
|
+
.env
|
|
156
|
+
.envrc
|
|
157
|
+
.venv
|
|
158
|
+
env/
|
|
159
|
+
venv/
|
|
160
|
+
ENV/
|
|
161
|
+
env.bak/
|
|
162
|
+
venv.bak/
|
|
163
|
+
|
|
164
|
+
# Conda environments
|
|
165
|
+
conda-meta/
|
|
166
|
+
|
|
167
|
+
# Spyder project settings
|
|
168
|
+
.spyderproject
|
|
169
|
+
.spyproject
|
|
170
|
+
|
|
171
|
+
# Rope project settings
|
|
172
|
+
.ropeproject
|
|
173
|
+
|
|
174
|
+
# mkdocs/Zensical documentation
|
|
175
|
+
/site
|
|
176
|
+
|
|
177
|
+
# mypy
|
|
178
|
+
.mypy_cache/
|
|
179
|
+
.dmypy.json
|
|
180
|
+
dmypy.json
|
|
181
|
+
|
|
182
|
+
# Pyre type checker
|
|
183
|
+
.pyre/
|
|
184
|
+
|
|
185
|
+
# pytype static type analyzer
|
|
186
|
+
.pytype/
|
|
187
|
+
|
|
188
|
+
# Cython debug symbols
|
|
189
|
+
cython_debug/
|
|
190
|
+
|
|
191
|
+
# PyCharm
|
|
192
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
193
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
194
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
195
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
196
|
+
# .idea/
|
|
197
|
+
|
|
198
|
+
# Abstra
|
|
199
|
+
# Abstra is an AI-powered process automation framework.
|
|
200
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
201
|
+
# Learn more at https://abstra.io/docs
|
|
202
|
+
.abstra/
|
|
203
|
+
|
|
204
|
+
# Visual Studio Code
|
|
205
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
206
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
207
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
208
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
209
|
+
# .vscode/
|
|
210
|
+
# Temporary file for partial code execution
|
|
211
|
+
tempCodeRunnerFile.py
|
|
212
|
+
|
|
213
|
+
# Ruff stuff:
|
|
214
|
+
.ruff_cache/
|
|
215
|
+
|
|
216
|
+
# PyPI configuration file
|
|
217
|
+
.pypirc
|
|
218
|
+
|
|
219
|
+
# Marimo
|
|
220
|
+
marimo/_static/
|
|
221
|
+
marimo/_lsp/
|
|
222
|
+
__marimo__/
|
|
223
|
+
|
|
224
|
+
# Streamlit
|
|
225
|
+
.streamlit/secrets.toml
|
|
226
|
+
|
|
227
|
+
*.lock
|
naboo_py-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: naboo-py
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An python support lib for the Naboo SCPI Device
|
|
5
|
+
Project-URL: Homepage, https://github.com/you/mypackage
|
|
6
|
+
Author-email: Henrique Domiciano <henriquedomiciano@yahoo.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Requires-Dist: future>=1.0.0
|
|
13
|
+
Requires-Dist: iso8601>=2.1.0
|
|
14
|
+
Requires-Dist: pyserial>=3.5
|
|
15
|
+
Requires-Dist: pyusb>=1.3.1
|
|
16
|
+
Requires-Dist: pyvisa-py>=0.7.2
|
|
17
|
+
Requires-Dist: pyvisa>=1.14.1
|
|
18
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
19
|
+
Requires-Dist: serial>=0.0.97
|
|
20
|
+
Requires-Dist: typing-extensions>=4.16.0
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
### Python Lib support of naboo SCPI device.
|
|
26
|
+
|
|
27
|
+
|
naboo_py-0.1.0/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "naboo-py"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "An python support lib for the Naboo SCPI Device"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Henrique Domiciano", email = "henriquedomiciano@yahoo.com" },
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"future>=1.0.0",
|
|
22
|
+
"iso8601>=2.1.0",
|
|
23
|
+
"pyserial>=3.5",
|
|
24
|
+
"pyusb>=1.3.1",
|
|
25
|
+
"pyvisa>=1.14.1",
|
|
26
|
+
"pyvisa-py>=0.7.2",
|
|
27
|
+
"pyyaml>=6.0.3",
|
|
28
|
+
"serial>=0.0.97",
|
|
29
|
+
"typing-extensions>=4.16.0",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = ["pytest"]
|
|
34
|
+
|
|
35
|
+
[tool.pyright]
|
|
36
|
+
include = ["src", "tests"]
|
|
37
|
+
extraPaths = ["src"]
|
|
38
|
+
venvPath = "."
|
|
39
|
+
venv = ".venv"
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
pythonpath = ["src"]
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/you/mypackage"
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["src/naboo"]
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
import time
|
|
4
|
+
from typing import List, Tuple
|
|
5
|
+
|
|
6
|
+
import pyvisa
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class NabooClient:
|
|
10
|
+
"""High-level pyvisa client for the Naboo SCPI digital output controller."""
|
|
11
|
+
|
|
12
|
+
CHANNEL_COUNT = 9
|
|
13
|
+
|
|
14
|
+
def __init__(self, resource_name: str, timeout_ms: int = 2000) -> None:
|
|
15
|
+
self._rm = pyvisa.ResourceManager()
|
|
16
|
+
self.inst = self._rm.open_resource(resource_name)
|
|
17
|
+
self.inst.timeout = timeout_ms
|
|
18
|
+
self.inst.write_termination = "\n"
|
|
19
|
+
self.inst.read_termination = "\n"
|
|
20
|
+
|
|
21
|
+
def close(self) -> None:
|
|
22
|
+
self.inst.close()
|
|
23
|
+
self._rm.close()
|
|
24
|
+
|
|
25
|
+
def __enter__(self) -> "NabooClient":
|
|
26
|
+
return self
|
|
27
|
+
|
|
28
|
+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
29
|
+
self.close()
|
|
30
|
+
|
|
31
|
+
def idn(self) -> str:
|
|
32
|
+
return self.inst.query("*IDN?").strip()
|
|
33
|
+
|
|
34
|
+
def queue_on(self, channel: int, duration_ms: int = 0) -> None:
|
|
35
|
+
self._validate_channel(channel)
|
|
36
|
+
self.inst.write(f"DIGital:ON {channel},{duration_ms}")
|
|
37
|
+
|
|
38
|
+
def queue_off(self, channel: int, duration_ms: int = 0) -> None:
|
|
39
|
+
self._validate_channel(channel)
|
|
40
|
+
self.inst.write(f"DIGital:OFF {channel},{duration_ms}")
|
|
41
|
+
|
|
42
|
+
def clear_queues(self) -> None:
|
|
43
|
+
self.inst.write("DIGital:CLEar")
|
|
44
|
+
|
|
45
|
+
def execute(self) -> None:
|
|
46
|
+
self.inst.write("DIGital:EXECute")
|
|
47
|
+
|
|
48
|
+
def execution_bitmap(self) -> int:
|
|
49
|
+
return int(self.inst.query("DIGital:EXECute:STATe?").strip())
|
|
50
|
+
|
|
51
|
+
def pin_state(self, channel: int) -> int:
|
|
52
|
+
self._validate_channel(channel)
|
|
53
|
+
return int(self.inst.query(f"DIGital:STATe? {channel}").strip())
|
|
54
|
+
|
|
55
|
+
def get_queue(self, channel: int) -> List[Tuple[int, int]]:
|
|
56
|
+
self._validate_channel(channel)
|
|
57
|
+
raw = self.inst.query(f"DIGital:QUEue? {channel}").strip()
|
|
58
|
+
if raw == "0" or raw == "":
|
|
59
|
+
return []
|
|
60
|
+
|
|
61
|
+
parts = [int(x) for x in raw.split(",")]
|
|
62
|
+
if len(parts) % 2 != 0:
|
|
63
|
+
raise ValueError(f"Unexpected queue response format: {raw!r}")
|
|
64
|
+
|
|
65
|
+
return [(parts[i], parts[i + 1]) for i in range(0, len(parts), 2)]
|
|
66
|
+
|
|
67
|
+
def wait_until_idle(self, poll_interval_s: float = 0.05, max_wait_s: float = 10.0) -> None:
|
|
68
|
+
deadline = time.monotonic() + max_wait_s
|
|
69
|
+
while self.execution_bitmap() != 0:
|
|
70
|
+
if time.monotonic() > deadline:
|
|
71
|
+
raise TimeoutError("Timed out waiting for Naboo execution_bitmap to return to 0.")
|
|
72
|
+
time.sleep(poll_interval_s)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def _validate_channel(cls, channel: int) -> None:
|
|
76
|
+
if not 1 <= channel <= cls.CHANNEL_COUNT:
|
|
77
|
+
raise ValueError(f"Channel must be between 1 and {cls.CHANNEL_COUNT}, got {channel}")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def discover_naboo_resource() -> str:
|
|
81
|
+
rm = pyvisa.ResourceManager()
|
|
82
|
+
try:
|
|
83
|
+
resources = rm.list_resources()
|
|
84
|
+
if not resources:
|
|
85
|
+
raise RuntimeError("No VISA resources found. Check USB connection and drivers.")
|
|
86
|
+
print("Discovered VISA resources:", resources)
|
|
87
|
+
for res in resources:
|
|
88
|
+
if "USB" in res.upper():
|
|
89
|
+
return res
|
|
90
|
+
return resources[0]
|
|
91
|
+
finally:
|
|
92
|
+
rm.close()
|
|
93
|
+
|
|
File without changes
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
from unittest.mock import MagicMock
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from naboo import client as client_module
|
|
6
|
+
from naboo.client import NabooClient, discover_naboo_resource
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.fixture
|
|
10
|
+
def mock_rm(monkeypatch):
|
|
11
|
+
"""Replace pyvisa.ResourceManager with a mock; returns the mock manager."""
|
|
12
|
+
rm = MagicMock(name="ResourceManager")
|
|
13
|
+
monkeypatch.setattr(client_module.pyvisa, "ResourceManager", lambda: rm)
|
|
14
|
+
return rm
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.fixture
|
|
18
|
+
def inst(mock_rm):
|
|
19
|
+
return mock_rm.open_resource.return_value
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def naboo(mock_rm, inst):
|
|
24
|
+
return NabooClient("USB0::FAKE::INSTR")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# ---------- construction / lifecycle ----------
|
|
28
|
+
|
|
29
|
+
def test_init_configures_instrument(mock_rm, inst):
|
|
30
|
+
NabooClient("USB0::FAKE::INSTR", timeout_ms=1234)
|
|
31
|
+
mock_rm.open_resource.assert_called_once_with("USB0::FAKE::INSTR")
|
|
32
|
+
assert inst.timeout == 1234
|
|
33
|
+
assert inst.write_termination == "\n"
|
|
34
|
+
assert inst.read_termination == "\n"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_close_closes_instrument_and_manager(naboo, mock_rm, inst):
|
|
38
|
+
naboo.close()
|
|
39
|
+
inst.close.assert_called_once()
|
|
40
|
+
mock_rm.close.assert_called_once()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_context_manager_closes(mock_rm, inst):
|
|
44
|
+
with NabooClient("USB0::FAKE::INSTR") as n:
|
|
45
|
+
assert isinstance(n, NabooClient)
|
|
46
|
+
inst.close.assert_called_once()
|
|
47
|
+
mock_rm.close.assert_called_once()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_context_manager_closes_on_exception(mock_rm, inst):
|
|
51
|
+
with pytest.raises(RuntimeError):
|
|
52
|
+
with NabooClient("USB0::FAKE::INSTR"):
|
|
53
|
+
raise RuntimeError("boom")
|
|
54
|
+
inst.close.assert_called_once()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# ---------- simple commands ----------
|
|
58
|
+
|
|
59
|
+
def test_idn_strips_whitespace(naboo, inst):
|
|
60
|
+
inst.query.return_value = " Acme,Naboo,001,1.0 \r\n"
|
|
61
|
+
assert naboo.idn() == "Acme,Naboo,001,1.0"
|
|
62
|
+
inst.query.assert_called_once_with("*IDN?")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_queue_on_default_duration(naboo, inst):
|
|
66
|
+
naboo.queue_on(3)
|
|
67
|
+
inst.write.assert_called_once_with("DIGital:ON 3,0")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_queue_on_with_duration(naboo, inst):
|
|
71
|
+
naboo.queue_on(3, 500)
|
|
72
|
+
inst.write.assert_called_once_with("DIGital:ON 3,500")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_queue_off(naboo, inst):
|
|
76
|
+
naboo.queue_off(9, 250)
|
|
77
|
+
inst.write.assert_called_once_with("DIGital:OFF 9,250")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_clear_queues(naboo, inst):
|
|
81
|
+
naboo.clear_queues()
|
|
82
|
+
inst.write.assert_called_once_with("DIGital:CLEar")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_execute(naboo, inst):
|
|
86
|
+
naboo.execute()
|
|
87
|
+
inst.write.assert_called_once_with("DIGital:EXECute")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_execution_bitmap(naboo, inst):
|
|
91
|
+
inst.query.return_value = "5\n"
|
|
92
|
+
assert naboo.execution_bitmap() == 5
|
|
93
|
+
inst.query.assert_called_once_with("DIGital:EXECute:STATe?")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_pin_state(naboo, inst):
|
|
97
|
+
inst.query.return_value = "1"
|
|
98
|
+
assert naboo.pin_state(2) == 1
|
|
99
|
+
inst.query.assert_called_once_with("DIGital:STATe? 2")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# ---------- channel validation ----------
|
|
103
|
+
|
|
104
|
+
@pytest.mark.parametrize("bad", [-1, 0, 10, 100])
|
|
105
|
+
@pytest.mark.parametrize("method", ["queue_on", "queue_off", "pin_state", "get_queue"])
|
|
106
|
+
def test_invalid_channel_raises_and_sends_nothing(naboo, inst, method, bad):
|
|
107
|
+
with pytest.raises(ValueError, match="Channel must be between 1 and 9"):
|
|
108
|
+
getattr(naboo, method)(bad)
|
|
109
|
+
inst.write.assert_not_called()
|
|
110
|
+
inst.query.assert_not_called()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@pytest.mark.parametrize("ch", [1, 9])
|
|
114
|
+
def test_boundary_channels_are_valid(naboo, inst, ch):
|
|
115
|
+
naboo.queue_on(ch)
|
|
116
|
+
inst.write.assert_called_once()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# ---------- get_queue parsing ----------
|
|
120
|
+
|
|
121
|
+
@pytest.mark.parametrize("raw", ["0", "", " \n"])
|
|
122
|
+
def test_get_queue_empty(naboo, inst, raw):
|
|
123
|
+
inst.query.return_value = raw
|
|
124
|
+
assert naboo.get_queue(1) == []
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def test_get_queue_pairs(naboo, inst):
|
|
128
|
+
inst.query.return_value = "1,100,0,200,1,0"
|
|
129
|
+
assert naboo.get_queue(4) == [(1, 100), (0, 200), (1, 0)]
|
|
130
|
+
inst.query.assert_called_once_with("DIGital:QUEue? 4")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_get_queue_odd_number_of_values_raises(naboo, inst):
|
|
134
|
+
inst.query.return_value = "1,100,0"
|
|
135
|
+
with pytest.raises(ValueError, match="Unexpected queue response"):
|
|
136
|
+
naboo.get_queue(1)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def test_get_queue_non_numeric_raises(naboo, inst):
|
|
140
|
+
inst.query.return_value = "a,b"
|
|
141
|
+
with pytest.raises(ValueError):
|
|
142
|
+
naboo.get_queue(1)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
# ---------- wait_until_idle ----------
|
|
146
|
+
|
|
147
|
+
def test_wait_until_idle_returns_when_bitmap_zero(naboo, inst, monkeypatch):
|
|
148
|
+
monkeypatch.setattr(client_module.time, "sleep", lambda _: None)
|
|
149
|
+
inst.query.side_effect = ["3", "1", "0"]
|
|
150
|
+
naboo.wait_until_idle()
|
|
151
|
+
assert inst.query.call_count == 3
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def test_wait_until_idle_already_idle_does_not_sleep(naboo, inst, monkeypatch):
|
|
155
|
+
sleep = MagicMock()
|
|
156
|
+
monkeypatch.setattr(client_module.time, "sleep", sleep)
|
|
157
|
+
inst.query.return_value = "0"
|
|
158
|
+
naboo.wait_until_idle()
|
|
159
|
+
sleep.assert_not_called()
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_wait_until_idle_times_out(naboo, inst, monkeypatch):
|
|
163
|
+
monkeypatch.setattr(client_module.time, "sleep", lambda _: None)
|
|
164
|
+
# fake clock: starts at 0, then jumps past the deadline
|
|
165
|
+
ticks = iter([0.0, 0.5, 11.0])
|
|
166
|
+
monkeypatch.setattr(client_module.time, "monotonic", lambda: next(ticks))
|
|
167
|
+
inst.query.return_value = "1" # never idle
|
|
168
|
+
with pytest.raises(TimeoutError):
|
|
169
|
+
naboo.wait_until_idle(max_wait_s=10.0)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
# ---------- discover_naboo_resource ----------
|
|
173
|
+
|
|
174
|
+
def test_discover_prefers_usb(mock_rm):
|
|
175
|
+
mock_rm.list_resources.return_value = (
|
|
176
|
+
"ASRL1::INSTR",
|
|
177
|
+
"USB0::0x1234::0x5678::SN::INSTR",
|
|
178
|
+
)
|
|
179
|
+
assert discover_naboo_resource() == "USB0::0x1234::0x5678::SN::INSTR"
|
|
180
|
+
mock_rm.close.assert_called_once()
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_discover_falls_back_to_first(mock_rm):
|
|
184
|
+
mock_rm.list_resources.return_value = ("ASRL1::INSTR", "GPIB0::1::INSTR")
|
|
185
|
+
assert discover_naboo_resource() == "ASRL1::INSTR"
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def test_discover_raises_when_nothing_found_and_still_closes(mock_rm):
|
|
189
|
+
mock_rm.list_resources.return_value = ()
|
|
190
|
+
with pytest.raises(RuntimeError, match="No VISA resources"):
|
|
191
|
+
discover_naboo_resource()
|
|
192
|
+
mock_rm.close.assert_called_once()
|