script/slaveinfo2xml.py
author Florian Pose <fp@igh-essen.com>
Thu, 12 Jun 2008 13:42:52 +0000
changeset 1008 bfa040859b56
parent 937 e2ee1bed5bd6
permissions -rwxr-xr-x
TODO.
914
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
#!/usr/bin/python
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
#
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
# $Id$
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
#
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
# Convert a slave information file to a slave description Xml.
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
from xml.dom.minidom import Document
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
import sys
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
import re
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
import os
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
import getopt
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
infoFileName = 'info'
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
class PdoEntry:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
    def __init__(self, index, subindex, name, bitlength):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
        self.index = index
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
        self.subindex = subindex
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
        self.name = name
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
        self.bitlength = bitlength
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
    def toXml(self, doc, element):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
        entryElement = doc.createElement('Entry')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
        indexElement = doc.createElement('Index')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
        indexText = doc.createTextNode('#x%04x' % self.index)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        indexElement.appendChild(indexText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
        entryElement.appendChild(indexElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
        if (self.index != 0):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
            subIndexElement = doc.createElement('SubIndex')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
            subIndexText = doc.createTextNode(str(self.subindex))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
            subIndexElement.appendChild(subIndexText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
            entryElement.appendChild(subIndexElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
        lengthElement = doc.createElement('BitLen')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
        lengthText = doc.createTextNode(str(self.bitlength))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
        lengthElement.appendChild(lengthText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
        entryElement.appendChild(lengthElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
        if (self.index != 0):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
            nameElement = doc.createElement('Name')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
            nameText = doc.createTextNode(self.name)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
            nameElement.appendChild(nameText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
            entryElement.appendChild(nameElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
            dataTypeElement = doc.createElement('DataType')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
            dataTypeText = doc.createTextNode(self.dataType())
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
            dataTypeElement.appendChild(dataTypeText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
            entryElement.appendChild(dataTypeElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        element.appendChild(entryElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    def dataType(self):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
        if self.bitlength == 1:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
            return 'BOOL'
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        elif self.bitlength % 8 == 0:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
            if self.bitlength <= 64:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
                return 'UINT%u' % self.bitlength
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
            else:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
                return 'STRING(%u)' % (self.bitlength / 8)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        else:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
            assert False, 'Invalid bit length %u' % self.bitlength 
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
class Pdo:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
    def __init__(self, dir, index):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        self.dir = dir
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
        self.index = index
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
        self.entries = []
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    def appendEntry(self, entry):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
        self.entries.append(entry)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    def toXml(self, doc, element):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        pdoElement = doc.createElement('%sxPdo' % self.dir)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
        indexElement = doc.createElement('Index')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        indexText = doc.createTextNode('#x%04x' % self.index)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
        indexElement.appendChild(indexText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
        pdoElement.appendChild(indexElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
        nameElement = doc.createElement('Name')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
        pdoElement.appendChild(nameElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
        for e in self.entries:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
            e.toXml(doc, pdoElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
        element.appendChild(pdoElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
class Device:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
    def __init__(self):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
        self.vendor = 0
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
        self.product = 0
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        self.revision = 0
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
        self.pdos = []
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    def parseInfoFile(self, fileName):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
        reVendor = re.compile('Vendor ID:.*\((\d+)\)')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
        reProduct = re.compile('Product code:.*\((\d+)\)')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
        reRevision = re.compile('Revision number:.*\((\d+)\)')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        rePdo = re.compile('([RT])xPdo\s+0x([0-9A-F]+)')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
        rePdoEntry = \
937
e2ee1bed5bd6 Adapted to new entry syntax.
Florian Pose <fp@igh-essen.com>
parents: 915
diff changeset
   115
            re.compile('0x([0-9A-F]+):([0-9A-F]+),\s+(\d+) bit,\s+"([^"]*)"')
914
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
        pdo = None
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
        f = open(fileName, 'r')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
        while True:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
            line = f.readline()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
            if not line: break
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
            match = reVendor.search(line)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
            if match:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
                self.vendor = int(match.group(1))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
            match = reProduct.search(line)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
            if match:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
                self.product = int(match.group(1))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
            match = reRevision.search(line)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
            if match:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
                self.revision = int(match.group(1))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
            match = rePdo.search(line)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
            if match:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
                pdo = Pdo(match.group(1), int(match.group(2), 16))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
                self.pdos.append(pdo)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
            match = rePdoEntry.search(line)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
            if match:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
                pdoEntry = PdoEntry(int(match.group(1), 16), \
937
e2ee1bed5bd6 Adapted to new entry syntax.
Florian Pose <fp@igh-essen.com>
parents: 915
diff changeset
   142
                    int(match.group(2), 16), match.group(4), \
e2ee1bed5bd6 Adapted to new entry syntax.
Florian Pose <fp@igh-essen.com>
parents: 915
diff changeset
   143
                    int(match.group(3)))
914
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
                pdo.appendEntry(pdoEntry)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
        f.close()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    def toXmlDocument(self):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
        doc = Document()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
        rootElement = doc.createElement('EtherCATInfo')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
        doc.appendChild(rootElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
        vendorElement = doc.createElement('Vendor')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
        rootElement.appendChild(vendorElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
        vendorIdElement = doc.createElement('Id')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
        idText = doc.createTextNode(str(self.vendor))
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
        vendorIdElement.appendChild(idText)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
        vendorElement.appendChild(vendorIdElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
        descriptionsElement = doc.createElement('Descriptions')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
        rootElement.appendChild(descriptionsElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
        devicesElement = doc.createElement('Devices')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
        descriptionsElement.appendChild(devicesElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
        deviceElement = doc.createElement('Device')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
        devicesElement.appendChild(deviceElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
        typeElement = doc.createElement('Type')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
        typeElement.setAttribute('ProductCode', '#x%08x' % self.product)
915
57907232b901 Fixed RevisionNo.
Florian Pose <fp@igh-essen.com>
parents: 914
diff changeset
   175
        typeElement.setAttribute('RevisionNo', '#x%08x' % self.revision)
914
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        deviceElement.appendChild(typeElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
        for p in self.pdos:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
            p.toXml(doc, deviceElement)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
        return doc
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
def usage():
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
    print """slaveinfo2xml.py [OPTIONS] [FILE]
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
    File defaults to 'info'.
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    Options:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
        -h Print this help."""
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
try:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
    opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
except getopt.GetoptError, err:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    print str(err)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    usage()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    sys.exit(2)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
if len(args) > 1:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    print "Only one argument allowed!"
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
    usage()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    sys.exit(2)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
elif len(args) == 1:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    infoFileName = args[0]
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
for o, a in opts:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
    if o in ("-h", "--help"):
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
        usage()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
        sys.exit()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
    else:
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
        assert False, "unhandled option"
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
d = Device()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
d.parseInfoFile(infoFileName)
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
doc = d.toXmlDocument()
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
# Print our newly created XML
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
print doc.toprettyxml(indent='  ')
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
#-----------------------------------------------------------------------------
c3e398de0d99 Added slaveinfo2xml.py.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222