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 High-level support for Param elements.
29 """
30
31
32 import re
33 import sys
34 from xml.sax.saxutils import escape as xmlescape
35 from xml.sax.xmlreader import AttributesImpl as Attributes
36 try:
37 import yaml
38 except ImportError:
39
40 pass
41
42
43 from glue import git_version
44 from . import ligolw
45 from . import types as ligolwtypes
46
47
48 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
49 __version__ = "git id %s" % git_version.id
50 __date__ = git_version.date
51
52
53
54
55
56
57
58
59
60
61
62 -def get_param(xmldoc, name):
63 """
64 Scan xmldoc for a param named name. Raises ValueError if not
65 exactly 1 such param is found.
66 """
67 params = Param.getParamsByName(xmldoc, name)
68 if len(params) != 1:
69 raise ValueError("document must contain exactly one %s param" % Param.ParamName(name))
70 return params[0]
71
74 """
75 Convenience wrapper for get_param() that recovers an instance of a
76 Python builtin type from a Param element.
77 """
78
79
80 return get_param(xml, name).pcdata
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 -class Param(ligolw.Param):
110 """
111 High-level Param element. The value is stored in the pcdata
112 attribute as the native Python type rather than as a string.
113 """
117
118 Name = ligolw.attributeproxy(u"Name", enc = ParamName.enc, dec = ParamName)
119 Scale = ligolw.attributeproxy(u"Scale", enc = ligolwtypes.FormatFunc[u"real_8"], dec = ligolwtypes.ToPyType[u"real_8"])
120 Type = ligolw.attributeproxy(u"Type", default = u"lstring")
121
123 if self.pcdata is not None:
124
125 if self.Type == u"yaml":
126 try:
127 yaml
128 except NameError:
129 raise NotImplementedError("yaml support not installed")
130 self.pcdata = yaml.load(self.pcdata)
131 else:
132 self.pcdata = ligolwtypes.ToPyType[self.Type](self.pcdata.strip())
133
134 - def write(self, fileobj = sys.stdout, indent = u""):
135 fileobj.write(self.start_tag(indent))
136 for c in self.childNodes:
137 if c.tagName not in self.validchildren:
138 raise ligolw.ElementError("invalid child %s for %s" % (c.tagName, self.tagName))
139 c.write(fileobj, indent + ligolw.Indent)
140 if self.pcdata is not None:
141 if self.Type == u"yaml":
142 try:
143 yaml
144 except NameError:
145 raise NotImplementedError("yaml support not installed")
146 fileobj.write(xmlescape(yaml.dump(self.pcdata).strip()))
147 else:
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 fileobj.write(xmlescape(ligolwtypes.FormatFunc[self.Type](self.pcdata).strip(u"\"") or u" "))
166 fileobj.write(self.end_tag(u"") + u"\n")
167
168 @classmethod
169 - def build(cls, name, Type, value, start = None, scale = None, unit = None, dataunit = None, comment = None):
170 """
171 Construct a LIGO Light Weight XML Param document subtree.
172 FIXME: document keyword arguments.
173 """
174 elem = cls()
175 elem.Name = name
176 elem.Type = Type
177 elem.pcdata = value
178
179
180 if dataunit is not None:
181 elem.DataUnit = dataunit
182 if scale is not None:
183 elem.Scale = scale
184 if start is not None:
185 elem.Start = start
186 if unit is not None:
187 elem.Unit = unit
188 if comment is not None:
189 elem.appendChild(ligolw.Comment()).pcdata = comment
190 return elem
191
192 @classmethod
194 """
195 Convenience wrapper for .build() that constructs a Param
196 element from an instance of a Python builtin type. See
197 .build() for a description of the valid keyword arguments.
198 """
199 return cls.build(name, ligolwtypes.FromPyType[type(value)], value, **kwargs)
200
201 @classmethod
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 -def use_in(ContentHandler):
225 """
226 Modify ContentHandler, a sub-class of
227 glue.ligolw.LIGOLWContentHandler, to cause it to use the Param
228 class defined in this module when parsing XML documents.
229
230 Example:
231
232 >>> from glue.ligolw import ligolw
233 >>> def MyContentHandler(ligolw.LIGOLWContentHandler):
234 ... pass
235 ...
236 >>> use_in(MyContentHandler)
237 """
238 def startParam(self, parent, attrs):
239 return Param(attrs)
240
241 ContentHandler.startParam = startParam
242
243 return ContentHandler
244