示例#1
0
def CreateAllDevices(**kwargs):
    """Try to create all possible devices in the current setup.

    This is useful when a setup failed to load many devices, and another
    attempt should be made.  Example:

    >>> CreateAllDevices()

    Note: Devices that are marked as lowlevel will not be automatically
    created, unless you set the lowlevel flag like:

    >>> CreateAllDevices(lowlevel=True)

    see also: `CreateDevice`, `RemoveDevice`
    """
    lowlevel = kwargs.get('lowlevel', False)
    if lowlevel and not session.checkUserLevel(ADMIN):
        session.log.error('Creating all lowlevel devices is only allowed '
                          'for admin users')
        lowlevel = False

    session.startMultiCreate()
    try:
        for devname, (_, devconfig) in iteritems(session.configured_devices):
            if devconfig.get('lowlevel', False) and not lowlevel:
                continue
            try:
                session.createDevice(devname, explicit=True)
            except NicosError:
                session.log.exception('error creating %s', devname)
    finally:
        session.endMultiCreate()
示例#2
0
def CreateDevice(*devnames):
    """Create all given devices.

    Examples:

    >>> CreateDevice('det')                 # create "det" device
    >>> CreateDevice('stx', 'sty', 'stz')   # create all of "stx", "sty", "stz"

    CreateDevice can also be used to make lowlevel devices accessible in the
    user namespace.

    see also: `CreateAllDevices`, `RemoveDevice`
    """
    for devname in devnames:
        if not isinstance(devname, string_types):
            raise UsageError('CreateDevice() arguments must be strings')
        session.createDevice(devname, explicit=True)
示例#3
0
文件: secop.py 项目: umithardal/nicos
    def makeDynamicDevices(self, setup_info):
        """create and destroy dynamic devices

        create devices from setup_info, and store the name of the setup
        creating the creator device in session.creator_devices for
        session.getSetupInfo()
        Based on the previous setup_info from self.setup_info,
        devices are created, recreated, destroyed or remain unchanged.
        If setup_info is empty, destroy all devices.
        """
        prevdevices = set(self.setup_info.keys())
        self._setROParam('setup_info', setup_info)

        # find setup of this secnode
        result = session.getSetupInfo()
        for setupname in session.loaded_setups:
            info = result.get(setupname, None)
            if info and self.name in info['devices']:
                break
        else:
            raise ConfigurationError('can not find setup')
        # add new or changed devices
        for devname, devcfg in setup_info.items():
            prevdevices.discard(devname)
            dev = session.devices.get(devname, None)
            if dev:
                if not isinstance(dev, SecopDevice) or (dev._attached_secnode and
                                                        dev._attached_secnode != self):
                    self.log.error('device %s already exists' % devname)
                    continue
                base = dev.__class__.__bases__[0]
                prevcfg = base.__module__ + '.' + base.__name__, dict(secnode=self.name, **dev._config)
            else:
                prevcfg = None
            if prevcfg != devcfg:
                session.configured_devices[devname] = devcfg
                session.dynamic_devices[devname] = setupname  # pylint: disable=undefined-loop-variable
                if dev is None:
                    # add new device
                    session.createDevice(devname, recreate=True, explicit=True)
                    dev = session.devices[devname]
                else:
                    # modify existing device
                    if dev._attached_secnode:
                        dev._attached_secnode.unregisterDevice(dev)
                    session.configured_devices[devname] = devcfg
                    session.dynamic_devices[devname] = setupname  # pylint: disable=undefined-loop-variable
                    try:
                        dev.replaceClass(devcfg[1])
                        dev.setAlive(self)
                    except ConfigurationError:
                        # above failed because an alias or attaching device requires a specific class
                        # make old device defunct and replace by a new device
                        session.destroyDevice(dev)
                        session.dynamic_devices.pop(devname, None)
                        session.createDevice(devname, recreate=True, explicit=True)
                        prevdevices.discard(devname)
                        dev = session.devices[devname]
                if not isinstance(dev, SecopReadable):
                    # we will not get status updates for these
                    dev.updateSecopStatus((SecopStatus.IDLE, ''))

        defunct = set()
        # defunct devices no longer available
        for devname in prevdevices:
            dev = session.devices.get(devname)
            if dev is None or dev._attached_secnode != self:
                continue
            if dev._sdevs:
                self.log.warning('defunct device is attached to %s' % ', '.join(dev._sdevs))
            dev.setDefunct()
            defunct.add(devname)

        # inform client that setups have changed
        session.setupCallback(list(session.loaded_setups), list(session.explicit_setups))