This file is indexed.

/usr/lib/python2.7/dist-packages/sardana/pool/poolbasegroup.py is in python-sardana 1.2.0-2.

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
#!/usr/bin/env python

##############################################################################
##
## This file is part of Sardana
##
## http://www.tango-controls.org/static/sardana/latest/doc/html/index.html
##
## 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 is part of the Python Pool library. It defines the base classes
for"""

__all__ = [ "PoolBaseGroup"]

__docformat__ = 'restructuredtext'

from taurus.core.taurusvalidator import AttributeNameValidator

from sardana import State, ElementType, TYPE_PHYSICAL_ELEMENTS
from poolexternal import PoolExternalObject
from poolcontainer import PoolContainer

class PoolBaseGroup(PoolContainer):

    def __init__(self, **kwargs):
        self._pending = True
        self._user_element_ids = None
        self._user_elements = None
        self._physical_elements = None
        self._physical_elements_set = None
        self._state_statistics = {}
        self._pool = kwargs.pop('pool')
        self.set_user_element_ids(kwargs.pop('user_elements'))
        PoolContainer.__init__(self)
        try:
            self._build_elements()
        except KeyError:
            self.info("failed to build element information. No problem. " \
                      "Probably one or more underlying elements have not " \
                      "been constructed yet")

    def _get_pool(self):
        return self._pool

    def _create_action_cache(self):
        raise NotImplementedError

    def _get_action_cache(self):
        if self._action_cache is None:
            self._action_cache = self._fill_action_cache()
        return self._action_cache

    def _set_action_cache(self, action_cache):
        physical_elements = self.get_physical_elements()
        if self._action_cache is not None:
            for ctrl_physical_elements in physical_elements.values():
                for physical_element in ctrl_physical_elements:
                    action_cache.remove_element(physical_element)

        self._action_cache = self._fill_action_cache(action_cache)

    def _fill_action_cache(self, action_cache=None, physical_elements=None):
        if action_cache is None:
            action_cache = self._create_action_cache()
        if physical_elements is None:
            physical_elements = self.get_physical_elements()
        for _, ctrl_physical_elements in physical_elements.items():
            for physical_element in ctrl_physical_elements:
                action_cache.add_element(physical_element)
        return action_cache

    def _calculate_element_state(self, elem, elem_state_info):
        u_state, u_status = elem_state_info
        if u_status is None:
            u_status = '%s is None' % elem.name
        else:
            u_status = u_status.split("\n", 1)[0]
        return u_state, u_status

    def _calculate_states(self, state_info=None):
        user_elements = self.get_user_elements()
        none, unknown = set(), set()
        fault, alarm, on, moving = set(), set(), set(), set()
        status = []
        if state_info is None:
            state_info = {}
            for elem in user_elements:
                if elem.get_type() == ElementType.External:
                    continue
                # cannot call get_state(us) here since it may lead to dead lock!
                si = elem.inspect_state(), elem.inspect_status()
                state_info[elem] = si
        for elem, elem_state_info in state_info.items():
            elem_type = elem.get_type()
            if elem_type == ElementType.External:
                continue
            u_state, u_status = self._calculate_element_state(elem, elem_state_info)
            if u_state == State.Moving:
                moving.add(elem)
            elif u_state == State.On:
                on.add(elem)
            elif u_state == State.Fault:
                fault.add(elem)
            elif u_state == State.Alarm:
                alarm.add(elem)
            elif u_state is State.Unknown:
                unknown.add(elem)
            elif u_state is None:
                none.add(elem)
            status.append(u_status)
        state = State.On
        if none or unknown:
            state = State.Unknown
        if fault:
            state = State.Fault
        elif alarm:
            state = State.Alarm
        elif moving:
            state = State.Moving
        self._state_statistics = { State.On : on, State.Fault : fault,
                                   State.Alarm : alarm, State.Moving : moving,
                                   State.Unknown : unknown, None : none }
        status = "\n".join(status)
        return state, status

    def _is_managed_element(self, element):
        return True

    def _build_elements(self):
        self._user_elements = []
        self._physical_elements =  {}
        self._physical_elements_set = set()
        
        pool = self._get_pool()
        for user_element_id in self._user_element_ids:
            # an internal element
            internal = type(user_element_id) is int
            if internal:
                try:
                    user_element = pool.get_element(id=user_element_id)
                except KeyError:
                    self._pending = True
                    self._user_elements = None

                    self._physical_elements = None
                    self._physical_elements_set = None
                    raise
                internal = self._is_managed_element(user_element)
                if not internal:
                    user_element_id = user_element.get_source()
            # a tango channel or non internal element (ex: ioregister or motor
            # in measurement group)
            if not internal:
                validator = AttributeNameValidator()
                params = validator.getParams(user_element_id)
                params['pool'] = self._get_pool()
                user_element = PoolExternalObject(**params)
            self.add_user_element(user_element)
        self._pending = False

    def on_element_changed(self, evt_src, evt_type, evt_value):
        pass
        
    def set_user_element_ids(self, new_element_ids):
        self.clear_user_elements()
        self._user_element_ids = new_element_ids
    
    def get_user_element_ids(self):
        """Returns the sequence of user element IDs
        
        :return: the sequence of user element IDs
        :rtype: sequence< :obj:`int`>"""         
        return self._user_element_ids
    
    user_element_ids = property(get_user_element_ids)

    def get_user_elements(self):
        """Returns the sequence of user elements
        
        :return: the sequence of user elements
        :rtype: sequence< :class:`~sardana.pool.poolelement.PoolElement`>""" 
        if self._pending:
            self._build_elements()
        return self._user_elements

    def get_user_elements_attribute_iterator(self):
        """Returns an iterator over the main attribute of each user element.
        
        :return: an iterator over the main attribute of each user element.
        :rtype: iter< :class:`~sardana.sardanaattribute.SardanaAttribute` >""" 
        for element in self.get_user_elements():
            yield element.get_default_attribute()

    get_user_elements_attribute = get_user_elements_attribute_iterator
    get_user_elements_attribute.__doc__ = get_user_elements_attribute_iterator.__doc__
    
    def get_user_elements_attribute_sequence(self):
        """Returns a sequence of main attribute of each user element.
        
        In loops use preferably :meth:`get_user_elements_attribute_iterator` for
        performance and memory reasons.
        
        :return: a sequence of main attribute of each user element.
        :rtype: sequence< :class:`~sardana.sardanaattribute.SardanaAttribute` >""" 
        return list(self.get_user_elements_attribute_iterator())
    
    def get_user_elements_attribute_map(self):
        """Returns a dictionary of main attribute of each user element.
        
        :return: a dictionary of main attribute of each user element.
        :rtype: dict< :class:`~sardana.pool.poolelement.PoolElement`, 
                :class:`~sardana.sardanaattribute.SardanaAttribute` >"""
        ret = {}
        for element in self.get_user_elements():
            ret[element] =  element.get_default_attribute()
        return ret
    
    def get_physical_elements(self):
        """Returns a dictionary or physical elements where key is a controller
        object and value is a sequence of pool elements
        
        :return: a dictionary of physical elements
        :rtype: dict< :class:`~sardana.pool.poolcontroller.PoolController,
                sequence<:class:`~sardana.pool.poolelement.PoolElement`>"""            
        if self._pending:
            self._build_elements()
        return self._physical_elements

    def get_physical_elements_iterator(self):
        """Returns an iterator over the physical elements.
        
        .. warning:: The order is non deterministic.
        
        :return: an iterator over the physical elements.
        :rtype: iter<:class:`~sardana.pool.poolelement.PoolElement` >"""         
        for _, elements in self.get_physical_elements().items():
            for element in elements:
                yield element

    def get_physical_elements_attribute_iterator(self):
        """Returns an iterator over the main attribute of each physical element.
        
        .. warning:: The order is non deterministic.
        
        :return: an iterator over the main attribute of each physical element.
        :rtype: iter< :class:`~sardana.sardanaattribute.SardanaAttribute` >"""         
        for element in self.get_physical_elements_iterator():
            yield element.get_default_attribute()

    def get_physical_elements_set(self):
        if self._pending:
            self._build_elements()
        return self._physical_elements_set

    def add_user_element(self, element, index=None):
        user_elements = self._user_elements
        physical_elements = self._physical_elements
        physical_elements_set = self._physical_elements_set
        
        if element in user_elements:
            raise Exception("Group already contains %s" % element.name)
        if index is None:
            index = len(user_elements)
        user_elements.insert(index, element)
        
        if not self._is_managed_element(element):
            return index

        self.add_element(element)
        self._find_physical_elements(element,
                                     physical_elements=physical_elements,
                                     physical_elements_set=physical_elements_set)
        action_cache = self._action_cache
        if action_cache is not None:
            self._fill_action_cache(action_cache=action_cache,
                                    physical_elements=physical_elements)
        element.add_listener(self.on_element_changed)
        return index

    def _find_physical_elements(self, element, physical_elements=None,
                                physical_elements_set=None):
        elem_type = element.get_type()
        if physical_elements is None:
            physical_elements = {}
        if physical_elements_set is None:
            physical_elements_set = set()
            
        if elem_type in TYPE_PHYSICAL_ELEMENTS:
            ctrl = element.controller
            own_elements = physical_elements.get(ctrl)
            if own_elements is None:
                physical_elements[ctrl] = own_elements = set()
            own_elements.add(element)
            physical_elements_set.add(element)
        else:
            for ctrl, elements in element.get_physical_elements().items():
                own_elements = physical_elements.get(ctrl)
                if own_elements is None:
                    physical_elements[ctrl] = own_elements = set()
                own_elements.update(elements)
                physical_elements_set.update(elements)
        return physical_elements

    # TODO: too complicated to implement for now
#    def remove_user_element(self, element):
#        try:
#            idx = self._user_elements.index(element)
#        except ValueError:
#            raise Exception("Group doesn't contain %s" % element.name)
#        action_cache = self.get_action_cache()
#        element.remove_listener(self.on_element_changed)
#        action_cache.remove_element(element)
#        del self._user_elements[idx]
#        del self._user_element_ids[self._user_element_ids.index(element.id)]
#        self.remove_element(element)

    def clear_user_elements(self):
        user_elements = self._user_elements
        if user_elements is not None:
            for element in user_elements:
                if element.get_type() != ElementType.External:
                    element.remove_listener(self.on_element_changed)
                    self.remove_element(element)
        self._action_cache = None
        self._pending = True
        self._user_elements = None
        self._user_element_ids = None
        self._physical_elements = None


    # --------------------------------------------------------------------------
    # stop
    # --------------------------------------------------------------------------

    def stop(self):
        for ctrl, elements in self.get_physical_elements().items():
            self.debug("Stopping %s %s", ctrl.name, [e.name for e in elements])
            try:
                ctrl.stop_elements(elements=elements)
            except:
                self.error("Unable to stop controller %s", ctrl.name)
                self.debug("Details:", exc_info=1)

    # --------------------------------------------------------------------------
    # abort
    # --------------------------------------------------------------------------

    def abort(self):
        for ctrl, elements in self.get_physical_elements().items():
            self.debug("Aborting %s %s", ctrl.name, [e.name for e in elements])
            try:
                ctrl.abort_elements(elements=elements)
            except:
                self.error("Unable to abort controller %s", ctrl.name)
                self.debug("Details:", exc_info=1)

    # --------------------------------------------------------------------------
    # involved in an operation
    # --------------------------------------------------------------------------

    def get_operation(self):
        for _, elements in self.get_physical_elements().items():
            for element in elements:
                op = element.get_operation()
                if op is not None:
                    return op
        return None