commit a389984d25daf978cf6ad1ff3a8664e1d656a6dd Author: perl_d Date: Tue Apr 29 16:38:52 2025 +0200 refactor: make a copier template from BEC plugin files diff --git a/.git_hooks/post-commit b/.git_hooks/post-commit new file mode 100644 index 0000000..3fe80fe --- /dev/null +++ b/.git_hooks/post-commit @@ -0,0 +1,3 @@ +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +semantic-release changelog -D version_variable=$SCRIPT_DIR/../../semantic_release/__init__.py:__version__ +semantic-release version -D version_variable=$SCRIPT_DIR/../../semantic_release/__init__.py:__version__ \ No newline at end of file diff --git a/.git_hooks/pre-commit b/.git_hooks/pre-commit new file mode 100644 index 0000000..392493b --- /dev/null +++ b/.git_hooks/pre-commit @@ -0,0 +1,3 @@ +black --line-length=100 $(git diff --cached --name-only --diff-filter=ACM -- '*.py') +isort --line-length=100 --profile=black --multi-line=3 --trailing-comma $(git diff --cached --name-only --diff-filter=ACM -- '*.py') +git add $(git diff --cached --name-only --diff-filter=ACM -- '*.py') diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4c73aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,180 @@ +**/*_venv +**/.idea +*.log +**/__pycache__ +**/.DS_Store +**/out +**/.vscode +**/.pytest_cache +**/*.egg* + +# recovery_config files +recovery_config_* + +# file writer data +**.h5 + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/**/_build/ +docs/**/autodoc/ +docs/**/_autosummary/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +**.prof + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/.gitlab-ci.yml.jinja b/.gitlab-ci.yml.jinja new file mode 100644 index 0000000..fc4182c --- /dev/null +++ b/.gitlab-ci.yml.jinja @@ -0,0 +1,6 @@ +include: +- file: /templates/plugin-repo-template.yml + inputs: + name: {{ project_name }} + target: {{ project_name }} + project: bec/awi_utils diff --git a/LICENSE.jinja b/LICENSE.jinja new file mode 100644 index 0000000..506fc4e --- /dev/null +++ b/LICENSE.jinja @@ -0,0 +1,29 @@ + +BSD 3-Clause License + +Copyright (c) {{ copyright_year }}, Paul Scherrer Institute + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..20a11ba --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1 @@ +# Add anything you don't want to check in to git, e.g. very large files \ No newline at end of file diff --git a/copier.yml b/copier.yml new file mode 100644 index 0000000..592ce9e --- /dev/null +++ b/copier.yml @@ -0,0 +1,24 @@ +# predefined values +# make sure these have 'when: false' so that the questions are not asked and the items are not saved in answers.yml + +test_directories: + type: str + multiselect: true + default: ["tests_bec_ipython_client", "tests_bec_widgets", "tests_dap_services", "tests_devices", "tests_file_writer", "tests_scans"] + when: false + +copyright_year: + type: int + default: 2025 + when: false + +# questions + +project_name: + type: str + help: What is your project name? + +# other configuration + +_tasks: + - "git init --initial-branch=main" \ No newline at end of file diff --git a/pyproject.toml.jinja b/pyproject.toml.jinja new file mode 100644 index 0000000..fb3fe31 --- /dev/null +++ b/pyproject.toml.jinja @@ -0,0 +1,77 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "{{ project_name }}" +version = "0.0.0" +description = "A plugin repository for BEC" +requires-python = ">=3.10" +classifiers = [ + "Development Status :: 3 - Alpha", + "Programming Language :: Python :: 3", + "Topic :: Scientific/Engineering", +] +dependencies = [] + +[project.optional-dependencies] +dev = [ + "black", + "isort", + "coverage", + "pylint", + "pytest", + "pytest-random-order", + "ophyd_devices", + "bec_server", +] + +[project.entry-points."bec"] +plugin_bec = "{{ project_name }}" + +[project.entry-points."bec.deployment.device_server"] +plugin_ds_startup = "{{ project_name }}.deployments.device_server.startup:run" + +[project.entry-points."bec.file_writer"] +plugin_file_writer = "{{ project_name }}.file_writer" + +[project.entry-points."bec.scans"] +plugin_scans = "{{ project_name }}.scans" + +[project.entry-points."bec.scans.metadata_schema"] +plugin_metadata_schema = "{{ project_name }}.scans.metadata_schema" + +[project.entry-points."bec.ipython_client_startup"] +plugin_ipython_client_pre = "{{ project_name }}.bec_ipython_client.startup.pre_startup" +plugin_ipython_client_post = "{{ project_name }}.bec_ipython_client.startup" + +[project.entry-points."bec.widgets.auto_updates"] +plugin_widgets_update = "{{ project_name }}.bec_widgets.auto_updates:PlotUpdate" + +[project.entry-points."bec.widgets.user_widgets"] +plugin_widgets = "{{ project_name }}.bec_widgets.widgets" + +[tool.hatch.build.targets.wheel] +include = ["*"] + +[tool.isort] +profile = "black" +line_length = 100 +multi_line_output = 3 +include_trailing_comma = true + +[tool.black] +line-length = 100 +skip-magic-trailing-comma = true + +[tool.pylint.basic] +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs = [ + ".*scanID.*", + ".*RID.*", + ".*pointID.*", + ".*ID.*", + ".*_2D.*", + ".*_1D.*", +] diff --git a/tests/{% yield test_dir from test_directories %}{{ test_dir }}{% endyield %}/README.md b/tests/{% yield test_dir from test_directories %}{{ test_dir }}{% endyield %}/README.md new file mode 100644 index 0000000..63d535f --- /dev/null +++ b/tests/{% yield test_dir from test_directories %}{{ test_dir }}{% endyield %}/README.md @@ -0,0 +1,34 @@ +# Getting Started with Testing using pytest + +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash +pip install pytest +``` + +in your _python environment_. +We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. + +## Introduction + +Tests in this package should be stored in the `tests` directory. +We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. + +To run all tests, navigate to the directory of the plugin from the command line, and run the command + +```bash +pytest -v --random-order ./tests +``` + +Note, the python environment needs to be active. +The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. + +## Test examples + +Writing tests can be quite specific for the given function. +We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. +A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). +In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). diff --git a/{{_copier_conf.answers_file}}.jinja b/{{_copier_conf.answers_file}}.jinja new file mode 100644 index 0000000..9b0aa19 --- /dev/null +++ b/{{_copier_conf.answers_file}}.jinja @@ -0,0 +1,2 @@ +# Changes here will be overwritten by Copier! +{{ _copier_answers|to_nice_yaml -}} \ No newline at end of file diff --git a/{{project_name}}/__init__.py b/{{project_name}}/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/bec_ipython_client/__init__.py b/{{project_name}}/bec_ipython_client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/bec_ipython_client/high_level_interface/__init__.py b/{{project_name}}/bec_ipython_client/high_level_interface/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/bec_ipython_client/startup/post_startup.py b/{{project_name}}/bec_ipython_client/startup/post_startup.py new file mode 100644 index 0000000..07d6da4 --- /dev/null +++ b/{{project_name}}/bec_ipython_client/startup/post_startup.py @@ -0,0 +1,36 @@ +""" +Post startup script for the BEC client. This script is executed after the +IPython shell is started. It is used to load the beamline specific +information and to setup the prompts. + +The script is executed in the global namespace of the IPython shell. This +means that all variables defined here are available in the shell. + +While command-line arguments have to be set in the pre-startup script, the +post-startup script can be used to load beamline specific information and +to setup the prompts. + + from bec_lib.logger import bec_logger + + logger = bec_logger.logger + + # pylint: disable=import-error + _args = _main_dict["args"] + + _session_name = "cSAXS" + if _args.session.lower() == "lamni": + from csaxs_bec.bec_ipython_client.plugins.cSAXS import * + from csaxs_bec.bec_ipython_client.plugins.LamNI import * + + _session_name = "LamNI" + lamni = LamNI(bec) + logger.success("LamNI session loaded.") + + elif _args.session.lower() == "csaxs": + print("Loading cSAXS session") + from csaxs_bec.bec_ipython_client.plugins.cSAXS import * + + logger.success("cSAXS session loaded.") +""" + +# pylint: disable=invalid-name, unused-import, import-error, undefined-variable, unused-variable, unused-argument, no-name-in-module diff --git a/{{project_name}}/bec_ipython_client/startup/pre_startup.py b/{{project_name}}/bec_ipython_client/startup/pre_startup.py new file mode 100644 index 0000000..1132c92 --- /dev/null +++ b/{{project_name}}/bec_ipython_client/startup/pre_startup.py @@ -0,0 +1,16 @@ +""" +Pre-startup script for BEC client. This script is executed before the BEC client +is started. It can be used to add additional command line arguments. +""" + +from bec_lib.service_config import ServiceConfig + + +def extend_command_line_args(parser): + """ + Extend the command line arguments of the BEC client. + """ + + # parser.add_argument("--session", help="Session name", type=str, default="cSAXS") + + return parser diff --git a/{{project_name}}/bec_widgets/__init__.py b/{{project_name}}/bec_widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/bec_widgets/widgets/__init__.py b/{{project_name}}/bec_widgets/widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/deployments/__init__.py b/{{project_name}}/deployments/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/deployments/device_server/__init__.py b/{{project_name}}/deployments/device_server/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/deployments/device_server/startup.py b/{{project_name}}/deployments/device_server/startup.py new file mode 100644 index 0000000..c4b1f88 --- /dev/null +++ b/{{project_name}}/deployments/device_server/startup.py @@ -0,0 +1,11 @@ +import os + + +def setup_epics_ca(): + # os.environ["EPICS_CA_AUTO_ADDR_LIST"] = "NO" + # os.environ["EPICS_CA_ADDR_LIST"] = "129.129.122.255 sls-x12sa-cagw.psi.ch:5836" + os.environ["PYTHONIOENCODING"] = "latin1" + + +def run(): + setup_epics_ca() diff --git a/{{project_name}}/device_configs/__init__.py b/{{project_name}}/device_configs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/devices/__init__.py b/{{project_name}}/devices/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/scans/__init__.py b/{{project_name}}/scans/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{project_name}}/scans/metadata_schema/metadata_schema_registry.py b/{{project_name}}/scans/metadata_schema/metadata_schema_registry.py new file mode 100644 index 0000000..deb6ef4 --- /dev/null +++ b/{{project_name}}/scans/metadata_schema/metadata_schema_registry.py @@ -0,0 +1,12 @@ +# from .metadata_schema_template import ExampleSchema + +METADATA_SCHEMA_REGISTRY = { + # Add models which should be used to validate scan metadata here. + # Make a model according to the template, and import it as above + # Then associate it with a scan like so: + # "example_scan": ExampleSchema +} + +# Define a default schema type which should be used as the fallback for everything: + +DEFAULT_SCHEMA = None diff --git a/{{project_name}}/scans/metadata_schema/metadata_schema_template.py b/{{project_name}}/scans/metadata_schema/metadata_schema_template.py new file mode 100644 index 0000000..c12e364 --- /dev/null +++ b/{{project_name}}/scans/metadata_schema/metadata_schema_template.py @@ -0,0 +1,34 @@ +# # By inheriting from BasicScanMetadata you can define a schema by which metadata +# # supplied to a scan must be validated. +# # This schema is a Pydantic model: https://docs.pydantic.dev/latest/concepts/models/ +# # but by default it will still allow you to add any arbitrary information to it. +# # That is to say, when you run a scan with which such a model has been associated in the +# # metadata_schema_registry, you can supply any python dictionary with strings as keys +# # and built-in python types (strings, integers, floats) as values, and these will be +# # added to the experiment metadata, but it *must* contain the keys and values of the +# # types defined in the schema class. +# # +# # +# # For example, say that you would like to enforce recording information about sample +# # pretreatment, you could define the following: +# # +# +# from bec_lib.metadata_schema import BasicScanMetadata +# +# +# class ExampleSchema(BasicScanMetadata): +# treatment_description: str +# treatment_temperature_k: int +# +# +# # If this was used according to the example in metadata_schema_registry.py, +# # then when calling the scan, the user would need to write something like: +# >>> scans.example_scan( +# >>> motor, +# >>> 1, +# >>> 2, +# >>> 3, +# >>> metadata={"treatment_description": "oven overnight", "treatment_temperature_k": 575}, +# >>> ) +# +# # And the additional metadata would be saved in the HDF5 file created for the scan.