thzdaq-core 0.0.6b4__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.
Files changed (121) hide show
  1. thzdaq_core-0.0.6b4/.gitignore +25 -0
  2. thzdaq_core-0.0.6b4/LICENSE +21 -0
  3. thzdaq_core-0.0.6b4/PKG-INFO +96 -0
  4. thzdaq_core-0.0.6b4/README.md +69 -0
  5. thzdaq_core-0.0.6b4/pyproject.toml +56 -0
  6. thzdaq_core-0.0.6b4/src/thzdaq_core/__init__.py +1 -0
  7. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/__init__.py +3 -0
  8. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/instrument_states.py +51 -0
  9. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/json_store.py +177 -0
  10. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/legacy_profiles.py +247 -0
  11. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/library.py +124 -0
  12. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/processes.py +50 -0
  13. thzdaq_core-0.0.6b4/src/thzdaq_core/adapters/yaml_profiles.py +117 -0
  14. thzdaq_core-0.0.6b4/src/thzdaq_core/application/__init__.py +1 -0
  15. thzdaq_core-0.0.6b4/src/thzdaq_core/application/approvals.py +150 -0
  16. thzdaq_core-0.0.6b4/src/thzdaq_core/application/assembly.py +187 -0
  17. thzdaq_core-0.0.6b4/src/thzdaq_core/application/bench.py +91 -0
  18. thzdaq_core-0.0.6b4/src/thzdaq_core/application/check.py +339 -0
  19. thzdaq_core-0.0.6b4/src/thzdaq_core/application/clock.py +8 -0
  20. thzdaq_core-0.0.6b4/src/thzdaq_core/application/connections.py +31 -0
  21. thzdaq_core-0.0.6b4/src/thzdaq_core/application/custom.py +370 -0
  22. thzdaq_core-0.0.6b4/src/thzdaq_core/application/daemon.py +150 -0
  23. thzdaq_core-0.0.6b4/src/thzdaq_core/application/describe.py +296 -0
  24. thzdaq_core-0.0.6b4/src/thzdaq_core/application/devices.py +438 -0
  25. thzdaq_core-0.0.6b4/src/thzdaq_core/application/dispatch.py +93 -0
  26. thzdaq_core-0.0.6b4/src/thzdaq_core/application/events.py +97 -0
  27. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/__init__.py +48 -0
  28. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/analysis.py +90 -0
  29. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/approvals.py +51 -0
  30. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/custom.py +94 -0
  31. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/daemon.py +90 -0
  32. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/devices.py +182 -0
  33. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/measurements.py +206 -0
  34. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/scripts.py +67 -0
  35. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/sessions.py +144 -0
  36. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/state.py +54 -0
  37. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/station.py +145 -0
  38. thzdaq_core-0.0.6b4/src/thzdaq_core/application/handlers/ui.py +85 -0
  39. thzdaq_core-0.0.6b4/src/thzdaq_core/application/kept.py +61 -0
  40. thzdaq_core-0.0.6b4/src/thzdaq_core/application/local.py +101 -0
  41. thzdaq_core-0.0.6b4/src/thzdaq_core/application/policy.py +88 -0
  42. thzdaq_core-0.0.6b4/src/thzdaq_core/application/polling.py +133 -0
  43. thzdaq_core-0.0.6b4/src/thzdaq_core/application/ports.py +163 -0
  44. thzdaq_core-0.0.6b4/src/thzdaq_core/application/procedures.py +42 -0
  45. thzdaq_core-0.0.6b4/src/thzdaq_core/application/processes.py +364 -0
  46. thzdaq_core-0.0.6b4/src/thzdaq_core/application/runner.py +413 -0
  47. thzdaq_core-0.0.6b4/src/thzdaq_core/application/runs.py +522 -0
  48. thzdaq_core-0.0.6b4/src/thzdaq_core/application/scripts.py +612 -0
  49. thzdaq_core-0.0.6b4/src/thzdaq_core/application/station.py +294 -0
  50. thzdaq_core-0.0.6b4/src/thzdaq_core/application/values.py +29 -0
  51. thzdaq_core-0.0.6b4/src/thzdaq_core/application/windows.py +114 -0
  52. thzdaq_core-0.0.6b4/src/thzdaq_core/application/workspaces.py +123 -0
  53. thzdaq_core-0.0.6b4/src/thzdaq_core/cli/__init__.py +3 -0
  54. thzdaq_core-0.0.6b4/src/thzdaq_core/cli/station.py +108 -0
  55. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/__init__.py +5 -0
  56. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/errors.py +9 -0
  57. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/leases.py +110 -0
  58. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/__init__.py +1 -0
  59. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/catalog.py +37 -0
  60. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/custom.py +189 -0
  61. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedure.py +218 -0
  62. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/__init__.py +37 -0
  63. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/common.py +203 -0
  64. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/grid.py +55 -0
  65. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/if_noise.py +167 -0
  66. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/iv_curve.py +70 -0
  67. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/monitor.py +183 -0
  68. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/pv_curve.py +208 -0
  69. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/reflection.py +120 -0
  70. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/shot_noise.py +166 -0
  71. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/sis_tools.py +141 -0
  72. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/two_sb/__init__.py +21 -0
  73. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/two_sb/balance.py +239 -0
  74. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/two_sb/common.py +327 -0
  75. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/two_sb/optimization.py +159 -0
  76. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/measurements/procedures/two_sb/srr_tn.py +238 -0
  77. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/parameters.py +388 -0
  78. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/sis/__init__.py +1 -0
  79. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/sis/calibration.py +224 -0
  80. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/sis/demagnetization.py +38 -0
  81. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/__init__.py +1 -0
  82. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/config.py +114 -0
  83. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/kinds.py +133 -0
  84. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/ports.py +189 -0
  85. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/profile.py +320 -0
  86. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/safety.py +149 -0
  87. thzdaq_core-0.0.6b4/src/thzdaq_core/domain/station/station.py +733 -0
  88. thzdaq_core-0.0.6b4/tests/conftest.py +55 -0
  89. thzdaq_core-0.0.6b4/tests/lab_helpers.py +124 -0
  90. thzdaq_core-0.0.6b4/tests/test_kept_state.py +247 -0
  91. thzdaq_core-0.0.6b4/tests/test_lab_actions.py +113 -0
  92. thzdaq_core-0.0.6b4/tests/test_lab_analysis.py +56 -0
  93. thzdaq_core-0.0.6b4/tests/test_lab_approvals.py +216 -0
  94. thzdaq_core-0.0.6b4/tests/test_lab_assistant.py +74 -0
  95. thzdaq_core-0.0.6b4/tests/test_lab_console.py +91 -0
  96. thzdaq_core-0.0.6b4/tests/test_lab_custom.py +346 -0
  97. thzdaq_core-0.0.6b4/tests/test_lab_daemon.py +105 -0
  98. thzdaq_core-0.0.6b4/tests/test_lab_devices.py +81 -0
  99. thzdaq_core-0.0.6b4/tests/test_lab_events.py +108 -0
  100. thzdaq_core-0.0.6b4/tests/test_lab_leases.py +185 -0
  101. thzdaq_core-0.0.6b4/tests/test_lab_measurements.py +342 -0
  102. thzdaq_core-0.0.6b4/tests/test_lab_raw.py +205 -0
  103. thzdaq_core-0.0.6b4/tests/test_lab_rights.py +132 -0
  104. thzdaq_core-0.0.6b4/tests/test_lab_safety.py +79 -0
  105. thzdaq_core-0.0.6b4/tests/test_lab_script.py +248 -0
  106. thzdaq_core-0.0.6b4/tests/test_lab_scripts.py +359 -0
  107. thzdaq_core-0.0.6b4/tests/test_lab_sessions.py +346 -0
  108. thzdaq_core-0.0.6b4/tests/test_lab_station.py +259 -0
  109. thzdaq_core-0.0.6b4/tests/test_lab_ui.py +238 -0
  110. thzdaq_core-0.0.6b4/tests/test_lab_windows.py +152 -0
  111. thzdaq_core-0.0.6b4/tests/test_lab_workspaces.py +89 -0
  112. thzdaq_core-0.0.6b4/tests/test_legacy_cli.py +154 -0
  113. thzdaq_core-0.0.6b4/tests/test_parameters_plots_data.py +297 -0
  114. thzdaq_core-0.0.6b4/tests/test_procedures.py +472 -0
  115. thzdaq_core-0.0.6b4/tests/test_runner_cli.py +273 -0
  116. thzdaq_core-0.0.6b4/tests/test_running_file.py +205 -0
  117. thzdaq_core-0.0.6b4/tests/test_safety.py +227 -0
  118. thzdaq_core-0.0.6b4/tests/test_script_logs.py +61 -0
  119. thzdaq_core-0.0.6b4/tests/test_sis_monitor.py +202 -0
  120. thzdaq_core-0.0.6b4/tests/test_station.py +489 -0
  121. thzdaq_core-0.0.6b4/tests/test_workspaces.py +49 -0
@@ -0,0 +1,25 @@
1
+ .idea
2
+ venv
3
+ .venv
4
+ .uv-cache
5
+ build
6
+ dist
7
+ *__pycache__
8
+ *.log
9
+ *.DS_Store
10
+ *.spec
11
+ !ThzDAQ.spec
12
+ *.ipynb
13
+ .env
14
+ /data/
15
+ # What a daemon run from sources keeps between runs
16
+ /daemon.json
17
+ /daemon-sim.json
18
+ # Window layout and perspectives of a local run
19
+ /settings.ini
20
+ # What a daemon run from sources keeps of instruments (a grid's angle)
21
+ /state.json
22
+ /state-sim.json
23
+ # The program's own measurements and the profiles' previous versions
24
+ /measurements/
25
+ stations/.history/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yarvod
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.5
2
+ Name: thzdaq-core
3
+ Version: 0.0.6b4
4
+ Summary: ThzDAQ bench model, measurements and data without GUI
5
+ Project-URL: Homepage, https://github.com/yarvod/ThzDAQ
6
+ Project-URL: Documentation, https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md
7
+ Project-URL: Issues, https://github.com/yarvod/ThzDAQ/issues
8
+ Project-URL: Releases, https://github.com/yarvod/ThzDAQ/releases
9
+ Author: yarvod
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Topic :: Scientific/Engineering :: Physics
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: numpy<2,>=1.26
21
+ Requires-Dist: pyflakes>=3.2
22
+ Requires-Dist: pyyaml>=6.0
23
+ Requires-Dist: thzdaq-analysis==0.0.6b4
24
+ Requires-Dist: thzdaq-api==0.0.6b4
25
+ Requires-Dist: thzdaq-instruments==0.0.6b4
26
+ Description-Content-Type: text/markdown
27
+
28
+ # thzdaq-core
29
+
30
+ The bench, its measurements and data, without GUI or network. Three layers; dependencies
31
+ point inward and import-linter checks them (root `pyproject.toml`):
32
+
33
+ - `domain/` — the station and its profile format, device kinds and ports, procedures with
34
+ their parameters and plots, the measurement, analysis, SIS tools. No files, clocks or
35
+ threads of its own.
36
+ - `application/` — use cases that drive the domain: the measurement runner (threads,
37
+ autosave, time stamps) and the ports it needs (`ports.py`).
38
+ - `adapters/` — files: measurements as JSON (`json_store.py`), profiles as YAML
39
+ (`yaml_profiles.py`), the old program's `settings.ini` (`legacy_profiles.py`).
40
+ - `cli/` — `thzdaq-station`: bench profiles as files, without the daemon.
41
+
42
+ The bench daemon (`thzdaq[daemon]`) runs it; clients reach it only through `thzdaq-api`
43
+ ([docs/daemon.md](https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md)).
44
+
45
+ ## Station
46
+
47
+ A profile (YAML, version 2) lists instruments (driver, connection) and the devices
48
+ configured on them; ids name the kind of device (`stations/2sb.example.yaml`):
49
+
50
+ ```python
51
+ from thzdaq_core.adapters.yaml_profiles import load_station
52
+ from thzdaq_core.domain.station.station import Station
53
+
54
+ with Station(load_station("stations/2sb.example.yaml"), simulate=True) as station:
55
+ bias = station.device("sis_bias_1")
56
+ bias.set_voltage(2.3e-3)
57
+ ```
58
+
59
+ ```bash
60
+ uv run thzdaq-station show stations/2sb.example.yaml
61
+ uv run thzdaq-station check stations/2sb.example.yaml --simulate
62
+ ```
63
+
64
+ `import-legacy` reads the QVariant-encoded `[Configs]` of the old program and needs PySide6:
65
+
66
+ ```bash
67
+ uv run python -m thzdaq_core.cli.station import-legacy settings.ini --name lab -o stations/lab.yaml
68
+ ```
69
+
70
+ ## Measurements
71
+
72
+ ```python
73
+ from thzdaq_core.adapters.json_store import MeasurementStore
74
+ from thzdaq_core.adapters.yaml_profiles import load_station
75
+ from thzdaq_core.application.runner import MeasurementRunner
76
+ from thzdaq_core.domain.measurements.catalog import get_procedure
77
+ from thzdaq_core.domain.station.station import Station
78
+
79
+ with Station(load_station("stations/2sb.example.yaml"), simulate=True) as station:
80
+ runner = MeasurementRunner(station, MeasurementStore("data"))
81
+ procedure = get_procedure("sis.current_vs_voltage")(voltage=(0, 7e-3, 351))
82
+ measurement = runner.run(procedure, devices={"bias": "sis_bias_1"})
83
+ print(measurement.status, len(measurement.records("iv")))
84
+ ```
85
+
86
+ ```bash
87
+ uv run thzdaq procedures
88
+ uv run thzdaq describe sis.current_vs_voltage
89
+ uv run thzdaq --simulate run sis.current_vs_voltage --device bias=sis_bias_1 --set voltage=0:7:351
90
+ uv run thzdaq show data/2026-10-29/000001_sis_current_vs_voltage.json
91
+ ```
92
+
93
+ Procedures are listed explicitly in `domain/measurements/procedures/__init__.py`
94
+ (`BUILTIN`); the catalog (`domain/measurements/catalog.py`) offers them by name.
95
+ Measurements are JSON files `data/<day>/<number>_<procedure>.json`, saved while the run
96
+ goes.
@@ -0,0 +1,69 @@
1
+ # thzdaq-core
2
+
3
+ The bench, its measurements and data, without GUI or network. Three layers; dependencies
4
+ point inward and import-linter checks them (root `pyproject.toml`):
5
+
6
+ - `domain/` — the station and its profile format, device kinds and ports, procedures with
7
+ their parameters and plots, the measurement, analysis, SIS tools. No files, clocks or
8
+ threads of its own.
9
+ - `application/` — use cases that drive the domain: the measurement runner (threads,
10
+ autosave, time stamps) and the ports it needs (`ports.py`).
11
+ - `adapters/` — files: measurements as JSON (`json_store.py`), profiles as YAML
12
+ (`yaml_profiles.py`), the old program's `settings.ini` (`legacy_profiles.py`).
13
+ - `cli/` — `thzdaq-station`: bench profiles as files, without the daemon.
14
+
15
+ The bench daemon (`thzdaq[daemon]`) runs it; clients reach it only through `thzdaq-api`
16
+ ([docs/daemon.md](https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md)).
17
+
18
+ ## Station
19
+
20
+ A profile (YAML, version 2) lists instruments (driver, connection) and the devices
21
+ configured on them; ids name the kind of device (`stations/2sb.example.yaml`):
22
+
23
+ ```python
24
+ from thzdaq_core.adapters.yaml_profiles import load_station
25
+ from thzdaq_core.domain.station.station import Station
26
+
27
+ with Station(load_station("stations/2sb.example.yaml"), simulate=True) as station:
28
+ bias = station.device("sis_bias_1")
29
+ bias.set_voltage(2.3e-3)
30
+ ```
31
+
32
+ ```bash
33
+ uv run thzdaq-station show stations/2sb.example.yaml
34
+ uv run thzdaq-station check stations/2sb.example.yaml --simulate
35
+ ```
36
+
37
+ `import-legacy` reads the QVariant-encoded `[Configs]` of the old program and needs PySide6:
38
+
39
+ ```bash
40
+ uv run python -m thzdaq_core.cli.station import-legacy settings.ini --name lab -o stations/lab.yaml
41
+ ```
42
+
43
+ ## Measurements
44
+
45
+ ```python
46
+ from thzdaq_core.adapters.json_store import MeasurementStore
47
+ from thzdaq_core.adapters.yaml_profiles import load_station
48
+ from thzdaq_core.application.runner import MeasurementRunner
49
+ from thzdaq_core.domain.measurements.catalog import get_procedure
50
+ from thzdaq_core.domain.station.station import Station
51
+
52
+ with Station(load_station("stations/2sb.example.yaml"), simulate=True) as station:
53
+ runner = MeasurementRunner(station, MeasurementStore("data"))
54
+ procedure = get_procedure("sis.current_vs_voltage")(voltage=(0, 7e-3, 351))
55
+ measurement = runner.run(procedure, devices={"bias": "sis_bias_1"})
56
+ print(measurement.status, len(measurement.records("iv")))
57
+ ```
58
+
59
+ ```bash
60
+ uv run thzdaq procedures
61
+ uv run thzdaq describe sis.current_vs_voltage
62
+ uv run thzdaq --simulate run sis.current_vs_voltage --device bias=sis_bias_1 --set voltage=0:7:351
63
+ uv run thzdaq show data/2026-10-29/000001_sis_current_vs_voltage.json
64
+ ```
65
+
66
+ Procedures are listed explicitly in `domain/measurements/procedures/__init__.py`
67
+ (`BUILTIN`); the catalog (`domain/measurements/catalog.py`) offers them by name.
68
+ Measurements are JSON files `data/<day>/<number>_<procedure>.json`, saved while the run
69
+ goes.
@@ -0,0 +1,56 @@
1
+ [project]
2
+ name = "thzdaq-core"
3
+ version = "0.0.6b4"
4
+ description = "ThzDAQ bench model, measurements and data without GUI"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [{ name = "yarvod" }]
10
+ classifiers = [
11
+ "Development Status :: 3 - Alpha",
12
+ "Intended Audience :: Science/Research",
13
+ "Operating System :: OS Independent",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3 :: Only",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Topic :: Scientific/Engineering :: Physics",
18
+ ]
19
+ dependencies = [
20
+ # The daemon is tested with numpy 1.x; thzdaq-analysis alone allows 2 for scripts
21
+ "numpy>=1.26,<2",
22
+ "pyflakes>=3.2",
23
+ "pyyaml>=6.0",
24
+ "thzdaq-analysis==0.0.6b4",
25
+ "thzdaq-api==0.0.6b4",
26
+ "thzdaq-instruments==0.0.6b4",
27
+ ]
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/yarvod/ThzDAQ"
31
+ Documentation = "https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md"
32
+ Issues = "https://github.com/yarvod/ThzDAQ/issues"
33
+ Releases = "https://github.com/yarvod/ThzDAQ/releases"
34
+
35
+ [project.scripts]
36
+ thzdaq-station = "thzdaq_core.cli.station:main"
37
+
38
+ [dependency-groups]
39
+ dev = [
40
+ "pytest>=8.2",
41
+ ]
42
+
43
+ [tool.uv.sources]
44
+ thzdaq-analysis = { workspace = true }
45
+ thzdaq-api = { workspace = true }
46
+ thzdaq-instruments = { workspace = true }
47
+
48
+ [build-system]
49
+ requires = ["hatchling>=1.27"]
50
+ build-backend = "hatchling.build"
51
+
52
+ [tool.hatch.build.targets.wheel]
53
+ packages = ["src/thzdaq_core"]
54
+
55
+ [tool.pytest.ini_options]
56
+ testpaths = ["tests"]
@@ -0,0 +1 @@
1
+ """ThzDAQ core: domain, application and adapters of the bench, no GUI and no network."""
@@ -0,0 +1,3 @@
1
+ """Adapters: files the core reads and writes (JSON measurements, YAML profiles,
2
+ legacy settings.ini).
3
+ """
@@ -0,0 +1,51 @@
1
+ """The InstrumentStates port in a JSON file of the daemon (state.json next to
2
+ daemon.json; the simulator's state-sim.json): every record by instrument id,
3
+ the whole file written again at each change, atomically (.partial, then
4
+ os.replace), under a lock: moves come from several threads. Only its own
5
+ daemon writes it (one per folder, FileLock).
6
+ """
7
+
8
+ import json
9
+ import logging
10
+ import os
11
+ import threading
12
+ from pathlib import Path
13
+ from typing import Any, Dict, Mapping, Optional
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ class JsonInstrumentStates:
19
+ def __init__(self, path: Path):
20
+ self.path = path
21
+ self._lock = threading.Lock()
22
+ self._records: Dict[str, Dict[str, Any]] = self._load()
23
+
24
+ def _load(self) -> Dict[str, Dict[str, Any]]:
25
+ try:
26
+ found = json.loads(self.path.read_text(encoding="utf-8"))
27
+ except FileNotFoundError:
28
+ return {}
29
+ except (OSError, ValueError) as e:
30
+ logger.error(f"{self.path} is not read, nothing kept is applied: {e}")
31
+ return {}
32
+ if not isinstance(found, dict):
33
+ logger.error(f"{self.path} holds no records, nothing kept is applied")
34
+ return {}
35
+ return {key: value for key, value in found.items() if isinstance(value, dict)}
36
+
37
+ def get(self, instrument_id: str) -> Optional[Mapping[str, Any]]:
38
+ with self._lock:
39
+ record = self._records.get(instrument_id)
40
+ return None if record is None else dict(record)
41
+
42
+ def put(self, instrument_id: str, record: Mapping[str, Any]) -> None:
43
+ with self._lock:
44
+ self._records[instrument_id] = dict(record)
45
+ text = json.dumps(self._records, indent=2, sort_keys=True)
46
+ partial = self.path.with_suffix(".partial")
47
+ try:
48
+ partial.write_text(text, encoding="utf-8")
49
+ os.replace(partial, self.path)
50
+ except OSError as e:
51
+ logger.error(f"{self.path}: {instrument_id} is not kept: {e}")
@@ -0,0 +1,177 @@
1
+ """Measurements on disk, as the daemon writes them: one JSON file each,
2
+ <root>/<YYYY-MM-DD>/<id>_<procedure>.json (thzdaq_api.measurement_file), the
3
+ procedure's name with _ for what a file name may not hold.
4
+
5
+ Ids are numbers counted per data folder: the next one is one more than the
6
+ largest in the file names, so they keep growing across runs of the program.
7
+ Start and end times (to the millisecond) are in every file as well.
8
+
9
+ While a measurement runs (RunningFile), its file holds its description
10
+ without records, written again only when it changes, and its records are
11
+ appended to the records file next to it, a line each; at its end the whole
12
+ file is written and the records file removed. A long monitor appends a line
13
+ per record instead of writing megabytes every few seconds. Writes of the file
14
+ go to a temporary file first, so a crash leaves the previous complete
15
+ version; a file still marked running reads back as interrupted, with the
16
+ records of its records file.
17
+ """
18
+
19
+ import logging
20
+ import os
21
+ import re
22
+ import threading
23
+ from pathlib import Path
24
+ from typing import IO, Any, Dict, Mapping, Optional, Tuple
25
+
26
+ from thzdaq_api.measurement_file import (
27
+ RUNNING,
28
+ Column,
29
+ Measurement,
30
+ MeasurementFolder,
31
+ PathLike,
32
+ dumps,
33
+ file_id,
34
+ record_line,
35
+ records_path,
36
+ write_file,
37
+ write_measurement,
38
+ )
39
+
40
+ logger = logging.getLogger(__name__)
41
+
42
+ # What a file name of every system allows of a procedure's name: letters,
43
+ # digits, _ and -; a script's "script:<title>" has a colon (a stream of a
44
+ # file on NTFS) and may have a slash (a folder)
45
+ UNSAFE = re.compile(r"[^\w-]+")
46
+
47
+
48
+ class MeasurementStore(MeasurementFolder):
49
+ """A folder of measurement files the daemon numbers and writes."""
50
+
51
+ def __init__(self, root: PathLike):
52
+ super().__init__(root)
53
+ self._last = 0
54
+ self._lock = threading.Lock()
55
+
56
+ def next_id(self) -> int:
57
+ """A number larger than every measurement in the folder and given so far."""
58
+ with self._lock:
59
+ found = [file_id(path) for path in self.paths()]
60
+ self._last = max([self._last, *(i for i in found if i is not None)]) + 1
61
+ return self._last
62
+
63
+ def path_for(self, measurement: Measurement) -> Path:
64
+ """data/<day it started>/<number>_<procedure>.json"""
65
+ if not measurement.started:
66
+ raise ValueError(
67
+ "a measurement has no file before it starts (no start time)"
68
+ )
69
+ day = measurement.started[:10]
70
+ procedure = UNSAFE.sub("_", measurement.procedure)
71
+ number = measurement.id
72
+ name = f"{number:06d}" if isinstance(number, int) else str(number)
73
+ return self.root / day / f"{name}_{procedure}.json"
74
+
75
+ def open(self, measurement: Measurement) -> "RunningFile":
76
+ return RunningFile(self, measurement, self.path_for(measurement))
77
+
78
+ def save(self, measurement: Measurement, path: Optional[PathLike] = None) -> Path:
79
+ """The whole measurement; one that ended needs its records file no
80
+ more (a file left by a crash is gathered so)."""
81
+ path = write_measurement(measurement, path or self.path_for(measurement))
82
+ measurement.metadata["path"] = str(path)
83
+ if measurement.status != RUNNING:
84
+ records_path(path).unlink(missing_ok=True)
85
+ return path
86
+
87
+
88
+ Shape = Tuple[str, Tuple[Column, ...]] # a table as its description has it
89
+
90
+
91
+ class RunningFile:
92
+ """The file of a running measurement (MeasurementWriter): its description
93
+ when it starts and whenever it changes, a line in the records file per
94
+ record, the whole file at its end.
95
+
96
+ A line is flushed as it is written, so it is the operating system's once
97
+ append returns; update puts the lines on the drive itself. A table or a
98
+ column the description lacks is described before its record is appended,
99
+ so every line has its column titles and units. When a line cannot be
100
+ written, the records file stops and every update writes the whole file,
101
+ as before records files."""
102
+
103
+ def __init__(self, store: MeasurementStore, measurement: Measurement, path: Path):
104
+ self.store = store
105
+ self.measurement = measurement
106
+ self.path = path
107
+ self._description = "" # as the file holds it
108
+ self._shapes: Dict[str, Shape] = {}
109
+ self._records: Optional[IO[str]] = None
110
+ measurement.metadata["path"] = str(path)
111
+ self._describe()
112
+ try:
113
+ self._records = open(
114
+ records_path(path), "w", encoding="utf-8", newline="\n"
115
+ )
116
+ except OSError:
117
+ logger.exception(f"{path}: no records file, the whole file is saved")
118
+
119
+ def append(self, table: str, record: Mapping[str, Any]) -> None:
120
+ if self._records is None:
121
+ return # update saves the whole file
122
+ if self._shape(table) != self._shapes.get(table):
123
+ self._describe()
124
+ try:
125
+ self._records.write(record_line(table, record))
126
+ self._records.flush()
127
+ except OSError:
128
+ logger.exception(f"{self.path}: records stop, the whole file is saved")
129
+ self._stop_records()
130
+ self._save()
131
+
132
+ def update(self) -> None:
133
+ if self._records is None:
134
+ self._save()
135
+ return
136
+ self._describe()
137
+ try:
138
+ os.fsync(self._records.fileno())
139
+ except OSError:
140
+ logger.exception(f"{self.path}: records not synced")
141
+
142
+ def close(self) -> None:
143
+ self._stop_records()
144
+ self._save()
145
+
146
+ def _shape(self, table: str) -> Shape:
147
+ found = self.measurement.tables[table]
148
+ return found.title, tuple(found.columns)
149
+
150
+ def _describe(self) -> None:
151
+ """Its description, when it is not the file's already (one that
152
+ failed is tried again by the next update, not by every record)."""
153
+ text = dumps(self.measurement.to_dict(records=False), indent=1)
154
+ if text == self._description:
155
+ return
156
+ self._shapes = {name: self._shape(name) for name in self.measurement.tables}
157
+ try:
158
+ write_file(self.path, text)
159
+ except OSError:
160
+ logger.exception(f"{self.path}: description not saved")
161
+ return
162
+ self._description = text
163
+
164
+ def _save(self) -> None:
165
+ try:
166
+ self.store.save(self.measurement, self.path)
167
+ except OSError:
168
+ logger.exception(f"{self.path}: not saved")
169
+
170
+ def _stop_records(self) -> None:
171
+ if self._records is None:
172
+ return
173
+ records, self._records = self._records, None
174
+ try:
175
+ records.close()
176
+ except OSError:
177
+ logger.exception(f"{self.path}: records file not closed")