Permalink
Browse files

Tools: AVR PDF: Cheap and simple parsing of the XML files.

  • Loading branch information...
salkinium committed Jun 3, 2013
1 parent 0a4b608 commit 153228962b844b8b6980b1189d077b0df717c58c
@@ -0,0 +1 @@
*.pyc
@@ -0,0 +1,117 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013, Roboterclub Aachen e.V.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Roboterclub Aachen e.V. nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ROBOTERCLUB AACHEN E.V. ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ROBOTERCLUB AACHEN E.V. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
import re, os, sys
import xml.etree.ElementTree as et
import xml.parsers.expat
from string import Template
from parser_exception import ParserException
from device_element import DeviceElementBase, DeviceString
# add python module logger to path
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'logger'))
from logger import Logger
class PartDescriptionFile:
""" PartDescriptionFile
Represents a device in xml format.
"""
def __init__(self, xml_file, logger=None):
self.pnode = self._openDeviceXML(xml_file)
node = self.pnode
if logger == None:
self.log = Logger()
else:
self.log = logger
self.device = node.findall('devices')[0][0]
self.name = self.device.get('name')
self.architecture = self.device.get('architecture')
self.family = self.device.get('family')
self.properties = {}
for memory_segment in self.device.iter('memory-segment'):
name = memory_segment.get('name')
size = int(memory_segment.get('size'), 16)
if name == 'FLASH':
self.properties['flash'] = size
elif name == 'IRAM':
self.properties['ram'] = size
elif name == 'EEPROM':
self.properties['eeprom'] = size
self.properties['interrupts'] = []
for module in self.device.iter('interrupt'):
self.properties['interrupts'].append(module.attrib)
self.modules = node.findall('modules')[0]
self.properties['modules'] = []
ignore_modules = ['LOCKBIT', 'FUSE', 'EEPROM', 'CPU', 'WATCHDOG']
for module in self.modules.iter('module'):
name = module.get('name')
if name not in ignore_modules:
self.properties['modules'].append(name)
def _openDeviceXML(self, filename):
try:
# parse the xml-file
xmltree = et.parse(filename).getroot()
except OSError as e:
raise ParserException(e)
except (xml.parsers.expat.ExpatError, xml.etree.ElementTree.ParseError) as e:
raise ParserException("while parsing xml-file '%s': %s" % (filename, e))
"""
TODO: Validate!!
try:
import lxml.etree # for validating
# validate against the embedded DTD file
try:
parser = lxml.etree.XMLParser(dtd_validation=True)
dummy = lxml.etree.parse(filename, parser)
except lxml.etree.XMLSyntaxError as e:
raise ParserException("Validation error in '%s': %s" % (filename, e))
else:
logging.debug("Validation OK!")
except ImportError as e:
logging.warning("Warning: couldn't load 'lxml' module. No validation done!")
"""
# since we have totally "validated" our xml file (see above) we can
# expect that all required elements exist...
return xmltree
def __str__(self):
s = "Architecture: " + self.architecture + "\n"
s += "Family: " + self.family + "\n"
s += "Name: " + self.name
return s
@@ -0,0 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class ParserException(Exception):
pass
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013, Roboterclub Aachen e.V.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Roboterclub Aachen e.V. nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ROBOTERCLUB AACHEN E.V. ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ROBOTERCLUB AACHEN E.V. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
import os
from device import PartDescriptionFile
import glob
if __name__ == "__main__":
"""
Some test code
"""
files = glob.glob(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'AVR_devices') + "/ATmega328P*")
for file in files:
dev = PartDescriptionFile(file)
print dev
print dev.properties

0 comments on commit 1532289

Please sign in to comment.