From 720aba94855674ba5163ada19f0de0b9b740944c Mon Sep 17 00:00:00 2001 From: David Perl Date: Tue, 28 Jul 2026 14:21:19 +0200 Subject: [PATCH] FIRST COMMIT: plugin repo copy, minus entry points and unshared components --- .copier-answers.yml | 9 + .git_hooks/post-commit | 3 + .git_hooks/pre-commit | 3 + .gitea/workflows/ci.yml | 102 ++++++++++ .gitea/workflows/create_update_pr.yml | 70 +++++++ .gitignore | 180 ++++++++++++++++++ LICENSE | 29 +++ bin/.gitignore | 1 + mx_bec/__init__.py | 0 mx_bec/bec_widgets/__init__.py | 0 mx_bec/bec_widgets/auto_updates/__init__.py | 0 mx_bec/bec_widgets/widgets/__init__.py | 0 mx_bec/devices/__init__.py | 0 mx_bec/macros/README.md | 6 + mx_bec/macros/__init__.py | 0 mx_bec/scans/__init__.py | 0 mx_bec/scans/metadata_schema/__init__.py | 0 .../metadata_schema_registry.py | 12 ++ .../metadata_schema_template.py | 34 ++++ mx_bec/scans/scan_customization/__init__.py | 0 .../scan_customization/scan_components.py | 12 ++ .../scans/scan_customization/scan_modifier.py | 33 ++++ pyproject.toml | 60 ++++++ 23 files changed, 554 insertions(+) create mode 100644 .copier-answers.yml create mode 100644 .git_hooks/post-commit create mode 100644 .git_hooks/pre-commit create mode 100644 .gitea/workflows/ci.yml create mode 100644 .gitea/workflows/create_update_pr.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 bin/.gitignore create mode 100644 mx_bec/__init__.py create mode 100644 mx_bec/bec_widgets/__init__.py create mode 100644 mx_bec/bec_widgets/auto_updates/__init__.py create mode 100644 mx_bec/bec_widgets/widgets/__init__.py create mode 100644 mx_bec/devices/__init__.py create mode 100644 mx_bec/macros/README.md create mode 100644 mx_bec/macros/__init__.py create mode 100644 mx_bec/scans/__init__.py create mode 100644 mx_bec/scans/metadata_schema/__init__.py create mode 100644 mx_bec/scans/metadata_schema/metadata_schema_registry.py create mode 100644 mx_bec/scans/metadata_schema/metadata_schema_template.py create mode 100644 mx_bec/scans/scan_customization/__init__.py create mode 100644 mx_bec/scans/scan_customization/scan_components.py create mode 100644 mx_bec/scans/scan_customization/scan_modifier.py create mode 100644 pyproject.toml diff --git a/.copier-answers.yml b/.copier-answers.yml new file mode 100644 index 0000000..8f46764 --- /dev/null +++ b/.copier-answers.yml @@ -0,0 +1,9 @@ +# Do not edit this file! +# It is needed to track the repo template version, and editing may break things. +# This file will be overwritten by copier on template updates. + +_commit: v1.5.0 +_src_path: https://github.com/bec-project/plugin_copier_template.git +make_commit: false +project_name: mx_bec +widget_plugins_input: [] 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/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..0eb4dc1 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,102 @@ +name: CI for mx_bec +on: + push: + pull_request: + workflow_dispatch: + inputs: + BEC_WIDGETS_BRANCH: + description: "Branch of BEC Widgets to install" + required: false + type: string + default: "main" + BEC_CORE_BRANCH: + description: "Branch of BEC Core to install" + required: false + type: string + default: "main" + OPHYD_DEVICES_BRANCH: + description: "Branch of Ophyd Devices to install" + required: false + type: string + default: "main" + BEC_PLUGIN_REPO_BRANCH: + description: "Branch of the BEC Plugin Repository to install" + required: false + type: string + default: "main" + PYTHON_VERSION: + description: "Python version to use" + required: false + type: string + default: "3.12" + +permissions: + pull-requests: write + +jobs: + test: + runs-on: ubuntu-latest + env: + QTWEBENGINE_DISABLE_SANDBOX: 1 + QT_QPA_PLATFORM: "offscreen" + + steps: + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "${{ inputs.PYTHON_VERSION || '3.12' }}" + + - name: Checkout BEC Plugin Repository + uses: actions/checkout@v4 + with: + repository: bec/mx_bec + ref: "${{ inputs.BEC_PLUGIN_REPO_BRANCH || github.head_ref || github.sha }}" + path: ./mx_bec + + - name: Lint for merge conflicts from template updates + shell: bash + # Find all Copier conflicts except this line + run: '! grep -r "<<<<<<< before updating" | grep -v "grep -r \"<<<<<<< before updating"' + + - name: Checkout BEC Core + uses: actions/checkout@v4 + with: + repository: bec/bec + ref: "${{ inputs.BEC_CORE_BRANCH || 'main' }}" + path: ./bec + + - name: Checkout Ophyd Devices + uses: actions/checkout@v4 + with: + repository: bec/ophyd_devices + ref: "${{ inputs.OPHYD_DEVICES_BRANCH || 'main' }}" + path: ./ophyd_devices + + - name: Checkout BEC Widgets + uses: actions/checkout@v4 + with: + repository: bec/bec_widgets + ref: "${{ inputs.BEC_WIDGETS_BRANCH || 'main' }}" + path: ./bec_widgets + + - name: Install dependencies + shell: bash + run: | + sudo apt-get update + sudo apt-get install -y libgl1 libegl1 x11-utils libxkbcommon-x11-0 libdbus-1-3 xvfb + sudo apt-get -y install libnss3 libxdamage1 libasound2t64 libatomic1 libxcursor1 + + - name: Install Python dependencies + shell: bash + run: | + pip install uv + uv pip install --system -e ./ophyd_devices + uv pip install --system -e ./bec/bec_lib[dev] + uv pip install --system -e ./bec/bec_ipython_client + uv pip install --system -e ./bec/bec_server[dev] + uv pip install --system -e ./bec_widgets[dev,pyside6] + uv pip install --system -e ./mx_bec + + - name: Run Pytest with Coverage + id: coverage + run: pytest --random-order --cov=./mx_bec --cov-config=./mx_bec/pyproject.toml --cov-branch --cov-report=xml --no-cov-on-fail ./mx_bec/tests/ || test $? -eq 5 diff --git a/.gitea/workflows/create_update_pr.yml b/.gitea/workflows/create_update_pr.yml new file mode 100644 index 0000000..cf69228 --- /dev/null +++ b/.gitea/workflows/create_update_pr.yml @@ -0,0 +1,70 @@ +name: Create template upgrade PR for mx_bec +on: + workflow_dispatch: + +permissions: + pull-requests: write + +jobs: + create_update_branch_and_pr: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Checkout + uses: actions/checkout@v4 + + - name: Create virtualenv + run: | + python -m virtualenv .venv + + - name: Install tools + run: | + source .venv/bin/activate + pip install copier PySide6 bec_lib + + - name: Perform update + run: | + source .venv/bin/activate + git config --global user.email "bec_ci_staging@psi.ch" + git config --global user.name "BEC automated CI" + + branch="chore/update-template-$(python -m uuid)" + echo "switching to branch $branch" + git checkout -b $branch + + echo "Running copier update..." + copier update --trust --defaults --conflict inline 2>&1 | tee copier.log + status=${PIPESTATUS[0]} + output="$(cat copier.log)" + echo $output + msg="$(printf '%s\n' "$output" | head -n 1)" + + if ! grep -q "make_commit: true" .copier-answers.yml ; then + echo "Autocommit not made, committing..." + git add -A + git commit -a -m "$msg" + fi + + if diff-index --quiet HEAD ; then + echo "No changes detected" + exit 0 + fi + + git push -u origin $branch + curl -X POST "https://gitea.psi.ch/api/v1/repos/${{ gitea.repository }}/pulls" \ + -H "Authorization: token ${{ secrets.CI_REPO_WRITE }}" \ + -H "Content-Type: application/json" \ + -d "{ + \"title\": \"Template: $(echo $msg)\", + \"body\": \"This PR was created by Gitea Actions\", + \"head\": \"$(echo $branch)\", + \"base\": \"main\" + }" 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/LICENSE b/LICENSE new file mode 100644 index 0000000..f9b721a --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ + +BSD 3-Clause License + +Copyright (c) 2025, 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/mx_bec/__init__.py b/mx_bec/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/bec_widgets/__init__.py b/mx_bec/bec_widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/bec_widgets/auto_updates/__init__.py b/mx_bec/bec_widgets/auto_updates/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/bec_widgets/widgets/__init__.py b/mx_bec/bec_widgets/widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/devices/__init__.py b/mx_bec/devices/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/macros/README.md b/mx_bec/macros/README.md new file mode 100644 index 0000000..a3aa13d --- /dev/null +++ b/mx_bec/macros/README.md @@ -0,0 +1,6 @@ +# Macros + +This directory is intended to store macros which will be loaded automatically when starting BEC. +Macros are small functions to make repetitive tasks easier. Functions defined in python files in this directory will be accessible from the BEC console. +Please do not put any code outside of function definitions here. If you wish for code to be automatically run when starting BEC, see the startup script at mx_bec/bec_ipython_client/startup/post_startup.py +For a guide on writing macros, please see: https://bec.readthedocs.io/en/latest/user/command_line_interface.html#how-to-write-a-macro diff --git a/mx_bec/macros/__init__.py b/mx_bec/macros/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/scans/__init__.py b/mx_bec/scans/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/scans/metadata_schema/__init__.py b/mx_bec/scans/metadata_schema/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/scans/metadata_schema/metadata_schema_registry.py b/mx_bec/scans/metadata_schema/metadata_schema_registry.py new file mode 100644 index 0000000..deb6ef4 --- /dev/null +++ b/mx_bec/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/mx_bec/scans/metadata_schema/metadata_schema_template.py b/mx_bec/scans/metadata_schema/metadata_schema_template.py new file mode 100644 index 0000000..c12e364 --- /dev/null +++ b/mx_bec/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. diff --git a/mx_bec/scans/scan_customization/__init__.py b/mx_bec/scans/scan_customization/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/scans/scan_customization/scan_components.py b/mx_bec/scans/scan_customization/scan_components.py new file mode 100644 index 0000000..674d701 --- /dev/null +++ b/mx_bec/scans/scan_customization/scan_components.py @@ -0,0 +1,12 @@ +""" +Scan components for mx_bec. + +The scan components module allows you to define custom components that can be used in your scans. +These components can be used to encapsulate reusable logic, interact with devices, or perform specific actions during the scan lifecycle. +""" + +from bec_server.scan_server.scans.scan_components import ScanComponents + + +class MxBecScanComponents(ScanComponents): + """Scan components for mx_bec.""" diff --git a/mx_bec/scans/scan_customization/scan_modifier.py b/mx_bec/scans/scan_customization/scan_modifier.py new file mode 100644 index 0000000..823f5f9 --- /dev/null +++ b/mx_bec/scans/scan_customization/scan_modifier.py @@ -0,0 +1,33 @@ +""" +Scan modifier plugin for mx_bec. + +The scan modifier allows you to modify the scan lifecycle and run custom actions before or after the scan hook or replace the scan hook entirely. +Note that the scan_modifier module must be registered as a plugin in the pyproject.toml file for it to be recognized by the BEC framework and that +there can only be one scan_modifier plugin registered at a time. If you need to run multiple scan modifiers, you can create a single scan +modifier plugin that runs multiple actions in sequence with conditional logic to determine which actions to run based on the scan context. +""" + +from bec_server.scan_server.scans.scan_modifier import ScanModifier, scan_hook_impl + + +class MxBecScanModifier(ScanModifier): + """ + Scan modifier for mx_bec. + + By inheriting from the ScanModifier base class, you get access to currently running scan (self.scan), the devices (self.dev), the scan info (self.scan_info), + the scan components (self.components) and the scan actions (self.actions). + """ + + def __init__(self, **kwargs): + """Initialize the scan modifier.""" + super().__init__(**kwargs) + + # Example of running code before the scan stage for a specific scan + # @scan_hook_impl("stage", "before") + # def before_stage(self): + # """Run before the stage hook.""" + # self.actions.send_client_info("Custom stage logic executed by ScanModifier.") + # if self.scan_info.scan_name == "example_scan": + # self.dev.samx.set(20) + + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c2d5587 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,60 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "mx_bec" +version = "0.0.1" +description = "A shared repository for MX BEC components" +requires-python = ">=3.11" +classifiers = [ + "Development Status :: 3 - Alpha", + "Programming Language :: Python :: 3", + "Topic :: Scientific/Engineering", +] +dependencies = [] + +[project.optional-dependencies] +dev = [ + "black", + "copier", + "isort", + "coverage", + "pylint", + "pytest", + "pytest-random-order", + "ophyd_devices", + "bec_server", +] + +[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.*", +] + +[tool.ruff] +line-length = 100 + +[tool.ruff.format] +skip-magic-trailing-comma = true +