Repository: NSLS-II/sirepo-bluesky (language: python)

Issue:
Sort out test failures
https://github.com/NSLS-II/sirepo-bluesky/actions/runs/5828844990/job/15807294877


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
index 0a0440fe..6e296314 100644
--- a/.github/workflows/docs.yml
+++ b/.github/workflows/docs.yml
@@ -44,32 +44,43 @@ jobs:
         with:
           docker-binary: ${{ env.DOCKER_BINARY }}
 
-      - name: Set up Python ${{ matrix.python-version }} with conda
-        uses: conda-incubator/setup-miniconda@v2
+      - name: Set up Python ${{ matrix.python-version }}
+        uses: actions/setup-python@v4
         with:
-          activate-environment: ${{ env.REPOSITORY_NAME }}-py${{ matrix.python-version }}
-          auto-update-conda: true
-          miniconda-version: "latest"
           python-version: ${{ matrix.python-version }}
-          mamba-version: "*"
-          channels: conda-forge
 
-      - name: Install documentation-building requirements
+      # - name: Set up Python ${{ matrix.python-version }}
+      #   uses: conda-incubator/setup-miniconda@v2
+      #   with:
+      #     activate-environment: ${{ env.REPOSITORY_NAME }}-py${{ matrix.python-version }}
+      #     auto-update-conda: true
+      #     miniconda-version: "latest"
+      #     python-version: ${{ matrix.python-version }}
+      #     mamba-version: "*"
+      #     channels: conda-forge
+
+      - name: Install documentation-building requirements with apt/dpkg
         run: |
-          set -vxeo pipefail
-          conda env list
-          mamba install -c conda-forge pandoc
-          pip install --upgrade pip wheel
-          pip install -v .
-          pip install -r requirements-dev.txt
-          pip list
-          conda list
+          set -vxeuo pipefail
+          wget --progress=dot:giga "https://github.com/jgm/pandoc/releases/download/3.1.6.1/pandoc-3.1.6.1-1-amd64.deb" -O /tmp/pandoc.deb
+          sudo dpkg -i /tmp/pandoc.deb
+          # conda install -c conda-forge -y pandoc
+          which pandoc
+          pandoc --version
+
+      - name: Install documentation-building requirements with pip
+        run: |
+          set -vxeuo pipefail
+          python -m pip install --upgrade pip wheel
+          python -m pip install -v .
+          python -m pip install -r requirements-dev.txt
+          python -m pip list
 
       - name: Copy databroker config file
         run: |
           set -vxeuo pipefail
           mkdir -v -p ~/.config/databroker/
-          cp examples/local.yml ~/.config/databroker/
+          cp -v examples/local.yml ~/.config/databroker/
 
       - name: Build Docs
         run: |
diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml
index 3dad078f..007955e9 100644
--- a/.github/workflows/publish-pypi.yml
+++ b/.github/workflows/publish-pypi.yml
@@ -30,7 +30,7 @@ jobs:
         run: |
           set -vxeuo pipefail
           python -m pip install --upgrade pip
-          pip install wheel setuptools
+          python -m pip install wheel setuptools
           python setup.py sdist bdist_wheel
 
       - name: Publish wheels to PyPI
diff --git a/docs/source/notebooks/madx.ipynb b/docs/source/notebooks/madx.ipynb
index 942a9c57..24ca93d5 100644
--- a/docs/source/notebooks/madx.ipynb
+++ b/docs/source/notebooks/madx.ipynb
@@ -24,7 +24,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_flyer_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__ -e flyer\n",
     "\n",
     "import matplotlib.pyplot as plt\n",
     "\n",
diff --git a/docs/source/notebooks/shadow.ipynb b/docs/source/notebooks/shadow.ipynb
index 9431dded..ec9ef62d 100644
--- a/docs/source/notebooks/shadow.ipynb
+++ b/docs/source/notebooks/shadow.ipynb
@@ -36,7 +36,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_det_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__\n",
     "\n",
     "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n",
     "from sirepo_bluesky.sirepo_ophyd import BeamStatisticsReport, create_classes\n",
@@ -104,7 +106,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_det_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__\n",
     "\n",
     "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n",
     "from sirepo_bluesky.sirepo_ophyd import BeamStatisticsReport, create_classes\n",
@@ -190,7 +194,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.10.11"
+   "version": "3.9.13"
   }
  },
  "nbformat": 4,
diff --git a/docs/source/notebooks/srw.ipynb b/docs/source/notebooks/srw.ipynb
index 4d306fa6..237e2257 100644
--- a/docs/source/notebooks/srw.ipynb
+++ b/docs/source/notebooks/srw.ipynb
@@ -36,7 +36,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_det_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__\n",
     "\n",
     "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n",
     "from sirepo_bluesky.sirepo_ophyd import create_classes\n",
@@ -103,7 +105,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_det_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__\n",
     "\n",
     "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n",
     "from sirepo_bluesky.sirepo_ophyd import create_classes\n",
@@ -184,7 +188,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "%run -i ../../../examples/prepare_det_env.py\n",
+    "from sirepo_bluesky import prepare_re_env\n",
+    "\n",
+    "%run -i $prepare_re_env.__file__\n",
     "\n",
     "from sirepo_bluesky.sirepo_bluesky import SirepoBluesky\n",
     "from sirepo_bluesky.sirepo_ophyd import create_classes\n",
diff --git a/docs/source/release-history.rst b/docs/source/release-history.rst
index 3bc9e159..92761f57 100644
--- a/docs/source/release-history.rst
+++ b/docs/source/release-history.rst
@@ -20,11 +20,13 @@ API
   is turned into a document for SRW and Shadow apps. The beam stats show up in
   Bluesky's ``BestEffortCallback``.
 - Removed old API support and relevant tests.
+- ``create_classes`` does not need the ``data`` kwarg as it obtains it via
+  ``connection``.
 
 Documentation
 .............
 - Suppress Shadow3 code's stdout in notebooks/code.
-- Converted project's `README.rst` to `README.md` for better rendering on PyPI.
+- Converted project's ``README.rst`` to ``README.md`` for better rendering on PyPI.
 
 Examples
 ........
diff --git a/examples/prepare_det_env.py b/examples/prepare_det_env.py
deleted file mode 100644
index 46f25c09..00000000
--- a/examples/prepare_det_env.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import datetime
-import json  # noqa F401
-
-import bluesky.plan_stubs as bps  # noqa F401
-import bluesky.plans as bp  # noqa F401
-import databroker
-import matplotlib.pyplot as plt
-import numpy as np  # noqa F401
-from bluesky.callbacks import best_effort
-from bluesky.run_engine import RunEngine
-from databroker import Broker
-from ophyd.utils import make_dir_tree
-
-from sirepo_bluesky.shadow_handler import ShadowFileHandler
-from sirepo_bluesky.srw_handler import SRWFileHandler
-
-RE = RunEngine({})
-bec = best_effort.BestEffortCallback()
-RE.subscribe(bec)
-
-# MongoDB backend:
-db = Broker.named("local")  # mongodb backend
-try:
-    databroker.assets.utils.install_sentinels(db.reg.config, version=1)
-except Exception:
-    pass
-
-RE.subscribe(db.insert)
-db.reg.register_handler("srw", SRWFileHandler, overwrite=True)
-db.reg.register_handler("shadow", ShadowFileHandler, overwrite=True)
-db.reg.register_handler("SIREPO_FLYER", SRWFileHandler, overwrite=True)
-
-plt.ion()
-
-root_dir = "/tmp/sirepo-bluesky-data"
-_ = make_dir_tree(datetime.datetime.now().year, base_path=root_dir)
diff --git a/examples/prepare_flyer_env.py b/examples/prepare_flyer_env.py
deleted file mode 100644
index e64cf28f..00000000
--- a/examples/prepare_flyer_env.py
+++ /dev/null
@@ -1,32 +0,0 @@
-import datetime
-
-import bluesky.plan_stubs as bps  # noqa F401
-import bluesky.plans as bp  # noqa F401
-import databroker
-from bluesky.callbacks import best_effort
-from bluesky.run_engine import RunEngine
-from databroker import Broker
-from ophyd.utils import make_dir_tree
-
-from sirepo_bluesky.madx_handler import MADXFileHandler
-from sirepo_bluesky.srw_handler import SRWFileHandler
-
-RE = RunEngine({})
-bec = best_effort.BestEffortCallback()
-bec.disable_plots()
-RE.subscribe(bec)
-
-# MongoDB backend:
-db = Broker.named("local")  # mongodb backend
-try:
-    databroker.assets.utils.install_sentinels(db.reg.config, version=1)
-except Exception:
-    pass
-
-RE.subscribe(db.insert)
-db.reg.register_handler("srw", SRWFileHandler, overwrite=True)
-db.reg.register_handler("SIREPO_FLYER", SRWFileHandler, overwrite=True)
-db.reg.register_handler("madx", MADXFileHandler, overwrite=True)
-
-root_dir = "/tmp/sirepo-bluesky-data"
-_ = make_dir_tree(datetime.datetime.now().year, base_path=root_dir)
diff --git a/requirements-dev.txt b/requirements-dev.txt
index b14da730..89130c89 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -16,6 +16,6 @@ matplotlib
 nbsphinx
 numpydoc
 pandoc
-sphinx
+sphinx==7.1.2
 sphinx-copybutton
 tabulate>=0.9.0
diff --git a/setup.py b/setup.py
index 9ec4bbc9..32aaf740 100644
--- a/setup.py
+++ b/setup.py
@@ -50,7 +50,7 @@
     entry_points={
         "console_scripts": [
             # 'command = some.module:some_function',
-            "json-yaml-converter = sirepo_bluesky.utils.json_yaml_converter:cli_converter"
+            "json-yaml-converter = sirepo_bluesky.utils.json_yaml_converter:cli_converter",
         ],
     },
     include_package_data=True,
diff --git a/sirepo_bluesky/__init__.py b/sirepo_bluesky/__init__.py
index 08ecbef5..8bac5028 100644
--- a/sirepo_bluesky/__init__.py
+++ b/sirepo_bluesky/__init__.py
@@ -1,6 +1,7 @@
 from ophyd import Signal
 
 from ._version import get_versions
+from .utils import prepare_re_env  # noqa: F401
 
 __version__ = get_versions()["version"]
 del get_versions
diff --git a/sirepo_bluesky/utils/prepare_re_env.py b/sirepo_bluesky/utils/prepare_re_env.py
new file mode 100644
index 00000000..f3ae4e15
--- /dev/null
+++ b/sirepo_bluesky/utils/prepare_re_env.py
@@ -0,0 +1,91 @@
+import argparse
+import datetime
+import json  # noqa F401
+
+import bluesky.plan_stubs as bps  # noqa F401
+import bluesky.plans as bp  # noqa F401
+import databroker
+import matplotlib.pyplot as plt
+import numpy as np  # noqa F401
+from bluesky.callbacks import best_effort
+from bluesky.run_engine import RunEngine
+from databroker import Broker
+from ophyd.utils import make_dir_tree
+
+DEFAULT_DB_TYPE = "local"
+DEFAULT_ROOT_DIR = "/tmp/sirepo-bluesky-data"
+DEFAULT_ENV_TYPE = "stepper"
+
+
+def re_env(db_type=DEFAULT_DB_TYPE, root_dir=DEFAULT_ROOT_DIR):
+    RE = RunEngine({})
+    bec = best_effort.BestEffortCallback()
+    RE.subscribe(bec)
+
+    db = Broker.named(db_type)
+    try:
+        databroker.assets.utils.install_sentinels(db.reg.config, version=1)
+    except Exception:
+        pass
+    RE.subscribe(db.insert)
+
+    _ = make_dir_tree(datetime.datetime.now().year, base_path=root_dir)
+
+    return dict(RE=RE, db=db, bec=bec)
+
+
+def register_handlers(db, handlers):
+    for handler_spec, handler_class in handlers.items():
+        db.reg.register_handler(handler_spec, handler_class, overwrite=True)
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description="Prepare bluesky environment")
+    parser.add_argument(
+        "-d",
+        "--db-type",
+        dest="db_type",
+        default=DEFAULT_DB_TYPE,
+        help="Type of databroker ('local', 'temp', etc.)",
+    )
+    parser.add_argument(
+        "-r",
+        "--root-dir",
+        dest="root_dir",
+        default=DEFAULT_ROOT_DIR,
+        help="The root dir to create YYYY/MM/DD dir structure.",
+    )
+
+    env_choices = ["stepper", "flyer"]
+    parser.add_argument(
+        "-e",
+        "--env-type",
+        dest="env_type",
+        choices=env_choices,
+        default=DEFAULT_ENV_TYPE,
+        help="Type of RE environment.",
+    )
+
+    args = parser.parse_args()
+    kwargs_re = dict(db_type=args.db_type, root_dir=args.root_dir)
+    ret = re_env(**kwargs_re)
+    globals().update(**ret)
+
+    from sirepo_bluesky.srw_handler import SRWFileHandler
+
+    if args.env_type == "stepper":
+        from sirepo_bluesky.shadow_handler import ShadowFileHandler
+
+        handlers = {"srw": SRWFileHandler, "SIREPO_FLYER": SRWFileHandler, "shadow": ShadowFileHandler}
+        plt.ion()
+    elif args.env_type == "flyer":
+        from sirepo_bluesky.madx_handler import MADXFileHandler
+
+        handlers = {"srw": SRWFileHandler, "SIREPO_FLYER": SRWFileHandler, "madx": MADXFileHandler}
+        bec.disable_plots()  # noqa: F821
+    else:
+        raise RuntimeError(
+            f"Unknown environment type: {args.env_type}.\nAvailable environment types: {env_choices}"
+        )
+
+    register_handlers(db, handlers)  # noqa: F821


Test patch:
diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml
index 3da1e52f..bd0dab11 100644
--- a/.github/workflows/testing.yml
+++ b/.github/workflows/testing.yml
@@ -44,36 +44,34 @@ jobs:
         with:
           docker-binary: ${{ env.DOCKER_BINARY }}
 
-      - name: Set up Python ${{ matrix.python-version }} with conda
-        uses: conda-incubator/setup-miniconda@v2
+      - name: Set up Python ${{ matrix.python-version }}
+        uses: actions/setup-python@v4
         with:
-          activate-environment: ${{ env.REPOSITORY_NAME }}-py${{ matrix.python-version }}
-          auto-update-conda: true
-          miniconda-version: "latest"
           python-version: ${{ matrix.python-version }}
-          mamba-version: "*"
-          channels: conda-forge
+
+      # - name: Set up Python ${{ matrix.python-version }} with conda
+      #   uses: conda-incubator/setup-miniconda@v2
+      #   with:
+      #     activate-environment: ${{ env.REPOSITORY_NAME }}-py${{ matrix.python-version }}
+      #     auto-update-conda: true
+      #     miniconda-version: "latest"
+      #     python-version: ${{ matrix.python-version }}
+      #     mamba-version: "*"
+      #     channels: conda-forge
 
       - name: Install the package and its dependencies
         run: |
-          # set -vxeuo pipefail
-          # Do not check for unbound variables (the '-u' flag) as it fails on
-          # conda deactivate command:
-          # /usr/share/miniconda3/envs/sirepo-bluesky-py3.9/etc/conda/deactivate.d/glib_deactivate.sh:
-          # line 1: GSETTINGS_SCHEMA_DIR_CONDA_BACKUP: unbound variable
           set -vxeo pipefail
-          conda env list
-          pip install --upgrade pip wheel
-          pip install -v .
-          pip install -r requirements-dev.txt
-          pip list
-          conda list
+          python -m pip install --upgrade pip wheel
+          python -m pip install -v .
+          python -m pip install -r requirements-dev.txt
+          python -m pip list
 
       - name: Copy databroker config file
         run: |
           set -vxeuo pipefail
           mkdir -v -p ~/.config/databroker/
-          cp examples/local.yml ~/.config/databroker/
+          cp -v examples/local.yml ~/.config/databroker/
 
       - name: Test with pytest
         run: |
diff --git a/sirepo_bluesky/tests/test_entrypoint.py b/sirepo_bluesky/tests/test_entrypoint.py
deleted file mode 100644
index a6f28c1f..00000000
--- a/sirepo_bluesky/tests/test_entrypoint.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from databroker import Broker
-
-
-def test_broker():
-    db = Broker.named("local")
-    return db
diff --git a/sirepo_bluesky/tests/test_sirepo_flyer.py b/sirepo_bluesky/tests/test_sirepo_flyer.py
index fd1a181d..90075693 100644
--- a/sirepo_bluesky/tests/test_sirepo_flyer.py
+++ b/sirepo_bluesky/tests/test_sirepo_flyer.py
@@ -68,7 +68,7 @@ def _test_sirepo_flyer(RE_no_plot, db, tmpdir, sim_id, server_name):
     hdr = db[-1]
     t = hdr.table(stream_name="sirepo_flyer")
     db_means = []
-    actual_means = [
+    expected_means = [
         36779651609602.38,
         99449330615601.89,
         149289119385413.34,
@@ -78,7 +78,9 @@ def _test_sirepo_flyer(RE_no_plot, db, tmpdir, sim_id, server_name):
     for i in range(len(t)):
         db_means.append(t.iloc[i]["sirepo_flyer_mean"])
 
-    assert set(actual_means) == set(db_means), "fly scan means do not match actual means"
+    assert np.allclose(
+        sorted(expected_means), sorted(db_means), rtol=1.0e-6
+    ), "fly scan expected means do not match actual means"
 
     durations = []
     for i in range(len(t)):