summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Albert <thejta@users.noreply.github.com>2016-08-31 09:30:15 -0500
committerGitHub <noreply@github.com>2016-08-31 09:30:15 -0500
commit4bb1f7a69cc287e6efa08aefb4b237fc0f9a73bb (patch)
tree45a6b63a5c95ef472d041c430a7e10efad736be7
parent11e39ffebe742a7a14f12410de56c5742e3b8c22 (diff)
parent3ac0d793e884906b64de2288f4ba31e017325b1e (diff)
downloadvpdtools-4bb1f7a69cc287e6efa08aefb4b237fc0f9a73bb.tar.gz
vpdtools-4bb1f7a69cc287e6efa08aefb4b237fc0f9a73bb.zip
Merge pull request #5 from open-power/comment-support
Comment support
-rwxr-xr-xcreateVpd.py101
-rw-r--r--examples/comments/comments.tvpd32
2 files changed, 101 insertions, 32 deletions
diff --git a/createVpd.py b/createVpd.py
index 7178031..af46e6c 100755
--- a/createVpd.py
+++ b/createVpd.py
@@ -44,6 +44,20 @@ import os
############################################################
# Classes - Classes - Classes - Classes - Classes - Classes
############################################################
+# This parser extension is necessary to save comments and write them back out in the final file
+# By default, element tree doesn't preserve comments
+# http://stackoverflow.com/questions/4474754/how-to-keep-comments-while-parsing-xml-using-python-elementtree
+class PCParser(ET.XMLTreeBuilder):
+ def __init__(self):
+ ET.XMLTreeBuilder.__init__(self)
+ # assumes ElementTree 1.2.X
+ self._parser.CommentHandler = self.handle_comment
+
+ def handle_comment(self, data):
+ self._target.start(ET.Comment, {})
+ self._target.data(data)
+ self._target.end(ET.Comment)
+
class RecordInfo:
"""Stores the info about each vpd record"""
def __init__(self):
@@ -100,7 +114,9 @@ def parseTvpd(tvpdFile, topLevel):
# Read in the file
# If there are tag mismatch errors or other general gross format problems, it will get caught here
# Once we return from this function, then we'll check to make sure only supported tags were given, etc..
- tvpdRoot = ET.parse(tvpdFile).getroot()
+ # Invoke the extended PCParser, which will handle preserving comments in the output file
+ parser = PCParser()
+ tvpdRoot = ET.parse(tvpdFile, parser=parser).getroot()
# Print the top level tags from the parsing
if (clDebug):
@@ -124,6 +140,10 @@ def parseTvpd(tvpdFile, topLevel):
# Go thru the tags at this level
for vpd in tvpdRoot:
+ # Comments aren't basestring tags
+ if not isinstance(vpd.tag, basestring):
+ continue
+
# See if this is a tag we even expect
if vpd.tag not in vpdTags:
out.error("Unsupported tag <%s> found while parsing the <vpd> level" % vpd.tag)
@@ -148,12 +168,17 @@ def parseTvpd(tvpdFile, topLevel):
# Loop thru the tags defined for this record
for record in vpd:
+ # Comments aren't basestring tags
+ if not isinstance(record.tag, basestring):
+ continue
+
# See if this is a tag we even expect
if record.tag not in recordTags:
out.error("Unsupported tag <%s> found while parsing the <record> level for record %s" % (record.tag, recordName))
errorsFound+=1
# We continue here because we don't want to parse down this hierarcy path when we don't know what it is
continue
+
# It was a supported tag
else:
recordTags[record.tag]+=1
@@ -172,6 +197,10 @@ def parseTvpd(tvpdFile, topLevel):
# Loop thru the tags defined for this keyword
for keyword in record:
+ # Comments aren't basestring tags
+ if not isinstance(keyword.tag, basestring):
+ continue
+
# See if this is a tag we even expect
if keyword.tag not in keywordTags:
out.error("Unsupported tag <%s> found while parsing the <keyword> level for keyword %s in record %s" % (keyword.tag, keywordName, recordName))
@@ -180,7 +209,7 @@ def parseTvpd(tvpdFile, topLevel):
continue
# It was a supported tag
else:
- keywordTags[keyword.tag] +=1
+ keywordTags[keyword.tag] += 1
# We've checked for unknown keyword tags, now make sure we have the right number of each
# This is a simple one, we can only have 1 of each
@@ -589,16 +618,20 @@ for record in manifest.iter("record"):
# --------
# We'll loop through all the tags found in this keyword and check for all required and any extra ones
for kw in keyword.iter():
+ # Comments aren't basestring tags
+ if not isinstance(kw.tag, basestring):
+ continue
+
if kw.tag in kwTags:
# Mark that we found a required tag
kwTags[kw.tag] = True
# Save the values we'll need into variables for ease of use
if (kw.tag == "kwformat"):
kwformat = kw.text.lower() # lower() for ease of compare
-
+
if (kw.tag == "kwlen"):
kwlen = int(kw.text)
-
+
if (kw.tag == "kwdata"):
# If it's mixed format, we want kwdata to actually hold all the xml tags contained in this kwdata
# Otherwise, grab the plain text so we can treat it like data later
@@ -673,35 +706,39 @@ for record in manifest.iter("record"):
# --------
# If the input format is mixed, loop over the kwdata and verify it is formatted properly
if (kwformat == "mixed"):
- # We can't use the length check code below for the mixed case, so track it here and check below
- kwdatalen = 0
- # We need to verify the format and length of the ascii or hex keywords embedded in here
- for kwd in kwdata.iter():
- # Make sure it only contains the two keywords we expect
- if kwd.tag.lower() in kwdTags:
- if (kwd.tag.lower() == "ascii"):
- kwdatalen += len(kwd.text)
-
- if (kwd.tag.lower() == "hex"):
- (rc, kwdata) = checkHexDataFormat(kwd.text)
- if (rc):
- out.error("checkHexDataFormat return an error for for keyword %s in record %s" % (keywordName, recordName))
- errorsFound+=1
- # Nibbles to bytes
- kwdatalen += (len(kwdata)/2)
-
- elif (kwd.tag.lower() == "kwdata"):
- next # Ignore this tag at this level
+ # We can't use the length check code below for the mixed case, so track it here and check below
+ kwdatalen = 0
+ # We need to verify the format and length of the ascii or hex keywords embedded in here
+ for kwd in kwdata.iter():
+ # Comments aren't basestring tags
+ if not isinstance(kwd.tag, basestring):
+ continue
+
+ # Make sure it only contains the two keywords we expect
+ if kwd.tag.lower() in kwdTags:
+ if (kwd.tag.lower() == "ascii"):
+ kwdatalen += len(kwd.text)
- else:
- # Flag that we found an unsupported tag. This may help catch typos, etc..
- out.error("The unsupported tag \"<%s>\" was found in kwdata for keyword %s in record %s" % (kwd.tag, keywordName, recordName))
- errorsFound+=1
-
- # Done looping through the tags we found, now check that the length isn't too long
- if (kwdatalen > kwlen):
- out.error("The total length of the mixed data is longer than the given <kwlen> for keyword %s in record %s" % (keywordName, recordName))
- errorsFound+=1
+ if (kwd.tag.lower() == "hex"):
+ (rc, kwdata) = checkHexDataFormat(kwd.text)
+ if (rc):
+ out.error("checkHexDataFormat return an error for for keyword %s in record %s" % (keywordName, recordName))
+ errorsFound+=1
+ # Nibbles to bytes
+ kwdatalen += (len(kwdata)/2)
+
+ elif (kwd.tag.lower() == "kwdata"):
+ next # Ignore this tag at this level
+
+ else:
+ # Flag that we found an unsupported tag. This may help catch typos, etc..
+ out.error("The unsupported tag \"<%s>\" was found in kwdata for keyword %s in record %s" % (kwd.tag, keywordName, recordName))
+ errorsFound+=1
+
+ # Done looping through the tags we found, now check that the length isn't too long
+ if (kwdatalen > kwlen):
+ out.error("The total length of the mixed data is longer than the given <kwlen> for keyword %s in record %s" % (keywordName, recordName))
+ errorsFound+=1
# --------
# Verify that the data isn't longer than the length given
diff --git a/examples/comments/comments.tvpd b/examples/comments/comments.tvpd
new file mode 100644
index 0000000..386911d
--- /dev/null
+++ b/examples/comments/comments.tvpd
@@ -0,0 +1,32 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!-- top level comment -->
+<vpd>
+ <!-- vpd level comment -->
+ <name>FILENAME</name>
+ <size>16kb</size>
+ <VD>01</VD>
+ <record name="VINI">
+ <!-- record level comment -->
+ <rdesc>The VINI record</rdesc>
+ <keyword name="RT">
+ <!-- keyword level comment -->
+ <kwdesc>The Record Type keyword</kwdesc>
+ <kwformat>ascii</kwformat>
+ <kwlen>4</kwlen>
+ <kwdata>VINI</kwdata>
+ </keyword>
+ <keyword name="MX">
+ <kwdesc>The Mixed Data keyword</kwdesc>
+ <kwformat>mixed</kwformat>
+ <kwlen>128</kwlen>
+ <kwdata>
+ <!-- kwdata level comment -->
+ <hex>
+ <!-- mixed level comment -->
+ FEEDB0B0DEADBEEF
+ </hex>
+ <ascii>STRING</ascii>
+ </kwdata>
+ </keyword>
+ </record>
+</vpd>
OpenPOWER on IntegriCloud