Changeset 261


Ignore:
Timestamp:
Feb 6, 2011 6:58:11 PM (14 years ago)
Author:
jemian
Message:

writes analyses to XML file, needs improvement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified topdoc/src/TopDoc/CmdReader.py

    r260 r261  
    2222import pprint
    2323import CommandTable
     24import xml.etree.ElementTree as ET
    2425
    2526
     
    4041    '''
    4142
    42     def __init__(self, filename, symbol, env, commands):
     43    def __init__(self, filename, symbol, env, commands, includeFiles):
    4344        '''
    4445        start the process
     
    5354        self.commands = commands
    5455        self.filename = filename
     56        self.includeFiles = includeFiles
    5557        self.os_path, self.os_filename = os.path.split(os.path.join(os.getcwd(), filename))
    5658        self.analysis = self.tokenAnalysis(filename)
     
    6567        self.os_filename = None     # discovered name of file
    6668        self.includes = []          # any command files included from filename
    67         self.lines = {}             # token list
     69        #self.lines = {}             # token list
    6870        self.analysis = {}
    6971        self.symbol = None
     
    9597                if line['tokens'][0]['tokStr'] == '<':
    9698                    incFile = os.path.join( cwd, line['readline'].strip()[1:].split()[0] )
     99                    self.includeFiles.assign(incFile, None, self.filename, lineNumber)
    97100                    self.includes.append(
    98101                      {
    99102                         'lineNumber': lineNumber,
    100103                         'filename': incFile,
    101                          'reader': CmdReader(incFile, self.symbol, self.env, self.commands)
     104                         'reader': CmdReader(incFile, self.symbol, self.env, self.commands, self.includeFiles)
    102105                       }
    103106                    )
     
    109112                    self.symbol.assign(var, arg, self.filename, lineNumber)
    110113                elif line['tokens'][0]['tokStr'] == 'putenv':
     114                    # arguments might not be in token[1] if using parentheses!
    111115                    var, arg = line['tokens'][1]['tokStr'].strip('"').split('=')
    112116                    self.env.assign(var, arg, self.filename, lineNumber)
     117                #elif line['tokens'][0]['tokStr'] == 'epicsEnvSet':
     118                #    var, arg = line['tokens'][1]['tokStr'].strip('"').split('=')
     119                #    self.env.assign(var, arg, self.filename, lineNumber)
    113120                elif line['tokens'][0]['tokStr'] == 'cd':
    114121                    arg = line['tokens'][1]['tokStr']
     
    125132                    self.commands.record(cmd, arg, self.filename, lineNumber)
    126133
     134    def __xmlNode_Dict(self, parent, tag, name, dict):
     135        '''
     136        write dictionary as subnode to XML file
     137        @param parent: parent XML node
     138        @param tag: name for this XML node
     139        @param name: attribute of tag
     140        @param dict: dictionary to write
     141        @return: XML node
     142        @note: Move this routine to another class, it is not recursive
     143        '''
     144        node = ET.SubElement(parent, tag)
     145        node.attrib["name"] = name
     146        for k, v in dict.items():
     147            self.__xmlNode_NameValue(node, k, None, str( v ))
     148        return node
     149
     150    def __xmlNode_NameValue(self, parent, tag, name, value):
     151        '''
     152        write name/value pair as subnode to XML file
     153        @param parent: parent XML node
     154        @param tag: name for this XML node
     155        @param name: attribute of tag
     156        @param value: value to write
     157        @return: XML node
     158        @note: Move this routine to another class, it is not recursive
     159        '''
     160        node = ET.SubElement(parent, tag)
     161        if name != None:
     162            node.attrib["name"] = name
     163        if value != None:
     164            node.text = str( value )
     165        return node
     166
     167    def toXml(self, xmlFile):
     168        '''
     169        write class data to XML file
     170        '''
     171        root = ET.Element("CmdReader")
     172       
     173        lists = {
     174                    "env": self.env,
     175                    "symbol": self.symbol,
     176                    "commands": self.commands,
     177                    "includeFiles": self.includeFiles
     178                 }
     179        for k, v in lists.items():
     180            node = ET.SubElement(root, "list")
     181            node.attrib["name"] = k
     182            item = 0
     183            for dict in v.list:
     184                item += 1
     185                self.__xmlNode_Dict(node, k, str(item), dict)
     186
     187        # this only gets the first file!
     188        node = ET.SubElement(root, "analysis")
     189        node.attrib["name"] = self.filename
     190        for lineNumber in self.analysis['numbers']:
     191            dict = self.analysis[lineNumber]
     192            line = ET.SubElement(node, "line")
     193            line.attrib["number"] = str(lineNumber)
     194            for item in "pattern readline".split():
     195                self.__xmlNode_NameValue(line, item, None, dict[item].strip())
     196            tokenNodes = ET.SubElement(line, "tokens")
     197            count = 0
     198            for token in dict['tokens']:
     199                count += 1
     200                tokenNode = ET.SubElement(tokenNodes, "token")
     201                tokenNode.attrib['number'] = str(count)
     202                for item in "tokName tokType start end tokStr".split():
     203                    self.__xmlNode_NameValue(tokenNode, item, None, str(token[item]).strip())
     204       
     205        # wrap it in an ElementTree instance, and save as XML
     206        tree = ET.ElementTree(root)
     207        tree.write(xmlFile)
     208
    127209
    128210if __name__ == '__main__':
     
    136218    env = SymbolTable.Symbols()
    137219    commands = CommandTable.Commands()
    138     cr = CmdReader( dict['cmdFile'], symbols, env, commands)
     220    includeFiles = SymbolTable.Symbols()
     221    cr = CmdReader( dict['cmdFile'], symbols, env, commands, includeFiles)
    139222
    140223    os.chdir( pwd )
    141 
    142     print "\n\n Environment variables:"
    143     for dict in env.list:
    144         print '<%s,%d> %s = %s' % (dict['file'], dict['line'], dict['name'], dict['value'])
    145 
    146     print "\n\n Global symbol table:"
    147     for dict in symbols.list:
    148         print '<%s,%d> %s = %s' % (dict['file'], dict['line'], dict['name'], dict['value'])
    149 
    150     print "\n\n Global command list:"
    151     for dict in commands.list:
    152         print '<%s,%d> %s %s' % (dict['file'], dict['line'], dict['command'], dict['args'])
     224    cr.toXml("myTestXmlFile.xml")
Note: See TracChangeset for help on using the changeset viewer.