Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/python3 

2# 

3# Copyright (C) Citrix Systems Inc. 

4# 

5# This program is free software; you can redistribute it and/or modify 

6# it under the terms of the GNU Lesser General Public License as published 

7# by the Free Software Foundation; version 2.1 only. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU Lesser General Public License for more details. 

13# 

14# You should have received a copy of the GNU Lesser General Public License 

15# along with this program; if not, write to the Free Software Foundation, Inc., 

16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

17# 

18# Metadata VDI format 

19# 

20 

21from xml.dom import minidom, Node 

22import struct 

23import sys 

24import string 

25import util 

26 

27HDR_STRING = "XSSM" 

28STRUCT_FMT = "%dsIHH" % len(HDR_STRING) 

29STRUCT_SIZE = struct.calcsize(STRUCT_FMT) 

30MD_MAJOR = 1 

31MD_MINOR = 2 

32XML_TAG = "SRMetadata" 

33 

34 

35def buildHeader(len): 

36 output = struct.pack(STRUCT_FMT, \ 

37 HDR_STRING, \ 

38 len, \ 

39 MD_MAJOR, \ 

40 MD_MINOR) 

41 return output 

42 

43 

44def unpackHeader(input): 

45 return struct.unpack(STRUCT_FMT, input) 

46 

47 

48def _testHdr(hdr): 

49 assert(hdr[0] == HDR_STRING) 

50 assert(hdr[2] <= MD_MAJOR) 

51 assert(hdr[3] <= MD_MINOR) 

52 

53 

54def unpackBody(input, len): 

55 return struct.unpack("%ds" % len, input) 

56 

57 

58def _generateXMLloop(Dict, e, dom): 

59 for key in Dict.keys(): 

60 entry = dom.createElement(key) 

61 e.appendChild(entry) 

62 if not isinstance(Dict[key], dict): 

63 textnode = dom.createTextNode(str(Dict[key])) 

64 entry.appendChild(textnode) 

65 else: 

66 _generateXMLloop(Dict[key], entry, dom) 

67 

68 

69def _generateXML(Dict): 

70 dom = minidom.Document() 

71 md = dom.createElement(XML_TAG) 

72 dom.appendChild(md) 

73 

74 _generateXMLloop(Dict, md, dom) 

75 return dom.toprettyxml() 

76 

77 

78def _walkXML(parent): 

79 Dict = {} 

80 

81 if not parent.hasChildNodes(): 81 ↛ 82line 81 didn't jump to line 82, because the condition on line 81 was never true

82 if parent.nodeValue is None: 

83 return '' 

84 

85 for node in parent.childNodes: 

86 if node.nodeType == Node.ELEMENT_NODE: 

87 # Print the element name 

88 Dict[util.to_plain_string(node.nodeName)] = "" 

89 

90 # Walk over any text nodes in the current node 

91 content = [] 

92 for child in node.childNodes: 

93 if child.nodeType == Node.TEXT_NODE and \ 

94 child.nodeValue.strip(): 

95 content.append(child.nodeValue.strip()) 

96 if content: 

97 strContent = ''.join(content) 

98 Dict[util.to_plain_string(node.nodeName)] = util.to_plain_string(strContent) 

99 else: 

100 # Walk the child nodes 

101 Dict[util.to_plain_string(node.nodeName)] = _walkXML(node) 

102 

103 return Dict 

104 

105 

106def _parseXML(str): 

107 dom = minidom.parseString(str) 

108 objectlist = dom.getElementsByTagName(XML_TAG)[0] 

109 Dict = _walkXML(objectlist) 

110 return Dict 

111 

112 

113def buildOutput(Dict): 

114 xmlstr = _generateXML(Dict) 

115 XML = struct.pack("%ds" % len(xmlstr), xmlstr) 

116 HDR = buildHeader(len(XML) + STRUCT_SIZE) 

117 return HDR + XML 

118 

119 

120def retrieveXMLfromFile(path): 

121 try: 

122 f = open(path, "rb") 

123 s = f.read(STRUCT_SIZE) 

124 assert(len(s) == STRUCT_SIZE) 

125 hdr = unpackHeader(s) 

126 _testHdr(hdr) 

127 xmllen = hdr[1] - STRUCT_SIZE 

128 s = f.read(xmllen) 

129 assert(len(s) == xmllen) 

130 XML = unpackBody(s, xmllen) 

131 return XML[0] 

132 

133 finally: 

134 f.close() 

135 

136 

137def writeXMLtoFile(path, dict): 

138 f = open(path, "wb") 

139 f.write(buildOutput(dict)) 

140 f.close() 

141 

142 

143def main(): 

144 path = sys.argv[1] 

145 xml = retrieveXMLfromFile(path) 

146 print(xml) 

147 print(_parseXML(xml)) 

148 

149if __name__ == '__main__': 149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true

150 main()