Skip to content

Commit

Permalink
Merge pull request #41 from teddyrendahl/quest_preplugin
Browse files Browse the repository at this point in the history
ENH: Experimental Plugin as MetaPlugin
  • Loading branch information
ZLLentz authored Jan 31, 2018
2 parents 7e79ef5 + d24c493 commit 587a2b2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 33 deletions.
14 changes: 13 additions & 1 deletion hutch_python/plugins/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@
class Plugin(BasePlugin):
"""
Plugin to load experiment-spefic includes.
By default, the Experiment plugin attempts to load information from the
PCDS Questionnaire. You can disable this by setting, ``questionnaire:
False`` underneath the ``experiment`` section in your YAML configuration
"""
name = 'experiment'

def pre_plugins(self):
if self.info.get('questionnaire', True):
from .questionnaire import Plugin as QSPlugin
return [QSPlugin(conf=self.conf)]

def get_objects(self):
expname = self.info['name']
proposal = self.info['proposal']
run = self.info['run']
expname = proposal + str(run)
expname = expname.lower()
if expname[:4] == 'auto':
expname = self.get_experiment_name()
logger.info('Loading experiment %s', expname)
Expand Down
3 changes: 2 additions & 1 deletion hutch_python/tests/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ load:
- hutch.py

experiment:
name: mfxx000
proposal: mfxx0
run: 10
import: experiment.User() as x

namespace:
Expand Down
25 changes: 25 additions & 0 deletions hutch_python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,28 @@ def restore_logging():


Experiment = namedtuple('Experiment', ('run', 'proposal'))


class QSBackend:
def __init__(self, run, proposal):
self.run = run
self.proposal = proposal

def find(self, multiples=False, **kwargs):
devices = [{
'_id': 'TST:USR:MMN:01',
'beamline': 'TST',
'device_class': 'hutch_python.tests.conftest.Experiment',
'location': 'Hutch-main experimental',
'args': ['{{run}}', '{{proposal}}'],
'kwargs': {},
'name': 'inj_x',
'prefix': 'TST:USR:MMN:01',
'purpose': 'Injector X',
'type': 'Device',
'run': self.run,
'proposal': self.proposal}]
if multiples:
return devices
else:
return devices[0]
File renamed without changes.
Empty file.
27 changes: 21 additions & 6 deletions hutch_python/tests/test_plugins/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,48 @@

import pytest

import hutch_python.plugins.questionnaire
from hutch_python.plugins.experiment import Plugin

from ..conftest import QSBackend

logger = logging.getLogger(__name__)


def test_experiment_plugin():
logger.debug('test_experiment_plugin')

info = {'name': 'sample_expname',
info = {'proposal': 'sample', 'run': '_expname',
'import': 'experiment'}
conf = dict(experiment=info)
plugin = Plugin(conf)
objs = plugin.get_objects()
assert 'sample_plan' in objs
assert 'another' in objs

info = {'name': 'sample_expname',
info = {'proposal': 'sample', 'run': '_expname',
'import': 'experiment.sample_plan'}
conf = dict(experiment=info)
plugin = Plugin(conf)
objs = plugin.get_objects()
assert 'sample_plan' in objs
assert 'another' not in objs

info = {'name': 'sample_expname',
info = {'proposal': 'sample', 'run': '_expname',
'import': 'experiment.sample_plan()'}
conf = dict(experiment=info)
plugin = Plugin(conf)
objs = plugin.get_objects()
assert objs['sample_plan'] == 5

info = {'name': 'sample_expname',
info = {'proposal': 'sample', 'run': '_expname',
'import': 'experiment as x'}
conf = dict(experiment=info)
plugin = Plugin(conf)
objs = plugin.get_objects()
assert 'x' in objs

info = {'name': 'sample_expname',
info = {'proposal': 'sample', 'run': '_expname',
'import': 'experiment.sample_plan as x, y'}
conf = dict(experiment=info)
plugin = Plugin(conf)
Expand All @@ -52,9 +55,21 @@ def test_experiment_plugin():
def test_experiment_auto():
logger.debug('test_experiment_auto')

info = {'name': 'automatic',
info = {'proposal': 'automatic',
'run': '15',
'import': 'experiment'}
conf = dict(experiment=info)
plugin = Plugin(conf)
with pytest.raises(NotImplementedError):
plugin.get_objects()


def test_questionnaire_preplugin():
info = {'run': 15, 'proposal': 'LR12'}
conf = dict(experiment=info)
plugin = Plugin(conf)
hutch_python.plugins.questionnaire.QSBackend = QSBackend
pre_plugins = plugin.pre_plugins()
objs = pre_plugins[0].get_objects()
assert objs['inj_x'].run == '15'
assert objs['inj_x'].proposal == 'LR12'
26 changes: 1 addition & 25 deletions hutch_python/tests/test_plugins/test_questionnaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,11 @@
import hutch_python.plugins.questionnaire
from hutch_python.plugins.questionnaire import Plugin

from ..conftest import QSBackend

logger = logging.getLogger(__name__)


class QSBackend:
def __init__(self, run, proposal):
self.run = run
self.proposal = proposal

def find(self, multiples=False, **kwargs):
devices = [{
'_id': 'TST:USR:MMN:01',
'beamline': 'TST',
'device_class': 'hutch_python.tests.conftest.Experiment',
'location': 'Hutch-main experimental',
'args': ['{{run}}', '{{proposal}}'],
'kwargs': {},
'name': 'inj_x',
'prefix': 'TST:USR:MMN:01',
'purpose': 'Injector X',
'type': 'Device',
'run': self.run,
'proposal': self.proposal}]
if multiples:
return devices
else:
return devices[0]


def test_questionnaire_plugin():
logger.debug("test_questionnaire_plugin")
conf = {'experiment': {'run': '15', 'proposal': 'LR12'},
Expand Down

0 comments on commit 587a2b2

Please sign in to comment.