Esempio n. 1
0
    def addCommand(self, arg1, arg2 = None, addNow=True):
        if not addNow:
            self.__commandsToAdd.append( (arg1, arg2) )
            return
        newCommand = None
        
        if type(arg1) == dict:
            attributesDict = arg1
            cmd = arg2
        
            cmdName = attributesDict['name']
            cmdType = attributesDict['type']
            del attributesDict['name']
            del attributesDict['type']
        else:
            attributesDict = {}
            attributesDict.update(arg1.getProperties())

            try:
                cmdName = attributesDict['name']
                cmdType = attributesDict['type']
                cmd = attributesDict['toexecute']
            except KeyError as err:
                logging.getLogger().error('%s: cannot add command: missing "%s" property', self.name(), err.args[0])
                return
            else:
                del attributesDict['name']
                del attributesDict['type']
                del attributesDict['toexecute']
        
        
        if cmdType.lower() == 'spec':
            if not 'version' in attributesDict:
                try:
                    attributesDict['version'] = self.specversion
                except AttributeError:
                    pass

            try:
                from Command.Spec import SpecCommand
                newCommand = SpecCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
        elif cmdType.lower() == 'taco':
            if not 'taconame' in attributesDict:
                try:
                    attributesDict['taconame'] = self.taconame
                except AttributeError:
                    pass

            try:
                from Command.Taco import TacoCommand
                newCommand = TacoCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
        elif cmdType.lower() == 'tango':
            if not 'tangoname' in attributesDict:
                try:
                    attributesDict['tangoname'] = self.tangoname
                except AttributeError:
                    pass
            try:
                from Command.Tango import TangoCommand
                newCommand = TangoCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error('%s: could not connect to device server %s (hint: is it running ?)', self.name(), attributesDict["tangoname"])
                raise ConnectionError
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)

        elif cmdType.lower() == 'exporter':
            if not 'exporter_address' in attributesDict:
              try:
                attributesDict['exporter_address'] = self.exporter_address
              except AttributeError:
                pass
            host, port = attributesDict["exporter_address"].split(":")

            try:
              attributesDict["address"] = host
              attributesDict["port"] = int(port)
              del attributesDict["exporter_address"]

              from Command.Exporter import ExporterCommand
              newCommand = ExporterCommand(cmdName, cmd, **attributesDict)
            except:
              logging.getLogger().exception('%s: cannot add command %s (hint: check attributes)', self.name(), cmdName)
        elif cmdType.lower() == "epics":
            try:
              from Command.Epics import EpicsCommand
              newCommand = EpicsCommand(cmdName, cmd, **attributesDict)
            except:
              logging.getLogger().exception('%s: cannot add EPICS channel %s (hint: check PV name)', self.name(), cmdName)

        elif cmdType.lower() == 'sardana':

            doorname = None
            taurusname = None
            cmdtype = None
            door_first = False
            tango_first = False

            if not 'doorname' in attributesDict:
                try:
                    attributesDict['doorname'] = self.doorname
                    doorname = self.doorname
                except AttributeError:
                    pass
            else:
                door_first = True 
                doorname = attributesDict['doorname']

            if not 'taurusname' in attributesDict:
                try:
                    attributesDict['taurusname'] = self.taurusname
                    taurusname = self.taurusname
                except AttributeError:
                    pass
            else:
                tango_first = True 
                taurusname = attributesDict['taurusname']

            if 'cmdtype' in attributesDict:
                cmdtype = attributesDict['cmdtype']
            
            # guess what kind of command to create
            if cmdtype is None:
                if taurusname is not None and doorname is None:
                     cmdtype = "command"
                elif doorname is not None and taurusname is None:
                     cmdtype = "macro"
                elif doorname is not None and taurusname is not None:
                     if door_first:
                         cmdtype = "macro"
                     elif tango_first:
                         cmdtype = "command"
                     else:
                         cmdtype = "macro"
                else:
                    logging.getLogger().error('%s: incomplete sardana command declaration. ignored', self.name())
            
            from Command.Sardana import SardanaCommand, SardanaMacro
            if cmdtype == 'macro' and doorname is not None:
                try:
                    newCommand = SardanaMacro(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error('%s: could not connect to sardana door %s (hint: is it running ?)', self.name(), attributesDict["doorname"])
                    raise ConnectionError
                except:
                    logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
            elif cmdtype == 'command' and taurusname is not None:
                try:
                    newCommand = SardanaCommand(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error('%s: could not connect to sardana device %s (hint: is it running ?)', self.name(), taurusname)
                    raise ConnectionError
                except:
                    logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
            else:
                logging.getLogger().error('%s: incomplete sardana command declaration. ignored', self.name())
                
        elif cmdType.lower() == 'pool':
            if not 'tangoname' in attributesDict:
                try:
                    attributesDict['tangoname'] = self.tangoname
                except AttributeError:
                    pass
            try:
                from Command.Pool import PoolCommand
                newCommand = PoolCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error('%s: could not connect to device server %s (hint: is it running ?)', self.name(), attributesDict["tangoname"])
                raise ConnectionError
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
        elif cmdType.lower() == 'tine':
            if not 'tinename' in attributesDict:
                try:
                    attributesDict['tinename'] = self.tinename
                except AttributeError:
                    pass

            try:
                from Command.Tine import TineCommand
                newCommand = TineCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)

                
        if newCommand is not None:
            self.__commands[cmdName] = newCommand

            if not type(arg1) == dict:
                i = 1
                for arg in arg1.getObjects('argument'):
                    onchange=arg.getProperty("onchange")
                    if onchange is not None: 
                        onchange=(onchange, weakref.ref(self))
                    valuefrom=arg.getProperty("valuefrom")
                    if valuefrom is not None: 
                        valuefrom=(valuefrom, weakref.ref(self))
                                     
                    try:
                        comboitems=arg["type"]["item"]
                    except IndexError:
                        try:
                            newCommand.addArgument(arg.getProperty('name'), arg.type, onchange=onchange, valuefrom=valuefrom)
                        except AttributeError:
                            logging.getLogger().error('%s, command "%s": could not add argument %d, missing type or name', self.name(), cmdName, i)
                            continue
                    else:
                        if type(comboitems) == list:
                            combo_items = []
                            for item in comboitems:
                                name = item.getProperty('name')
                                value = item.getProperty('value')
                                if name is None or value is None:
                                    logging.getLogger().error("%s, command '%s': could not add argument %d, missing combo item name or value", self.name(), cmdName, i)
                                    continue
                                else:
                                    combo_items.append( (name, value) )
                        else:
                            name = comboitems.getProperty('name')
                            value = comboitems.getProperty('value')
                            if name is None or value is None:
                                combo_items = ( (name, value), )
                            else:
                                logging.getLogger().error("%s, command '%s': could not add argument %d, missing combo item name or value", self.name(), cmdName, i)
                                continue
                            
                        newCommand.addArgument(arg.getProperty('name'), "combo", combo_items, onchange, valuefrom)
                    
                    i += 1                               
                    
            return newCommand
            else:
                del attributesDict['name']
                del attributesDict['type']
                del attributesDict['toexecute']
        
        
        if cmdType.lower() == 'spec':
            if not 'version' in attributesDict:
                try:
                    attributesDict['version'] = self.specversion
                except AttributeError:
                    pass

            try:
                from Command.Spec import SpecCommand
                newCommand = SpecCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
        elif cmdType.lower() == 'taco':
            if not 'taconame' in attributesDict:
                try:
                    attributesDict['taconame'] = self.taconame
                except AttributeError:
                    pass

            try:
                from Command.Taco import TacoCommand
                newCommand = TacoCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception('%s: could not add command "%s" (hint: check command attributes)', self.name(), cmdName)
        elif cmdType.lower() == 'tango':
    def addCommand(self, arg1, arg2=None, addNow=True):
        if not addNow:
            self.__commandsToAdd.append((arg1, arg2))
            return
        newCommand = None

        if type(arg1) == dict:
            attributesDict = arg1
            cmd = arg2

            cmdName = attributesDict['name']
            cmdType = attributesDict['type']
            del attributesDict['name']
            del attributesDict['type']
        else:
            attributesDict = {}
            attributesDict.update(arg1.getProperties())

            try:
                cmdName = attributesDict['name']
                cmdType = attributesDict['type']
                cmd = attributesDict['toexecute']
            except KeyError as err:
                logging.getLogger().error(
                    '%s: cannot add command: missing "%s" property',
                    self.name(), err.args[0])
                return
            else:
                del attributesDict['name']
                del attributesDict['type']
                del attributesDict['toexecute']

        if cmdType.lower() == 'spec':
            if not 'version' in attributesDict:
                try:
                    attributesDict['version'] = self.specversion
                except AttributeError:
                    pass

            try:
                from Command.Spec import SpecCommand
                newCommand = SpecCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(), cmdName)
        elif cmdType.lower() == 'taco':
            if not 'taconame' in attributesDict:
                try:
                    attributesDict['taconame'] = self.taconame
                except AttributeError:
                    pass

            try:
                from Command.Taco import TacoCommand
                newCommand = TacoCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(), cmdName)
        elif cmdType.lower() == 'tango':
            if not 'tangoname' in attributesDict:
                try:
                    attributesDict['tangoname'] = self.tangoname
                except AttributeError:
                    pass
            try:
                from Command.Tango import TangoCommand
                newCommand = TangoCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error(
                    '%s: could not connect to device server %s (hint: is it running ?)',
                    self.name(), attributesDict["tangoname"])
                raise ConnectionError
            except:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(), cmdName)

        elif cmdType.lower() == 'exporter':
            if not 'exporter_address' in attributesDict:
                try:
                    attributesDict['exporter_address'] = self.exporter_address
                except AttributeError:
                    pass
            host, port = attributesDict["exporter_address"].split(":")

            try:
                attributesDict["address"] = host
                attributesDict["port"] = int(port)
                del attributesDict["exporter_address"]

                from Command.Exporter import ExporterCommand
                newCommand = ExporterCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception(
                    '%s: cannot add command %s (hint: check attributes)',
                    self.name(), cmdName)
        elif cmdType.lower() == "epics":
            try:
                from Command.Epics import EpicsCommand
                newCommand = EpicsCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception(
                    '%s: cannot add EPICS channel %s (hint: check PV name)',
                    self.name(), cmdName)

        elif cmdType.lower() == 'sardana':

            doorname = None
            taurusname = None
            cmdtype = None
            door_first = False
            tango_first = False

            if not 'doorname' in attributesDict:
                try:
                    attributesDict['doorname'] = self.doorname
                    doorname = self.doorname
                except AttributeError:
                    pass
            else:
                door_first = True
                doorname = attributesDict['doorname']

            if not 'taurusname' in attributesDict:
                try:
                    attributesDict['taurusname'] = self.taurusname
                    taurusname = self.taurusname
                except AttributeError:
                    pass
            else:
                tango_first = True
                taurusname = attributesDict['taurusname']

            if 'cmdtype' in attributesDict:
                cmdtype = attributesDict['cmdtype']

            # guess what kind of command to create
            if cmdtype is None:
                if taurusname is not None and doorname is None:
                    cmdtype = "command"
                elif doorname is not None and taurusname is None:
                    cmdtype = "macro"
                elif doorname is not None and taurusname is not None:
                    if door_first:
                        cmdtype = "macro"
                    elif tango_first:
                        cmdtype = "command"
                    else:
                        cmdtype = "macro"
                else:
                    logging.getLogger().error(
                        '%s: incomplete sardana command declaration. ignored',
                        self.name())

            from Command.Sardana import SardanaCommand, SardanaMacro
            if cmdtype == 'macro' and doorname is not None:
                try:
                    newCommand = SardanaMacro(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error(
                        '%s: could not connect to sardana door %s (hint: is it running ?)',
                        self.name(), attributesDict["doorname"])
                    raise ConnectionError
                except:
                    logging.getLogger().exception(
                        '%s: could not add command "%s" (hint: check command attributes)',
                        self.name(), cmdName)
            elif cmdtype == 'command' and taurusname is not None:
                try:
                    newCommand = SardanaCommand(cmdName, cmd, **attributesDict)
                except ConnectionError:
                    logging.getLogger().error(
                        '%s: could not connect to sardana device %s (hint: is it running ?)',
                        self.name(), taurusname)
                    raise ConnectionError
                except:
                    logging.getLogger().exception(
                        '%s: could not add command "%s" (hint: check command attributes)',
                        self.name(), cmdName)
            else:
                logging.getLogger().error(
                    '%s: incomplete sardana command declaration. ignored',
                    self.name())

        elif cmdType.lower() == 'pool':
            if not 'tangoname' in attributesDict:
                try:
                    attributesDict['tangoname'] = self.tangoname
                except AttributeError:
                    pass
            try:
                from Command.Pool import PoolCommand
                newCommand = PoolCommand(cmdName, cmd, **attributesDict)
            except ConnectionError:
                logging.getLogger().error(
                    '%s: could not connect to device server %s (hint: is it running ?)',
                    self.name(), attributesDict["tangoname"])
                raise ConnectionError
            except:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(), cmdName)
        elif cmdType.lower() == 'tine':
            if not 'tinename' in attributesDict:
                try:
                    attributesDict['tinename'] = self.tinename
                except AttributeError:
                    pass

            try:
                from Command.Tine import TineCommand
                newCommand = TineCommand(cmdName, cmd, **attributesDict)
            except:
                logging.getLogger().exception(
                    '%s: could not add command "%s" (hint: check command attributes)',
                    self.name(), cmdName)

        if newCommand is not None:
            self.__commands[cmdName] = newCommand

            if not type(arg1) == dict:
                i = 1
                for arg in arg1.getObjects('argument'):
                    onchange = arg.getProperty("onchange")
                    if onchange is not None:
                        onchange = (onchange, weakref.ref(self))
                    valuefrom = arg.getProperty("valuefrom")
                    if valuefrom is not None:
                        valuefrom = (valuefrom, weakref.ref(self))

                    try:
                        comboitems = arg["type"]["item"]
                    except IndexError:
                        try:
                            newCommand.addArgument(arg.getProperty('name'),
                                                   arg.type,
                                                   onchange=onchange,
                                                   valuefrom=valuefrom)
                        except AttributeError:
                            logging.getLogger().error(
                                '%s, command "%s": could not add argument %d, missing type or name',
                                self.name(), cmdName, i)
                            continue
                    else:
                        if type(comboitems) == list:
                            combo_items = []
                            for item in comboitems:
                                name = item.getProperty('name')
                                value = item.getProperty('value')
                                if name is None or value is None:
                                    logging.getLogger().error(
                                        "%s, command '%s': could not add argument %d, missing combo item name or value",
                                        self.name(), cmdName, i)
                                    continue
                                else:
                                    combo_items.append((name, value))
                        else:
                            name = comboitems.getProperty('name')
                            value = comboitems.getProperty('value')
                            if name is None or value is None:
                                combo_items = ((name, value), )
                            else:
                                logging.getLogger().error(
                                    "%s, command '%s': could not add argument %d, missing combo item name or value",
                                    self.name(), cmdName, i)
                                continue

                        newCommand.addArgument(arg.getProperty('name'),
                                               "combo", combo_items, onchange,
                                               valuefrom)

                    i += 1

            return newCommand