Skip to content

Commit

Permalink
Merge pull request #67 from teddyrendahl/empty_happi
Browse files Browse the repository at this point in the history
FIX: Catch empty happi databases
  • Loading branch information
ZLLentz authored Mar 8, 2018
2 parents 3fc6c6b + 75bd1da commit 800bf19
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions hutch_python/happi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def get_happi_objs(db, hutch):
# Assume we want hutch devices that are active
reqs = dict(beamline=hutch.upper(), active=True)
containers = client.search(**reqs)
if not containers:
logger.warning("No devices found in database for %s",
hutch.upper())
return dict()
# Instantiate the devices needed
dev_namespace = load_devices(*containers, pprint=False)
return dev_namespace.__dict__
Expand Down
6 changes: 5 additions & 1 deletion hutch_python/qs_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def get_qs_objs(proposal, run):
else:
qs_client = happi.Client(database=QSBackend(run, proposal,
use_kerberos=True))

# Create namespace
if not qs_client.all_devices:
logger.warning("No devices found in PCDS Questionnaire for %s",
proposal)
return dict()
dev_namespace = load_devices(*qs_client.all_devices, pprint=False)
return dev_namespace.__dict__
return {}
6 changes: 5 additions & 1 deletion hutch_python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def log_queue():


class QSBackend:
empty = False

def __init__(self, run, proposal, use_kerberos=True, user=None, pw=None):
self.run = run
self.proposal = proposal
Expand All @@ -79,7 +81,9 @@ def find(self, multiples=False, **kwargs):
'pw': self.pw,
'kerberos': self.kerberos,
'proposal': self.proposal}]
if multiples:
if self.empty:
return None
elif multiples:
return devices
else:
return devices[0]
Expand Down
7 changes: 7 additions & 0 deletions hutch_python/tests/test_happi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os.path
import logging
import simplejson
import tempfile

from hutch_python.happi import get_happi_objs, get_lightpath

Expand All @@ -14,6 +16,11 @@ def test_happi_objs():
objs = get_happi_objs(db, 'tst')
assert len(objs) == 2
assert all([obj.active for obj in objs.values()])
# Make sure we can handle an empty JSON file
with tempfile.NamedTemporaryFile('w+') as tmp:
simplejson.dump(dict(), tmp)
tmp.seek(0)
assert get_happi_objs(tmp.name, 'tst') == {}


def test_get_lightpath():
Expand Down
4 changes: 4 additions & 0 deletions hutch_python/tests/test_questionnaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def test_qs_load():
assert objs['inj_x'].run == '15'
assert objs['inj_x'].proposal == 'LR12'
assert objs['inj_x'].kerberos == 'True'
# Check that we can handle an empty Questionnaire
QSBackend.empty = True
assert get_qs_objs('LR12', '15') == dict()
QSBackend.empty = False


def test_ws_auth_conf(temporary_config):
Expand Down

0 comments on commit 800bf19

Please sign in to comment.