Calling Macros from other Macros

There are two ways to invoke a Macro from within another Macro.

Here is a slightly modified example code from the Sardana distribution demonstrating how macros are invoked:

#!/usr/bin/env python

from sardana.macroserver.macro import Macro, Type, ParamRepeat

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# a 'runsubs' macro that shows the different ways to call a macro from inside
# another macro
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--~-~-

class runsubs(Macro):
    """ 
    This macro is part of the examples package. It was written for demonstration purposes.
    
    Call type will allow to choose to format in which the ascan macro is called
    from this macro:
     1 - self.execMacro('ascan', motor.getName(), '0', '10', '4', '0.2')
     2 - self.execMacro(['ascan', motor, 0, 10, 4, 0.2])
     3 - params = 'ascan', motor, 0, 10, 4, 0.2
         self.execMacro(params)
     4 - self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
     5 - macro, prep = self.createMacro("ascan %s 0 10 4 0.2" % motor.getName())
         macro.hooks = [ self.hook ]
         self.runMacro(macro)
     6 - macro, prep = self.createMacro('ascan', motor, 0, 10, 4, 0.2)
         macro.hooks = [ self.hook ]
         self.runMacro(macro)
     7 - params = 'ascan', motor, 0, 10, 4, 0.2
         macro, prep = self.createMacro(params)
         macro.hooks = [ self.hook ]
         self.runMacro(macro)
     8 - self.execMacro( 'mv', [[motor, 0]])
     9 - self.execMacro( 'mv', [[motor.getName(), 0],['d1_mot66', 0.05]])
    10 - macro, prep = self.createMacro('mv', [[motor.getName(), 1],['d1_mot66', 0.03]])
         self.runMacro(macro)
        
        Options 5,6 and 7 use the lower level macro API in order to be able to
        attach hooks to the ascan macro."""
    param_def = [
       ['motor',      Type.Motor,   None, 'Motor to move'],
       ['call_type',  Type.Integer, 2, 'type of run to execute internally'],
    ]
    
    def hook(self):
        self.info("executing hook in a step of a scan...")
    
    def run(self, motor, call_type):

        if call_type == 1:
            self.execMacro('ascan', motor.getName(), '0', '10', '4', '0.2')
        elif call_type == 2:
            self.execMacro('ascan', motor, 0, 10, 4, 0.2)
        elif call_type == 3:
            params = 'ascan', motor, 0, 10, 4, 0.2
            self.execMacro(params)
        elif call_type == 4:
            self.execMacro("ascan %s 0 10 4 0.2" % motor.getName())
        elif call_type == 5:
            macro, prep = self.createMacro("ascan %s 0 10 4 0.2" % motor.getName())
            macro.hooks = [ self.hook ]
            self.runMacro(macro)
        elif call_type == 6:
            macro, prep = self.createMacro('ascan', motor, 0, 10, 4, 0.2)
            macro.hooks = [ self.hook ]
            self.runMacro(macro)
        elif call_type == 7:
            params = 'ascan', motor, 0, 10, 4, 0.2
            macro, prep = self.createMacro(params)
            macro.hooks = [ self.hook ]
            self.runMacro(macro)
        #
        # the macro mv uses the ParamRepeat() feature for the definition
        # of the input parameters. This requires a different syntax.
        # Whether a macro uses ParamRepeat() can be checked by, e.g.:
        # spock> prdef mv
        #
        elif call_type == 8:
            self.execMacro( 'mv', [[motor, 0]])
        elif call_type == 9:
            self.execMacro( 'mv', [[motor.getName(), 0],['d1_mot66', 0.05]])
        elif call_type == 10:
            macro, prep = self.createMacro('mv', [[motor.getName(), 1],['d1_mot66', 0.03]])
            self.runMacro(macro)
        else:
            self.output( "runsubs: failed to identify type")
            return 
        self.output("runsubs: DONE")



2019-11-13