Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Refractor hooks for move macros #1544

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/sardana/macroserver/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

__all__ = ["OverloadPrint", "PauseEvent", "Hookable", "ExecMacroHook",
"MacroFinder", "Macro", "macro", "iMacro", "imacro",
"MoveMacro", "movemacro",
"MacroFunc", "Type", "Table", "List", "ViewOption",
"LibraryError", "Optional", "StopException", "AbortException",
"InterruptException"]
Expand Down Expand Up @@ -65,6 +66,7 @@
from sardana.macroserver.msoptions import ViewOption

from sardana.taurus.core.tango.sardana.pool import PoolElement
from sardana import sardanacustomsettings


class OverloadPrint(object):
Expand Down Expand Up @@ -2525,3 +2527,12 @@ def __init__(self, *args, **kwargs):

def run(self, *args):
return self._function(self, *args)


class MoveMacro(Macro, Hookable):

hints = {'allowsHooks': ('pre-move', 'post-move')}

enable_hooks = getattr(sardanacustomsettings,
'PRE_POST_MOVE_HOOK_IN_MV',
True)
8 changes: 3 additions & 5 deletions src/sardana/macroserver/macros/hkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import numpy as np

from sardana.sardanautils import py2_round
from sardana.macroserver.macro import Hookable, Macro, iMacro, Type
from sardana.macroserver.macro import Hookable, Macro, iMacro, Type, MoveMacro
from sardana.macroserver.macros.scan import aNscan
from sardana.macroserver.msexception import UnknownEnv

Expand Down Expand Up @@ -196,14 +196,13 @@ def fl(self, ch,
if mat:
return regx.sub(repl, ch)

class br(Macro, _diffrac, Hookable):
class br(MoveMacro, _diffrac):
"""Move the diffractometer to the reciprocal space coordinates given by
H, K and L.
If a fourth parameter is given, the combination of angles to be set is
the correspondig to the given index. The index of the
angles combinations are then changed."""

hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = [
['H', Type.String, None, "H value"],
['K', Type.String, None, "K value"],
Expand Down Expand Up @@ -274,11 +273,10 @@ def run(self, H, K, L, AnglesIndex, FlagNotBlocking, FlagPrinting):
hkl_values[l_idx], self.diffrac.WaveLength])


class ubr(Macro, _diffrac, Hookable):
class ubr(MoveMacro, _diffrac):
"""Move the diffractometer to the reciprocal space coordinates given by
H, K and L und update.
"""
hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = [
["hh", Type.String, "Not set", "H position"],
["kk", Type.String, "Not set", "K position"],
Expand Down
39 changes: 16 additions & 23 deletions src/sardana/macroserver/macros/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@
from PyTango import DevState

from sardana.macroserver.macro import Macro, macro, Type, ViewOption, \
iMacro, Hookable
iMacro, Hookable, MoveMacro
from sardana.macroserver.msexception import StopException, UnknownEnv
from sardana.macroserver.scan.scandata import Record
from sardana.macroserver.macro import Optional
from sardana import sardanacustomsettings

##########################################################################
#
Expand Down Expand Up @@ -460,44 +459,40 @@ def run(self, motor_list):
self.execMacro('wm', motor_list, **Table.PrettyOpts)


class mv(Macro, Hookable):
class mv(MoveMacro):
"""Move motor(s) to the specified position(s)"""

hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = [
['motor_pos_list',
[['motor', Type.Moveable, None, 'Motor to move'],
['pos', Type.Float, None, 'Position to move to']],
None, 'List of motor/position pairs'],
]

def run(self, motor_pos_list):
self.motors, positions = [], []
def prepare(self, motor_pos_list):
self.motors, self.positions = [], []
for m, p in motor_pos_list:
self.motors.append(m)
positions.append(p)

enable_hooks = getattr(sardanacustomsettings,
'PRE_POST_MOVE_HOOK_IN_MV',
True)
self.positions.append(p)

if enable_hooks:
def run(self, motor_pos_list):
if self.enable_hooks:
for preMoveHook in self.getHooks('pre-move'):
preMoveHook()

for m, p in zip(self.motors, positions):
for m, p in zip(self.motors, self.positions):
self.debug("Starting %s movement to %s", m.getName(), p)

motion = self.getMotion(self.motors)
state, pos = motion.move(positions)
state, pos = motion.move(self.positions)
if state != DevState.ON:
self.warning("Motion ended in %s", state.name)
msg = []
for motor in self.motors:
msg.append(motor.information())
self.info("\n".join(msg))

if enable_hooks:
if self.enable_hooks:
for postMoveHook in self.getHooks('post-move'):
postMoveHook()

Expand All @@ -511,20 +506,19 @@ def run(self, motor):
self.info("Motor %s" % str(motor.stateObj.read().rvalue))


class umv(Macro, Hookable):
class umv(MoveMacro):
"""Move motor(s) to the specified position(s) and update"""

hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = mv.param_def

def prepare(self, motor_pos_list, **opts):
self.all_names = []
self.all_pos = []
self.motors = []
# self.motors = []
self.print_pos = False
for motor, pos in motor_pos_list:
self.all_names.append([motor.getName()])
self.motors.append(motor)
#self.motors.append(motor) # here?
pos, posObj = motor.getPosition(force=True), motor.getPositionObj()
self.all_pos.append([pos])
posObj.subscribeEvent(self.positionChanged, motor)
Expand All @@ -533,6 +527,7 @@ def run(self, motor_pos_list):
self.print_pos = True
try:
mv, _ = self.createMacro('mv', motor_pos_list)
self.motors = mv.motors # or here?
mv._setHooks(self.hooks)
self.runMacro(mv)
finally:
Expand Down Expand Up @@ -564,10 +559,9 @@ def printAllPos(self):
self.flushOutput()


class mvr(Macro, Hookable):
class mvr(MoveMacro):
"""Move motor(s) relative to the current position(s)"""

hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = [
['motor_disp_list',
[['motor', Type.Moveable, None, 'Motor to move'],
Expand All @@ -591,10 +585,9 @@ def run(self, motor_disp_list):
self.runMacro(mv)


class umvr(Macro, Hookable):
class umvr(MoveMacro):
"""Move motor(s) relative to the current position(s) and update"""

hints = {'allowsHooks': ('pre-move', 'post-move')}
param_def = mvr.param_def

def run(self, motor_disp_list):
Expand Down