Esempio n. 1
0
    def kh_dbLoadRecords(self, arg0, tokens, ref):
        local_macros = macros.Macros(**self.env.db)
        full_line = reconstruct_line(tokens).strip()
        tokenLog = TokenLog()
        tokenLog.tokenList = tokens
        tokenLog.token_pointer = 1
        args = parse_bracketed_macro_definitions(tokenLog)
        nargs = len(args)
        if nargs not in (1, 2, 3,):
            msg = str(ref) + full_line
            raise UnhandledTokenPattern, msg
        utils.logMessage(arg0 + full_line, utils.LOGGING_DETAIL__NOISY)

        dbFileName = local_macros.replace(utils.strip_quotes(args[0]))

        if nargs in (2, 3,):    # accumulate additional macro definitions
            local_macros = self.parse_macro_args(args[1], ref, tokens, local_macros)

        if nargs in (3,):
            path = args[2]
            # msg = str(ref) + full_line
            if self.symbols.exists(path):   # substitute from symbol table
                path = self.symbols.get(path).value
            if os.path.exists(path):
                dbFileName = os.path.join(path, dbFileName)

        try:
            obj = database.Database(self, dbFileName, ref, **local_macros.db)
            self.database_list.append(obj)
            self.kh_shell_command(arg0, tokens, ref)
        except text_file.FileNotFound, _exc:
            msg = 'Could not find database file: ' + dbFileName
            utils.detailedExceptionLog(msg)
            return
Esempio n. 2
0
    def kh_putenv(self, arg0, tokens, ref):
        '''
        process an instance of putenv

        :param tokens: token list
        :param ref: instance of :class:`FileRef`
        :raise UnhandledTokenPattern: unhandled token pattern
        '''
        argument_list = []
        for tkn in tokens[1:]:
            if tkn['tokName'] == 'STRING':
                argument_list.append( tkn['tokStr'] )

        if len(argument_list) == 1:
            var, arg = utils.strip_quotes( argument_list[0].strip() ).split('=')
            arg = utils.strip_quotes(arg.strip())
            self.env.set(var, arg, self, ref)
        elif len(argument_list) == 2:
            var, arg = argument_list
            arg = utils.strip_quotes(arg.strip())
            self.env.set(var, arg)
        else:
            msg = str(ref) + reconstruct_line(tokens).strip()
            raise UnhandledTokenPattern, msg

        self.kh_shell_command(arg0, tokens, ref)
Esempio n. 3
0
 def kh_loadCommandFile(self, arg0, tokens, ref):
     fname = utils.strip_parentheses(reconstruct_line(tokens).strip())
     # fname is given relative to current working directory
     fname_expanded = self.env.replace(fname)
     self.kh_shell_command('<', tokens, ref)
     try:
         obj = CommandFile(self, fname_expanded, ref, self.env, self.symbols)
         self.includedCommandFile_list.append(obj)
     except FileNotFound, _exc:
         msg = 'File not found: ' + fname_expanded
         utils.detailedExceptionLog(msg)
         return
Esempio n. 4
0
 def kh_cd(self, arg0, tokens, ref):
     path = reconstruct_line(tokens).strip()
     path = utils.strip_quotes(path)           # strip double-quotes
     if self.symbols.exists(path):       # symbol substitution
         path = self.symbols.get(path).value
     path = self.env.replace(path)       # macro substitution
     if len(path) == 0:
         path = self.startup_directory
     if len(path) > 0 and os.path.exists(path):
         if os.path.abspath(path) != os.getcwd():    # only cd if it is really different
             os.chdir(path)
             self.kh_shell_command(arg0, tokens, ref)
             utils.logMessage(arg0 + ' ' + path, utils.LOGGING_DETAIL__MEDIUM)
Esempio n. 5
0
 def kh_dbLoadTemplate(self, arg0, tokens, ref):
     # TODO: Can one template call another?
     local_macros = macros.Macros(**self.env.db)
     args = utils.strip_parentheses(reconstruct_line(tokens).strip()).split(',')
     if len(args) in (1, 2):
         tfile = os.path.join(os.getcwd(), utils.strip_quotes(args[0]))
     if len(args) == 2:
         # such as in 8idi:  dbLoadTemplate("aiRegister.substitutions", top)
         # This is an ERROR.  The IOC should be corrected.
         '''from the EPICS documentation regarding dbLoadTemplate():
         
         dbLoadTemplate(char *subfile, char *substitutions)
         
         This IOC command reads a template substitutions file which 
         provides instructions for loading database instance files 
         and gives values for the $(xxx) macros they may contain. 
         This command performs those substitutions while loading the 
         database instances requested.
         
         The subfile parameter gives the name of the template substitution file to be used. 
         The optional substitutions parameter may contain additional global macro values, 
         which can be overridden by values given within the substitution file.
         '''
         path = self.symbols.get(utils.strip_quotes(args[1]).strip(), None)
         if isinstance(path, macros.KVpair):
             alternative = os.path.join(path.value, tfile)
             if os.path.exists(alternative):
                 tfile = alternative
         else:
             msg = 'problem parsing: ' + arg0 + reconstruct_line(tokens).strip()
             utils.logMessage(msg, utils.LOGGING_DETAIL__IMPORTANT)
     obj = template.Template(tfile, ref, **local_macros.db)
     self.template_list.append(obj)
     self.database_list += obj.database_list
     self.kh_shell_command(arg0, tokens, ref)
     for k, v in obj.getPVs():
         self.pv_dict[k] = v
Esempio n. 6
0
 def kh_epicsEnvSet(self, arg0, tokens, ref):
     '''symbol assignment'''
     if len(tokens) == 7:
         var = tokens[2]['tokStr']
         value = tokens[4]['tokStr']
     else:
         text = utils.strip_parentheses(reconstruct_line(tokens).strip())
         parts = text.split(',')
         if len(parts) == 1:
             parts = text.split(' ')
         if len(parts) != 2:
             raise UnhandledTokenPattern('epicsEnvSet'+text)
         var, value = parts
     self.env.set(utils.strip_quotes( var ), utils.strip_quotes( value ), self, ref)
     self.kh_shell_command(arg0, tokens, ref)
Esempio n. 7
0
 def parse_macro_args(self, arg, ref, tokens, parent_macros):
     local_macros = macros.Macros(**parent_macros.db)
     for definition in utils.strip_quotes(arg).split(','):
         if definition.find('=') < 0:
             # if self.symbols.get(definition, None)
             # such as:  iocSubString=asdCreateSubstitutionString("IOC",iocprefix)
             msg = str(ref) + reconstruct_line(tokens).strip()
             utils.logMessage(msg, utils.LOGGING_DETAIL__IMPORTANT)
             #raise UnhandledTokenPattern, msg
         else:
             k, v = [_.strip() for _ in definition.split('=')]
             # expand macros now to avoid possible infinite loop while replacing
             # example:   PORT=IP_$(PORT)
             v = local_macros.replace(v)
             local_macros.set(k, v, self, ref)
     return local_macros
Esempio n. 8
0
 def kh_shell_command(self, arg0, tokens, ref):
     linetext = reconstruct_line(tokens).strip()
     cmd = Command(self, arg0, self.pwd, linetext, ref, **self.env.db)
     self.commands.append(cmd)