Skip to content

Commit

Permalink
Merge pull request #1027 from BCDA-APS/1026-certain-imports
Browse files Browse the repository at this point in the history
allow package import even if databroker v2.0+ is installed in environment
  • Loading branch information
prjemian authored Oct 24, 2024
2 parents 6692d5b + b3a93bb commit e101bab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 48 deletions.
4 changes: 2 additions & 2 deletions apstools/devices/aps_data_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
from ophyd import Device
from ophyd import Signal

from ..utils import run_in_thread

logger = logging.getLogger(__name__)

NOT_AVAILABLE = "-n/a-"
Expand Down Expand Up @@ -191,6 +189,8 @@ def start_workflow(self, workflow="", timeout=TIMEOUT_DEFAULT, **kwargs):
The reporting process will continue until the workflow ends or the
timeout period is exceeded. It does not affect the actual workflow.
"""
from ..utils import run_in_thread

if workflow == "":
workflow = self.workflow.get()
else:
Expand Down
14 changes: 0 additions & 14 deletions apstools/utils/_core.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
from enum import Enum

import databroker._drivers.mongo_normalized
import databroker._drivers.msgpack
import intake
import pandas
import pyRestTable

FIRST_DATA = "1995-01-01"
LAST_DATA = "2100-12-31"

CATALOG_CLASSES = (
databroker.Broker,
databroker._drivers.mongo_normalized.BlueskyMongoCatalog,
databroker._drivers.msgpack.BlueskyMsgpackCatalog,
intake.Catalog,
)
MONGO_CATALOG_CLASSES = (
databroker.Broker,
databroker._drivers.mongo_normalized.BlueskyMongoCatalog,
# intake.Catalog,
)
MAX_EPICS_STRINGOUT_LENGTH = 40


Expand Down
43 changes: 28 additions & 15 deletions apstools/utils/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

import logging

import databroker
import databroker._drivers.mongo_normalized
import databroker._drivers.msgpack
import databroker.queries
import pandas as pd
import pyRestTable

from ._core import CATALOG_CLASSES
from .list_runs import getRunData
from .profile_support import getDefaultNamespace
from .profile_support import ipython_shell_namespace
Expand Down Expand Up @@ -85,8 +80,11 @@ def findCatalogsInNamespace():


def getCatalog(ref=None):
"""Return a catalog object."""
from databroker import catalog

if isinstance(ref, str): # and ref in databroker.catalog:
return databroker.catalog[ref]
return catalog[ref]
if ref is not None and hasattr(ref, "v2"):
return ref.v2

Expand Down Expand Up @@ -120,23 +118,28 @@ def getDatabase(db=None, catalog_name=None):
(new in release 1.4.0)
"""
from databroker import catalog

if not hasattr(db, "v2"):
# fmt: off
if (
hasattr(catalog_name, "name")
and catalog_name in databroker.catalog
and catalog_name in catalog
):
# in case a catalog was passed as catalog_name
db = catalog_name
elif catalog_name is None:
db = getDefaultDatabase()
else:
db = databroker.catalog[catalog_name]
db = catalog[catalog_name]
# fmt: on
return db.v2


def getDefaultCatalog():
"""Return the default databroker catalog."""
from databroker import catalog

cats = findCatalogsInNamespace()
if len(cats) == 1:
return cats[list(cats.keys())[0]]
Expand All @@ -151,11 +154,11 @@ def getDefaultCatalog():
"No catalog defined. Multiple catalog objects available. Specify one of these: {choices}"
)

cats = list(databroker.catalog)
cats = list(catalog)
if len(cats) == 1:
return databroker.catalog[cats[0]]
return catalog[cats[0]]
if len(cats) > 1:
choices = " ".join([f'databroker.catalog["{k}"]' for k in cats])
choices = " ".join([f'catalog["{k}"]' for k in cats])
raise ValueError(
"No catalog defined. "
"Multiple catalog configurations available."
Expand Down Expand Up @@ -191,6 +194,13 @@ def getDefaultDatabase():
(new in release 1.4.0)
"""
from databroker import Broker
from databroker import catalog as db_catalog
from databroker._drivers.mongo_normalized import BlueskyMongoCatalog
from databroker._drivers.msgpack import BlueskyMsgpackCatalog
from intake import Catalog

CATALOG_CLASSES = (Broker, BlueskyMongoCatalog, BlueskyMsgpackCatalog, Catalog)
# look through the console namespace
g = ipython_shell_namespace()
if len(g) == 0:
Expand All @@ -211,8 +221,8 @@ def getDefaultDatabase():

# get the most recent run from each
time_ref = {}
for cat_name in list(databroker.catalog):
cat = databroker.catalog[cat_name]
for cat_name in list(db_catalog):
cat = db_catalog[cat_name]
if cat in db_list:
if len(cat) > 0:
run = cat.v2[-1]
Expand Down Expand Up @@ -387,15 +397,18 @@ def quantify_md_key_use(
tune_mr 1
========================== =====
"""
from databroker import catalog
from databroker.queries import TimeRange

key = key or "plan_name"
catalog_name = catalog_name or "mongodb_config"
query = query or {}
since = since or "1995-01-01"
until = until or "2100-12-31"

cat = (
(db or databroker.catalog[catalog_name])
.v2.search(databroker.queries.TimeRange(since=since, until=until))
(db or catalog[catalog_name])
.v2.search(TimeRange(since=since, until=until))
.search(query)
)

Expand Down
17 changes: 9 additions & 8 deletions apstools/utils/list_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@
import warnings
from collections import defaultdict

import databroker
import databroker._drivers.mongo_normalized
import databroker._drivers.msgpack
import databroker.queries

from ._core import FIRST_DATA
from ._core import LAST_DATA
from ._core import MONGO_CATALOG_CLASSES
from ._core import TableStyle
from .query import db_query

Expand Down Expand Up @@ -345,11 +339,13 @@ def _check_cat(self):

def _apply_search_filters(self):
"""Search for runs from the catalog."""
from databroker.queries import TimeRange

since = self.since or FIRST_DATA
until = self.until or LAST_DATA
self._check_cat()
query = {}
query.update(databroker.queries.TimeRange(since=since, until=until))
query.update(TimeRange(since=since, until=until))
query.update(self.query or {})
cat = self.cat.v2.search(query)
return cat
Expand Down Expand Up @@ -385,6 +381,10 @@ def _sort(uid):
exc,
)
else:
from databroker import Broker
from databroker._drivers.mongo_normalized import BlueskyMongoCatalog

MONGO_CATALOG_CLASSES = (Broker, BlueskyMongoCatalog)
if isinstance(cat, MONGO_CATALOG_CLASSES) and self.sortby == "time":
if self.reverse:
# the default rendering: from MongoDB in reverse time order
Expand Down Expand Up @@ -586,12 +586,13 @@ def summarize_runs(since=None, db=None):
Instance of ``databroker.Broker()``
(default: ``db`` from the IPython shell)
"""
from databroker.queries import TimeRange
from . import ipython_shell_namespace

db = db or ipython_shell_namespace()["db"]
# no APS X-ray experiment data before 1995!
since = since or "1995"
cat = db.v2.search(databroker.queries.TimeRange(since=since))
cat = db.v2.search(TimeRange(since=since))
plans = defaultdict(list)
t0 = time.time()
for n, uid in enumerate(cat):
Expand Down
5 changes: 3 additions & 2 deletions apstools/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from collections import defaultdict
from functools import wraps

import databroker
import ophyd
import pyRestTable
from bluesky import plan_stubs as bps
Expand Down Expand Up @@ -329,6 +328,8 @@ def replay(headers, callback=None, sort=True):
(new in apstools release 1.1.11)
"""
from databroker import Header

# fmt: off
callback = callback or ipython_shell_namespace().get(
"bec", # get from IPython shell
Expand Down Expand Up @@ -363,7 +364,7 @@ def decreasing_time_sorter(run):
# fmt: on

for h in sorted(runs, key=sorter):
if not isinstance(h, databroker.Header):
if not isinstance(h, Header):
# fmt: off
raise TypeError(
f"Must be a databroker Header: received: {type(h)}: |{h}|"
Expand Down
10 changes: 3 additions & 7 deletions apstools/utils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
~db_query
"""

import databroker

from ._core import FIRST_DATA
from ._core import LAST_DATA

Expand Down Expand Up @@ -37,6 +35,7 @@ def db_query(db, query):
:func:`databroker.catalog.search`
"""
from databroker.queries import TimeRange

if query is None:
return db
Expand All @@ -50,11 +49,8 @@ def db_query(db, query):
if not until:
until = LAST_DATA

# fmt: off
_db = db.v2.search(
databroker.queries.TimeRange(since=since, until=until)
)
# fmt: on
time_span = TimeRange(since=since, until=until)
_db = db.v2.search(time_span)
else:
_db = db

Expand Down

0 comments on commit e101bab

Please sign in to comment.