Exemplo n.º 1
0
    def selectDevice(self, context, key=None):
        """Select a device in our current context."""
        if not len(self.devices):
            raise errors.NoDevicesAvailableError()
        if key is None:
            # use the first device
            key = sorted(self.devices.keys())[0]
        try:
            dev = self.devices[key]
        except KeyError:
            raise errors.NoSuchDeviceError()
        if not dev.accessibleFrom(context.ID):
            raise DeviceLockedError()

        if 'device' in context:
            if context['device'] != dev.guid:
                try:
                    oldDev = self.devices[context['device']]
                except KeyError:
                    pass
                else:
                    # we're trying to select a new device.
                    # make sure to unlock previously selected device
                    if oldDev.lockedInContext(context.ID):
                        oldDev.unlock(context.ID)
                    oldDev.deselect(context)
                context['device'] = dev.guid
                dev.select(context)
        else:
            context['device'] = dev.guid
            dev.select(context)
        return dev
Exemplo n.º 2
0
 def getDevice(self, context, key=None):
     if not len(self.devices):
         raise errors.NoDevicesAvailableError()
     if key is None:
         # use the first device
         key = sorted(self.devices.keys())[0]
     try:
         dev = self.devices[key]
     except KeyError:
         raise errors.NoSuchDeviceError()
     if not dev.accessibleFrom(context.ID):
         raise DeviceLockedError()
     return dev
Exemplo n.º 3
0
 def selectedDevice(self, context):
     """Get the selected device from the given context."""
     if not len(self.devices):
         raise errors.NoDevicesAvailableError()
     try:
         key = context['device']
     except KeyError:
         raise errors.DeviceNotSelectedError()
     try:
         dev = self.devices[key]
     except KeyError:
         raise errors.NoSuchDeviceError()
     if not dev.accessibleFrom(context.ID):
         raise DeviceLockedError()
     return dev