| 1 |
""" |
| 2 |
xmlstan elements of VOTable. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import base64 |
| 12 |
import functools |
| 13 |
|
| 14 |
from gavo import utils |
| 15 |
from gavo.utils import ElementTree |
| 16 |
from gavo.utils.stanxml import ( |
| 17 |
Element, registerPrefix, getPrefixInfo, schemaURL, escapePCDATA) |
| 18 |
from gavo.votable import common |
| 19 |
|
| 20 |
from gavo.utils.dachstypes import ( |
| 21 |
BytesWritable, Dict, List, Optional, Tuple) |
| 22 |
|
| 23 |
|
| 24 |
NAMESPACES = { |
| 25 |
"1.5": "http://www.ivoa.net/xml/VOTable/v1.3", |
| 26 |
"1.4": "http://www.ivoa.net/xml/VOTable/v1.3", |
| 27 |
"1.3": "http://www.ivoa.net/xml/VOTable/v1.3", |
| 28 |
"1.2": "http://www.ivoa.net/xml/VOTable/v1.2", |
| 29 |
"1.1": "http://www.ivoa.net/xml/VOTable/v1.1", |
| 30 |
} |
| 31 |
|
| 32 |
# WARNING: if you change the default version of the VOTables generated, |
| 33 |
# you'll probably have to adapt them in resources/xsl, too. |
| 34 |
registerPrefix("vot", NAMESPACES["1.3"], schemaURL("VOTable.xsd")) |
| 35 |
registerPrefix("vot2", NAMESPACES["1.2"], schemaURL("VOTable-1.2.xsd")) |
| 36 |
registerPrefix("vot1", NAMESPACES["1.1"], schemaURL("VOTable-1.1.xsd")) |
| 37 |
|
| 38 |
registerPrefix("mivot", |
| 39 |
"http://www.ivoa.net/xml/mivot", schemaURL("mivot.xsd")) |
| 40 |
|
| 41 |
|
| 42 |
class _MIVOTElements: |
| 43 |
"""elements from MIVOT, i.e., data model annotation |
| 44 |
""" |
| 45 |
class _MIVOTElement(Element): |
| 46 |
_prefix = "mivot" |
| 47 |
_local = False |
| 48 |
_a_ID = None |
| 49 |
|
| 50 |
class VODML(_MIVOTElement): |
| 51 |
_childSequence = ["REPORT", "MODEL", "GLOBALS", "TEMPLATES"] |
| 52 |
|
| 53 |
class REPORT(_MIVOTElement): |
| 54 |
_mayBeEmpty = True |
| 55 |
_a_status = None |
| 56 |
|
| 57 |
class MODEL(_MIVOTElement): |
| 58 |
_mayBeEmpty = True |
| 59 |
_a_name = None |
| 60 |
_a_url = None |
| 61 |
|
| 62 |
class GLOBALS(_MIVOTElement): |
| 63 |
_childSequence = ["INSTANCE", "COLLECTION"] |
| 64 |
|
| 65 |
class TEMPLATES(_MIVOTElement): |
| 66 |
_a_tableref = None |
| 67 |
_childSequence = ["INSTANCE"] |
| 68 |
|
| 69 |
class COLLECTION(_MIVOTElement): |
| 70 |
_a_dmid = None |
| 71 |
_a_dmrole = None |
| 72 |
_a_dmtype = None |
| 73 |
_childSequence = ["ATTRIBUTE", "REFERENCE", "INSTANCE", "COLLECTION"] |
| 74 |
|
| 75 |
class INSTANCE(_MIVOTElement): |
| 76 |
_a_dmtype = None |
| 77 |
_childSequence = ["REFERENCE", "ATTRIBUTE"] |
| 78 |
|
| 79 |
class ATTRIBUTE(_MIVOTElement): |
| 80 |
_mayBeEmpty = True |
| 81 |
_a_dmrole = None |
| 82 |
_a_ref = None |
| 83 |
_a_dmtype = None |
| 84 |
_a_value = None |
| 85 |
_a_type = None |
| 86 |
_a_arrayindex = None |
| 87 |
_childSequence = [ |
| 88 |
"COLLECTION", "INSTANCE", "REFERENCE"] |
| 89 |
|
| 90 |
class REFERENCE(_MIVOTElement): |
| 91 |
_mayBeEmpty = True |
| 92 |
_a_dmrole = None |
| 93 |
_a_sourceref = None |
| 94 |
_a_dmref = None |
| 95 |
|
| 96 |
|
| 97 |
class VOTable(_MIVOTElements): |
| 98 |
"""The container for VOTable elements. |
| 99 |
""" |
| 100 |
class _VOTElement(Element): |
| 101 |
_prefix = "vot" |
| 102 |
_local = True |
| 103 |
|
| 104 |
class DescribedElement(_VOTElement): |
| 105 |
_a_ID = None |
| 106 |
_a_ref = None |
| 107 |
_a_name = None |
| 108 |
_a_ucd = None |
| 109 |
_a_utype = None |
| 110 |
_mayBeEmpty = True |
| 111 |
|
| 112 |
def getDesignation(self) -> str: |
| 113 |
"""returns some name-like thing for a FIELD or PARAM. |
| 114 |
""" |
| 115 |
if self.name: |
| 116 |
res = self.name |
| 117 |
elif self.ID: |
| 118 |
res = self.ID |
| 119 |
else: |
| 120 |
res = "%s_%s"%(self.__class__.__name__, "%x"%id(self)) |
| 121 |
return res |
| 122 |
|
| 123 |
def getDescription(self) -> str: |
| 124 |
"""returns the description for this element, or an empty string. |
| 125 |
""" |
| 126 |
try: |
| 127 |
return next(self.iterChildrenOfType(VOTable.DESCRIPTION)).text_ |
| 128 |
except StopIteration: |
| 129 |
return "" |
| 130 |
|
| 131 |
class _ValuedElement(DescribedElement): |
| 132 |
_a_unit = None |
| 133 |
_a_xtype = None |
| 134 |
|
| 135 |
class TypedElement(_ValuedElement): |
| 136 |
_a_ref = None |
| 137 |
_a_arraysize = None |
| 138 |
_a_datatype = None |
| 139 |
_a_precision = None |
| 140 |
_a_ref = None |
| 141 |
_a_type = None |
| 142 |
_a_width = None |
| 143 |
_a_format = None |
| 144 |
|
| 145 |
def isScalar(self) -> bool: |
| 146 |
return self.arraysize is None or self.arraysize=='1' |
| 147 |
|
| 148 |
def isMultiDim(self) -> bool: |
| 149 |
return common.isMultiDim(self.arraysize) |
| 150 |
|
| 151 |
def hasVarLength(self) -> bool: |
| 152 |
return common.hasVarLength(self.arraysize) |
| 153 |
|
| 154 |
def getLength(self) -> Optional[int]: |
| 155 |
"""returns the number of items one should expect in value, or |
| 156 |
None for variable-length arrays. |
| 157 |
""" |
| 158 |
return common.getLength(self.arraysize) |
| 159 |
|
| 160 |
def getShape(self) -> Tuple[int, ...] | None: |
| 161 |
return common.getShape(self.datatype, self.arraysize) |
| 162 |
|
| 163 |
def _setNULLValue(self, val: str) -> None: |
| 164 |
"""sets the null literal of self to val. |
| 165 |
""" |
| 166 |
valEls = list(self.iterChildrenWithName("VALUES")) |
| 167 |
if valEls: |
| 168 |
valEls[0](null=val) |
| 169 |
else: |
| 170 |
self[VOTable.VALUES(null=val)] |
| 171 |
|
| 172 |
class _RefElement(_ValuedElement): |
| 173 |
_a_ref = None |
| 174 |
_a_ucd = None |
| 175 |
_a_utype = None |
| 176 |
childSequence: List[str] = [] |
| 177 |
|
| 178 |
class _ContentElement(_VOTElement): |
| 179 |
"""An element containing tabular data. |
| 180 |
|
| 181 |
These are usually serialized using some kind of streaming. |
| 182 |
|
| 183 |
See votable.tablewriter for details. |
| 184 |
""" |
| 185 |
def write(self, file: BytesWritable) -> None: |
| 186 |
raise NotImplementedError("This _ContentElement cannot write yet") |
| 187 |
|
| 188 |
|
| 189 |
class _BinaryDataElement(_ContentElement): |
| 190 |
"""a base class for both BINARY and BINARY2. |
| 191 |
""" |
| 192 |
_childSequence = ["STREAM"] |
| 193 |
encoding = "base64" |
| 194 |
|
| 195 |
def write(self, file: BytesWritable) -> None: |
| 196 |
# To be able to write incrementally, encode chunks of multiples |
| 197 |
# of base64's block size until the stream is finished. |
| 198 |
blockSize = 57 |
| 199 |
buf, bufFil, flushThreshold = [], 0, blockSize*20 |
| 200 |
file.write( |
| 201 |
utils.bytify( |
| 202 |
'<%s>'%self.name_+ |
| 203 |
'<STREAM encoding="base64">')) |
| 204 |
try: |
| 205 |
for data in self.iterSerialized(): # type: ignore # def'd in tablewriter |
| 206 |
buf.append(data) |
| 207 |
bufFil += len(data) |
| 208 |
if bufFil>flushThreshold: |
| 209 |
curData = b''.join(buf) |
| 210 |
curBlockLen = (len(curData)//blockSize)*blockSize |
| 211 |
file.write(base64.b64encode(curData[:curBlockLen])) |
| 212 |
buf = [curData[curBlockLen:]] |
| 213 |
finally: |
| 214 |
file.write(base64.b64encode(b"".join(buf))) |
| 215 |
file.write(utils.bytify("</STREAM></%s>"%self.name_)) |
| 216 |
|
| 217 |
class BINARY(_BinaryDataElement): |
| 218 |
pass |
| 219 |
|
| 220 |
class BINARY2(_BinaryDataElement): |
| 221 |
pass |
| 222 |
|
| 223 |
class COOSYS(_VOTElement): |
| 224 |
_mayBeEmpty = True |
| 225 |
_a_ID = None |
| 226 |
_a_epoch = None |
| 227 |
_a_equinox = None |
| 228 |
_a_system = None |
| 229 |
_a_refposition = None |
| 230 |
|
| 231 |
class TIMESYS(_VOTElement): |
| 232 |
_mayBeEmpty = True |
| 233 |
_a_ID = None |
| 234 |
_a_timescale = "UNKNOWN" |
| 235 |
_a_refposition = "UNKNOWN" |
| 236 |
_a_timeorigin = None |
| 237 |
|
| 238 |
class TYPE(_VOTElement): |
| 239 |
pass |
| 240 |
|
| 241 |
class ROLE(_VOTElement): |
| 242 |
pass |
| 243 |
|
| 244 |
class DATA(_VOTElement): |
| 245 |
_childSequence = ["INFO", "TABLEDATA", "BINARY", "BINARY2", "FITS"] |
| 246 |
|
| 247 |
class DEFINITIONS(_VOTElement): |
| 248 |
pass |
| 249 |
|
| 250 |
class DESCRIPTION(_VOTElement): |
| 251 |
_childSequence = [None] |
| 252 |
|
| 253 |
class FIELD(TypedElement): |
| 254 |
_childSequence = ["DESCRIPTION", "VALUES", "LINK"] |
| 255 |
|
| 256 |
class FIELDref(_RefElement): pass |
| 257 |
|
| 258 |
class FITS(_VOTElement): |
| 259 |
_childSequence = ["STREAM"] |
| 260 |
|
| 261 |
class GROUP(DescribedElement): |
| 262 |
_mayBeEmpty = True |
| 263 |
_a_ref = None |
| 264 |
_childSequence = ["DESCRIPTION", "PARAM", "FIELDref", |
| 265 |
"PARAMref", "GROUP"] |
| 266 |
|
| 267 |
class INFO(_ValuedElement): |
| 268 |
_a_ref = None |
| 269 |
_a_value = None |
| 270 |
_childSequence = [None] |
| 271 |
|
| 272 |
def isEmpty(self) -> bool: |
| 273 |
return self.value is None |
| 274 |
|
| 275 |
class INFO_atend(INFO): |
| 276 |
# a bad hack; TAP mandates INFO items below table, and this is |
| 277 |
# the least complicated way to force this. |
| 278 |
name_ = "INFO" |
| 279 |
|
| 280 |
class LINK(_VOTElement): |
| 281 |
_a_ID = None |
| 282 |
_a_action = None |
| 283 |
_a_content_role = None |
| 284 |
_name_a_content_role = "content-role" |
| 285 |
_a_content_type = None |
| 286 |
_name_a_content_type = "content-type" |
| 287 |
_a_gref = None |
| 288 |
_a_href = None |
| 289 |
_a_name = None |
| 290 |
_a_value = None |
| 291 |
_a_title = None |
| 292 |
_childSequence: List[str] = [] |
| 293 |
_mayBeEmpty = True |
| 294 |
|
| 295 |
class MAX(_VOTElement): |
| 296 |
_a_inclusive = None |
| 297 |
_a_value = None |
| 298 |
_childSequence: List[str] = [] |
| 299 |
|
| 300 |
def isEmpty(self) -> bool: |
| 301 |
return self.value is None |
| 302 |
|
| 303 |
class MIN(_VOTElement): |
| 304 |
_a_inclusive = None |
| 305 |
_a_value = None |
| 306 |
_childSequence: List[str] = [] |
| 307 |
|
| 308 |
def isEmpty(self) -> bool: |
| 309 |
return self.value is None |
| 310 |
|
| 311 |
class OPTION(_VOTElement): |
| 312 |
_a_name = None |
| 313 |
_a_value = "" # as with PARAM below |
| 314 |
_childSequence = ["OPTION"] |
| 315 |
_mayBeEmpty = True |
| 316 |
|
| 317 |
class PARAM(TypedElement): |
| 318 |
_mayBeEmpty = True |
| 319 |
_a_value = "" # supposed to mean "ah, somewhat null" |
| 320 |
# Needs to be cared for in client code. |
| 321 |
_childSequence = ["DESCRIPTION", "VALUES", "LINK"] |
| 322 |
|
| 323 |
class PARAMref(_RefElement): pass |
| 324 |
|
| 325 |
class RESOURCE(_VOTElement): |
| 326 |
_a_ID = None |
| 327 |
_a_name = None |
| 328 |
_a_type = None |
| 329 |
_a_utype = None |
| 330 |
_childSequence = ["DESCRIPTION", "VODML", "DEFINITIONS", |
| 331 |
"INFO", "COOSYS", "TIMESYS", "GROUP", |
| 332 |
"PARAM", "LINK", "TABLE", "INFO_atend", "RESOURCE", "stub"] |
| 333 |
# (stub for delayed overflow warnings and such) |
| 334 |
|
| 335 |
def writeErrorElement(self, |
| 336 |
outputFile: BytesWritable, |
| 337 |
exception: Exception): |
| 338 |
outputFile.write( |
| 339 |
utils.bytify( |
| 340 |
"""<INFO name="QUERY_STATUS" value="ERROR">%s</INFO>"""% |
| 341 |
escapePCDATA("Error while serializing VOTable," |
| 342 |
" content is probably incomplete: %s"% |
| 343 |
utils.safe_str(exception)))) |
| 344 |
|
| 345 |
class STREAM(_VOTElement): |
| 346 |
_a_actuate = None |
| 347 |
_a_encoding = None |
| 348 |
_a_expires = None |
| 349 |
_a_href = None |
| 350 |
_a_rights = None |
| 351 |
_a_type = None |
| 352 |
_childSequence = [None] |
| 353 |
|
| 354 |
class TABLE(DescribedElement): |
| 355 |
"""A TABLE element. |
| 356 |
|
| 357 |
If you want to access fields by name (getFieldForName), make sure |
| 358 |
name and ids are unique. |
| 359 |
""" |
| 360 |
_a_nrows = None |
| 361 |
_childSequence = ["DESCRIPTION", "INFO", "GROUP", "FIELD", "PARAM", "LINK", |
| 362 |
"DATA", "stub"] # (stub for delayed overflow warnings and such) |
| 363 |
|
| 364 |
_fieldIndex = None |
| 365 |
|
| 366 |
@functools.lru_cache(1) |
| 367 |
def getFields(self) -> List["VOTable.FIELD"]: |
| 368 |
return list(self.iterChildrenOfType(VOTable.FIELD)) |
| 369 |
|
| 370 |
def _getFieldIndex(self) -> Dict[str, "VOTable.FIELD"]: |
| 371 |
if self._fieldIndex is None: |
| 372 |
index = {} |
| 373 |
for child in self.getFields(): |
| 374 |
if child.name: |
| 375 |
index[child.name] = child |
| 376 |
if child.ID: |
| 377 |
index[child.ID] = child |
| 378 |
self._fieldIndex = index |
| 379 |
return self._fieldIndex |
| 380 |
|
| 381 |
def getFieldForName(self, name: str) -> "VOTable.FIELD": |
| 382 |
"""returns the FIELD having a name or id of name. |
| 383 |
|
| 384 |
A KeyError is raised when the field does not exist; if names are |
| 385 |
not unique, the last column with the name specified is returned. |
| 386 |
""" |
| 387 |
return self._getFieldIndex()["name"] |
| 388 |
|
| 389 |
class TABLEDATA(_ContentElement): |
| 390 |
_childSequence = ["TR"] |
| 391 |
|
| 392 |
def write(self, file: BytesWritable) -> None: |
| 393 |
file.write(b"<TABLEDATA>") |
| 394 |
try: |
| 395 |
for row in self.iterSerialized(): # type: ignore # def'd in tablewriter |
| 396 |
file.write(row.encode("utf-8")) |
| 397 |
finally: |
| 398 |
file.write(b"</TABLEDATA>") |
| 399 |
|
| 400 |
class TD(_VOTElement): |
| 401 |
_a_encoding = None |
| 402 |
_childSequence = [None] |
| 403 |
_mayBeEmpty = True |
| 404 |
|
| 405 |
class TR(_VOTElement): |
| 406 |
_a_ID = None |
| 407 |
_childSequence = ["TD"] |
| 408 |
|
| 409 |
class VALUES(_VOTElement): |
| 410 |
_a_ID = None |
| 411 |
_a_null = None |
| 412 |
_a_ref = None |
| 413 |
_a_type = None |
| 414 |
|
| 415 |
def isEmpty(self) -> bool: |
| 416 |
return self.null is None and Element.isEmpty(self) |
| 417 |
|
| 418 |
class VOTABLE(_VOTElement): |
| 419 |
_a_ID = None |
| 420 |
_a_version = "1.5" |
| 421 |
_prefix = "vot" |
| 422 |
_supressedPrefix = "vot" |
| 423 |
_mayBeEmpty = True |
| 424 |
# The following is for when the xmlstan tree is processed by |
| 425 |
# tablewriter.write rather than asETree |
| 426 |
_fixedTagMaterial = ('xmlns="%s" xmlns:xsi="%s"' |
| 427 |
' xsi:schemaLocation="%s %s"')%(( |
| 428 |
getPrefixInfo("vot")[0], |
| 429 |
getPrefixInfo("xsi")[0]) |
| 430 |
+getPrefixInfo("vot")) |
| 431 |
_childSequence = ["DESCRIPTION", "VODML", "DEFINITIONS", "INFO", "COOSYS", |
| 432 |
"TIMESYS", "GROUP", "PARAM", "RESOURCE"] |
| 433 |
|
| 434 |
class VOTABLE11(VOTABLE): |
| 435 |
# An incredibly nasty hack that kinda works due to the fact that |
| 436 |
# all elements here are local -- make this your top-level element |
| 437 |
# and only use what's legal in VOTable 1.1, and you get a VOTable1.1 |
| 438 |
# conforming document |
| 439 |
name_ = "VOTABLE" |
| 440 |
_a_version = "1.1" |
| 441 |
_prefix = "vot1" |
| 442 |
_supressedPrefix = "vot1" |
| 443 |
# The following is for when the xmlstan tree is processed by |
| 444 |
# tablewriter.write rather than asETree |
| 445 |
_fixedTagMaterial = ('xmlns="%s" xmlns:xsi="%s"' |
| 446 |
' xsi:schemaLocation="%s %s"')%(( |
| 447 |
getPrefixInfo("vot1")[0], |
| 448 |
getPrefixInfo("xsi")[0]) |
| 449 |
+getPrefixInfo("vot1")) |
| 450 |
|
| 451 |
class VOTABLE12(VOTABLE): |
| 452 |
# see VOTABLE11 |
| 453 |
name_ = "VOTABLE" |
| 454 |
_a_version = "1.2" |
| 455 |
_prefix = "vot2" |
| 456 |
_supressedPrefix = "vot2" |
| 457 |
# The following is for when the xmlstan tree is processed by |
| 458 |
# tablewriter.write rather than asETree |
| 459 |
_fixedTagMaterial = ('xmlns="%s" xmlns:xsi="%s"' |
| 460 |
' xsi:schemaLocation="%s %s"')%(( |
| 461 |
getPrefixInfo("vot1")[0], |
| 462 |
getPrefixInfo("xsi")[0]) |
| 463 |
+getPrefixInfo("vot2")) |
| 464 |
|
| 465 |
|
| 466 |
def voTag(tagName: str, version: str="1.4"): |
| 467 |
"""returns the VOTable QName for tagName. |
| 468 |
|
| 469 |
You only need this if you want to search in ElementTrees. |
| 470 |
""" |
| 471 |
return ElementTree.QName(NAMESPACES[version], tagName) |