This file is indexed.

/usr/lib/python2.7/dist-packages/sardana/taurus/qt/qtgui/extra_macroexecutor/macrobutton.py is in python-sardana 1.6.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python

##############################################################################
##
## This file is part of Sardana
##
## http://www.sardana-controls.org/
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Sardana is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Sardana is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with Sardana.  If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################

"""
This module provides a button for executing macros
"""

__all__ = ['MacroButton']

import functools
import uuid

import PyTango

import taurus
from taurus.core import TaurusEventType, TaurusDevice
from taurus.external.qt import Qt
from taurus.qt.qtgui.container import TaurusWidget
from taurus.qt.qtgui.base import TaurusBaseWidget
from taurus.qt.qtgui.button import TaurusCommandButton
from taurus.qt.qtgui.dialog import ProtectTaurusMessageBox
from taurus.core.util.colors import DEVICE_STATE_PALETTE
from taurus.qt.qtgui.util.ui import UILoadable


class DoorStateListener(Qt.QObject):
    '''A listener of Change and periodic events from a Door State attribute.
    It converts the received Tango events and emits a Qt signal
    '''

    __pyqtSignals__ = ["doorStateChanged"]

    def eventReceived(self, evt_src, evt_type, evt_value):
        if evt_type not in (TaurusEventType.Change, TaurusEventType.Periodic):
            return
        door_state = evt_value.value
        self.emit(Qt.SIGNAL('doorStateChanged'), door_state)

        
@UILoadable(with_ui='ui')
class MacroButton(TaurusWidget):
    ''' A button to execute/pause/stop macros. The model must be a valid door.
        
    .. todo:: Not implemented but will be needed: set an icon
 
    .. todo:: It may be useful to have all the streams from qdoor available 
             somehow (right-click?)
    '''

    __pyqtSignals__ = ['statusUpdated', 'resultUpdated']

    def __init__(self, parent=None, designMode=False):
        TaurusWidget.__init__(self, parent, designMode)
        self.loadUi()
        self.door = None
        self.door_state_listener = None
        self.macro_name = ''
        self.macro_args = []
        self.macro_id = None
        self.running_macro = None

        self.ui.progress.setValue(0)

        self.ui.button.setCheckable(True)
        self.connect(self.ui.button, Qt.SIGNAL('clicked()'), 
                     self._onButtonClicked)

    def toggleProgress(self, visible):
        '''deprecated'''
        self.warning('toggleProgress is deprecated. Use showProgress')
        self.showProgress(visible)
        
    def showProgress(self, visible):
        '''Set whether the progress bar is shown
        
        :param visible: (bool) If True, the progress bar is shown. Otherwise it 
                        is hidden'''
        self.ui.progress.setVisible(visible)

    def setModel(self, model):
        '''
        reimplemented from :class:`TaurusWidget`. A door device name is 
        expected as the model
        '''
        TaurusWidget.setModel(self, model)
        if self.door is not None:
            self.disconnect(self.door, Qt.SIGNAL('macroStatusUpdated'), self._statusUpdated)
            self.disconnect(self.door, Qt.SIGNAL('resultUpdated'), self._resultUpdated)

            # disable management of Door Tango States
            self.door.getAttribute('State').removeListener(self.door_state_listener)
            self.disconnect(self.door_state_listener, Qt.SIGNAL('doorStateChanged'), self._doorStateChanged)
            self.door_state_listener = None

        try: self.door = taurus.Device(model)
        except: return

        self.connect(self.door, Qt.SIGNAL('macroStatusUpdated'), self._statusUpdated)
        self.connect(self.door, Qt.SIGNAL('resultUpdated'), self._resultUpdated)

        # Manage Door Tango States
        self.door_state_listener = DoorStateListener()
        self.connect(self.door_state_listener, Qt.SIGNAL('doorStateChanged'), self._doorStateChanged)
        self.door.getAttribute('State').addListener(self.door_state_listener)

    def _doorStateChanged(self, state):
        '''slot called on door state changes'''
        color = '#' + DEVICE_STATE_PALETTE.hex(state)
        stylesheet = 'QFrame{border: 4px solid %s;}' % color
        self.ui.frame.setStyleSheet(stylesheet)

        # In case state is not ON, and macro not triggered by the button, disable it
        door_available = True
        if state not in [PyTango.DevState.ON, PyTango.DevState.ALARM] and not self.ui.button.isChecked():
            door_available = False

        self.ui.button.setEnabled(door_available)
        self.ui.progress.setEnabled(door_available)


    def _statusUpdated(self, *args):
        '''slot called on status changes'''
        # SHOULD SEE THE DOCUMENTATION ABOUT THE ARGS AND ALSO THE STATUS STATE MACHINE
        # ARGS FORMAT IS (GUESSING WITH PRINT STATEMENTS)
        # e.g. ((<sardana.taurus.core.tango.sardana.macro.Macro object at 0x7f29300bc210>, [{u'step': 100.0, u'state': u'stop', u'range': [0.0, 100.0], u'id': u'b226f5e8-c807-11e0-8abe-001d0969db5b'}]),)
        # ( (MacroObj, [status_dict, .?.]), .?.)

        # QUESTIONS: THIS MACRO OBJECT HAS ALOS STEP, RANGE, ...
        # AND ALSO THE STATUS DICT... WHICH SHOULD I USE?

        first_tuple = args[0]
        self.running_macro = first_tuple[0]

        status_dict = first_tuple[1][0]
        # KEYS RECEIVED FROM A 'SCAN' MACRO AND A 'TWICE' MACRO: IS IT GENERAL ?!?!?!
        macro_id = status_dict['id']
        # if macro id is unknown ignoring this signal
        if macro_id is None:
            return
        # check if we have launch this macro, otherwise ignore the signal
        if macro_id != str(self.macro_id):
            return
        state = status_dict['state']
        step = status_dict['step']
        step_range = status_dict['range']

        # Update progress bar
        self.ui.progress.setMinimum(step_range[0])
        self.ui.progress.setMaximum(step_range[1])
        self.ui.progress.setValue(step)

        if state in ['stop', 'abort', 'finish', 'alarm']:
            self.ui.button.setChecked(False)

        self.emit(Qt.SIGNAL('statusUpdated'), status_dict)

    def _resultUpdated(self, *args):
        '''slot called on result changes'''
        # ARGS APPEAR TO BE EMPTY... SHOULD THEY CONTAIN THE RESULT ?!?!?!
        # I have to rely on the 'macro object' received in the last status update
        if self.running_macro is None:
            return
        result = self.running_macro.getResult()
        self.emit(Qt.SIGNAL('resultUpdated'), result)

    def setText(self, text):
        '''set the button text
        
        :param text: (str) text for the button
        '''
        self.setButtonText(text)

    def setButtonText(self, text):        
        '''same as :meth:`setText`
        '''
        # SHOULD ALSO BE POSSIBLE TO SET AN ICON
        self.ui.button.setText(text)

    def setMacroName(self, name):
        '''set the name of the macro to be executed
        
        :param name: (str) text for the button
        '''
        self.macro_name = str(name)

    def updateMacroArgument(self, index, value):
        '''change a given argument
        
        :param index: (int) positional index for this argument
        :param value: (str) value for this argument
        '''
        #make sure that the macro_args is at least as long as index
        while len(self.macro_args) < index + 1:
            self.macro_args.append('')
        #update the given argument
        self.macro_args[index] = str(value)
    
    def updateMacroArgumentFromSignal(self, index, obj, signal):
        '''deprecated'''
        msg = 'updateMacroArgumentFromSignal is deprecated. connectArgEditors'
        self.warning(msg)
        self.connect(obj, signal, 
                     functools.partial(self.updateMacroArgument, index))
    
    def connectArgEditors(self, signals):
        '''Associate signals to argument changes. 
        
        :param signals: (seq<tuple>) An ordered sequence of (`obj`, `sig`) 
                        tuples , where `obj` is a parameter editor object and 
                        `sig` is a signature for a signal emitted by `obj` which
                        provides the value of a parameter as its argument.
                        Each (`obj`, `sig`) tuple is associated to parameter
                        corresponding to its position in the `signals` sequence.
                        '''
        
        for i,(obj,sig) in enumerate(signals):
            self.connect(obj, Qt.SIGNAL(sig), 
                         functools.partial(self.updateMacroArgument, i))

    def _onButtonClicked(self):
        if self.ui.button.isChecked():
            self.runMacro()
        else:
            self.abort()

    @ProtectTaurusMessageBox(msg='Error while executing the macro.')
    def runMacro(self):
        '''execute the macro with the current arguments'''
        if self.door is None:
            return

        # Thanks to gjover for the hint... :-D
        #macro_cmd = self.macro_name + ' ' + ' '.join(self.macro_args)
        self.macro_id = uuid.uuid1()
        macro_cmd_xml = '<macro name="%s" id="%s">\n' % \
                        (self.macro_name, self.macro_id)
        for arg in self.macro_args:
            macro_cmd_xml += '<param value="%s"/>\n' % arg
        macro_cmd_xml += '</macro>'
        try:
            #self.door.runMacro(macro_cmd)
            self.door.runMacro(macro_cmd_xml)
        except Exception, e:
            self.ui.button.setChecked(False)
            raise e

    def abort(self):
        '''abort the macro.'''
        if self.door is None:
            return
        self.door.PauseMacro()
        # Since this could be done by error (impatient users clicking more than once)
        # we provide a warning message that does not make the process too slow
        # It may also be useful and 'ABORT' at TaurusApplication level (macros+motions+acquisitions)
        title = 'Aborting macro'
        message = 'The following macro is still running:\n\n'
        message += '%s %s\n\n' % (self.macro_name, ' '.join(self.macro_args))
        message += 'Are you sure you want to abort?\n'
        buttons = Qt.QMessageBox.Ok | Qt.QMessageBox.Cancel
        ans = Qt.QMessageBox.warning(self, title, message, buttons, Qt.QMessageBox.Ok)
        if ans == Qt.QMessageBox.Ok:
            self.door.abort(synch=True)
        else:
            self.ui.button.setChecked(True)
            self.door.ResumeMacro()

    @classmethod
    def getQtDesignerPluginInfo(cls):
        '''reimplemented from :class:`TaurusWidget`'''
        return {'container': False,
                'group': 'Taurus Sardana',
                'module': 'taurus.qt.qtgui.extra_macroexecutor',
                'icon': ':/designer/pushbutton.png'}


class MacroButtonAbortDoor(Qt.QPushButton, TaurusBaseWidget):
    '''Deprecated class. Instead use TaurusCommandButton.
    A button for aborting macros on a door
    '''
    #todo: why not inheriting from (TaurusBaseComponent, Qt.QPushButton)?
    def __init__(self, parent=None, designMode=False):
        name = self.__class__.__name__
        self.call__init__wo_kw(Qt.QPushButton, parent)
        self.call__init__(TaurusBaseWidget, name, designMode=designMode)
        self.warning('Deprecation warning: use TaurusCommandButton class ' +\
                     'instead of MacroButtonAbortDoor')

        self.setText('Abort')
        self.setToolTip('Abort Macro')
        self.connect(self, Qt.SIGNAL('clicked()'), self.abort)

    def getModelClass(self):
        '''reimplemented from :class:`TaurusBaseWidget`'''
        return TaurusDevice

    @ProtectTaurusMessageBox(msg='An error occurred trying to abort the macro.')
    def abort(self):
        '''stops macros'''
        door = self.getModelObj()
        if door is not None:
            door.stopMacro()


if __name__ == '__main__':
    import sys
    from taurus.qt.qtgui.application import TaurusApplication
    from taurus.core.util.argparse import get_taurus_parser
    from sardana.macroserver.macros.test import SarDemoEnv

    parser = get_taurus_parser()
    parser.set_usage("python macrobutton.py [door_name]")
    parser.set_description("Macro button for macro execution")

    app = TaurusApplication(app_name="macrobutton",
                            app_version=taurus.Release.version)

    args = app.get_command_line_args()

    if len(args) < 1:
        parser.print_help(sys.stderr)
        sys.exit(1)

    door_name = args[0]

    w = Qt.QWidget()
    w.setLayout(Qt.QGridLayout())

    col = 0
    w.layout().addWidget(Qt.QLabel('macro name'), 0, col)
    macro_name = Qt.QLineEdit()
    w.layout().addWidget(macro_name, 1, col)

    _argEditors = []
    for a in range(5):
        col += 1
        w.layout().addWidget(Qt.QLabel('arg%d' % a), 0, col)
        argEdit = Qt.QLineEdit()
        w.layout().addWidget(argEdit, 1, col)
        _argEditors.append(argEdit)


    from sardana.taurus.qt.qtcore.tango.sardana.macroserver import registerExtensions
    registerExtensions()
    mb = MacroButton()

    mb.setModel(door_name)

    w.layout().addWidget(mb, 2, 0, 2, 7)

    w.layout().addWidget(Qt.QLabel('Result:'), 4, 0)

    result_label = Qt.QLabel()
    w.layout().addWidget(result_label, 4, 1, 1, 5)

    show_progress = Qt.QCheckBox('Progress')
    show_progress.setChecked(True)
    w.layout().addWidget(show_progress, 5, 0)

    mb_abort = TaurusCommandButton(command = 'StopMacro',
                                   icon=':/actions/media_playback_stop.svg')
    mb_abort.setModel(door_name)

    w.layout().addWidget(mb_abort, 5, 1)

    # Change macro name
    Qt.QObject.connect(macro_name, Qt.SIGNAL('textChanged(QString)'), mb.setMacroName)
    Qt.QObject.connect(macro_name, Qt.SIGNAL('textChanged(QString)'), mb.setButtonText)
    
    # connect the argument editors
    signals = [(e, 'textChanged(QString)') for e in _argEditors]
    mb.connectArgEditors(signals)

    def update_result(result):
        result_label.setText(str(result))

    def toggle_progress(showProgress):
        visible = show_progress.isChecked()
        mb.toggleProgress(visible)

    # Toggle progressbar
    Qt.QObject.connect(show_progress, Qt.SIGNAL('stateChanged(int)'), toggle_progress)
    # Update possible macro result
    Qt.QObject.connect(mb, Qt.SIGNAL('resultUpdated'), update_result)

    # Obtain a demo motor
    try:
        demo_motor_name = SarDemoEnv(door_name).getMotors()[0]
    except Exception, e:
        from taurus.core.util.log import warning, debug
        warning('It was unable to obtain a demo motor')
        debug('Details: %s' % e.message)
        demo_motor_name = ''

    # Since everything is now connected, the parameters will be updated
    macro_name.setText('ascan')
    macro_params = [demo_motor_name, '0', '1', '5', '.1']
    for e,v in zip(_argEditors, macro_params):
        e.setText(v)

    w.show()
    sys.exit(app.exec_())