forked from openpreserve/jpylyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathetpatch.py
More file actions
123 lines (97 loc) · 3.63 KB
/
Copy pathetpatch.py
File metadata and controls
123 lines (97 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import xml.etree.ElementTree as ET
from byteconv import bytesToText
# TODO:
# 1) Find out whether these patches are necessary
# 2) learn how to write and test patches properly
# I don't want to mess with ANYthing :)
def tostring(elem, enc, meth):
return ET.tostring(elem, enc, meth)
def fromstring(text):
return ET.fromstring(text)
def SubElement(parent, tag):
return ET.SubElement(parent, tag)
class Element(ET.Element):
# Replacement for ET's 'findtext' function, which has a bug
# that will return empty string if text field contains integer with
# value of zero (0); If there is no match, return None
def findElementText(self, match):
elt = self.find(match)
if elt is not None:
return(elt.text)
else:
return(None)
def findAllText(self, match):
# Searches element and returns list that contains 'Text' attribute
# of all matching sub-elements. Returns empty list if element
# does not exist
try:
return [result.text for result in self.findall(match)]
except:
return []
def appendChildTagWithText(self, tag, text):
# Append childnode with text
el = ET.SubElement(self, tag)
el.text = text
def appendIfNotEmpty(self,subelement):
# Append sub-element, but only if subelement is not empty
if len(subelement) != 0:
self.append(subelement)
def makeHumanReadable(self, remapTable = {}):
# Takes element object, and returns a modified version in which all
# non-printable 'text' fields (which may contain numeric data or binary strings)
# are replaced by printable strings
#
# Property values in original tree may be mapped to alternative (more user-friendly)
# reportable values using a remapTable, which is a nested dictionary.
for elt in self.iter():
# Text field of this element
textIn = elt.text
# Tag name
tag = elt.tag
# Step 1: replace property values by values defined in enumerationsMap,
# if applicable
try:
# If tag is in enumerationsMap, replace property values
parameterMap = remapTable[tag]
try:
# Map original property values to values in dictionary
remappedValue = parameterMap[textIn]
except KeyError:
# If value doesn't match any key: use original value instead
remappedValue = textIn
except KeyError:
# If tag doesn't match any key in enumerationsMap, use original value
remappedValue = textIn
# Step 2: convert all values to text strings
if remappedValue != None:
# Data type
textType = type(remappedValue)
# Convert text field, depending on type
if textType == bytes:
textOut = bytesToText(remappedValue)
elif textType in[int,float,bool]:
textOut=str(remappedValue)
else:
textOut=remappedValue
# Update output tree
elt.text = textOut
def toxml(self, indent = " "):
return(ET.tostring(self, 'UTF-8','xml'))
# Disabled pretty-printing for now as minidom appears to choke on
# entity references, i.e. code below will go wrong:
#
# return minidom.parseString(selfAsString).toprettyxml(indent)