| 1 |
""" |
| 2 |
Generating a utype/value sequence for ASTs. |
| 3 |
|
| 4 |
Yet another insane serialization for an insane data model. Sigh. |
| 5 |
|
| 6 |
The way we come up with the STC utypes here is described in an IVOA note. |
| 7 |
|
| 8 |
Since the utypes are basically xpaths into STC-X, there is not terribly |
| 9 |
much we need to do here. However, due to STC-X being a nightmare, |
| 10 |
certain rules need to be formulated what utypes to generate. |
| 11 |
|
| 12 |
Here, we use UtypeMakers for this. There's a default UtypeMaker |
| 13 |
that implements the basic algorithm of the STC note (in iterUtypes, |
| 14 |
a method that yields all utype/value pairs for an STC-X node, which |
| 15 |
is a stanxml Element). |
| 16 |
|
| 17 |
To customize what is being generated, define _gener_<child name> |
| 18 |
methods, where <child name> is a key within the dictionaries |
| 19 |
returned by stanxml.Element's getChildDict method. |
| 20 |
|
| 21 |
To make the definition of the _gener_ methods easier, there's |
| 22 |
the handles decorator that you can pass a list of such child names. |
| 23 |
""" |
| 24 |
|
| 25 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 26 |
#c |
| 27 |
#c This program is free software, covered by the GNU GPL. See the |
| 28 |
#c COPYING file in the source distribution. |
| 29 |
|
| 30 |
|
| 31 |
from gavo import utils |
| 32 |
from gavo.stc import common |
| 33 |
from gavo.stc import stcxgen |
| 34 |
from gavo.stc.stcx import STC |
| 35 |
|
| 36 |
|
| 37 |
#################### utype maker definition |
| 38 |
|
| 39 |
def handles(seq): |
| 40 |
"""is a decorator for UtypeMaker methods. |
| 41 |
|
| 42 |
It adds a "handles" attribute as evaluated by AutoUtypeMaker. |
| 43 |
""" |
| 44 |
def deco(meth): |
| 45 |
meth.handles = seq |
| 46 |
return meth |
| 47 |
return deco |
| 48 |
|
| 49 |
|
| 50 |
class UtypeMaker_t(type): |
| 51 |
"""A metaclass to facilite easy definition of UtypeMakers. |
| 52 |
|
| 53 |
This metaclass primarily operates on the handles hints left by the |
| 54 |
decorator. |
| 55 |
""" |
| 56 |
def __init__(cls, name, bases, dict): |
| 57 |
type.__init__(cls, name, bases, dict) |
| 58 |
cls._createHandlesMethods(list(dict.values())) |
| 59 |
|
| 60 |
def _createHandlesMethods(cls, items): |
| 61 |
for item in items: |
| 62 |
for name in getattr(item, "handles", ()): |
| 63 |
setattr(cls, "_gener_"+name, item) |
| 64 |
|
| 65 |
|
| 66 |
class UtypeMaker(object, metaclass=UtypeMaker_t): |
| 67 |
"""An object encapsulating information on how to turn a stanxml |
| 68 |
node into a sequence of utype/value pairs. |
| 69 |
|
| 70 |
This is an "universal" base, serving as a simple default. |
| 71 |
Any class handling specific node types must fill out at least |
| 72 |
the rootType attribute, giving the utype at which this UtypeMaker |
| 73 |
should kick in. |
| 74 |
|
| 75 |
By default, utype/value pairs are only returned for nonempty |
| 76 |
element content. To change this, define _gener_<name>(node, |
| 77 |
prefix) -> iterator methods. |
| 78 |
|
| 79 |
The actual pairs are retrieved by calling iterUtypes(node, |
| 80 |
parentPrefix). |
| 81 |
""" |
| 82 |
|
| 83 |
# attributes that don't get serialized to utypes per spec |
| 84 |
bannedAttributes = set("id frame_id coord_system_id unit" |
| 85 |
" pos_angle_unit pos_unit spectral_unit time_unit" |
| 86 |
" vel_time_unit gen_unit xsi:type ucd xmlns:stc xmlns xmlns:xsi" |
| 87 |
" xsi:schemaLocation".split()) |
| 88 |
|
| 89 |
rootType = None |
| 90 |
|
| 91 |
def _generPlain(self, name, child, prefix): |
| 92 |
childType = utypejoin(prefix, name) |
| 93 |
maker = _getUtypeMaker(childType) |
| 94 |
for item in child: |
| 95 |
for pair in maker.iterUtypes(item, childType): |
| 96 |
yield pair |
| 97 |
|
| 98 |
def _gener__colRef(self, name, child, prefix): |
| 99 |
yield prefix, child[0] |
| 100 |
|
| 101 |
def iterUtypes(self, node, prefix): |
| 102 |
children = node.getChildDict() |
| 103 |
if node.text_: |
| 104 |
yield prefix, node.text_ |
| 105 |
for attName, name in node.iterAttNames(): |
| 106 |
if name not in self.bannedAttributes: |
| 107 |
val = getattr(node, attName) |
| 108 |
if val is not None: |
| 109 |
yield "%s.%s"%(prefix, name.split(":")[-1]), val |
| 110 |
for name, child in children.items(): |
| 111 |
handler = getattr(self, "_gener_"+name, self._generPlain) |
| 112 |
for pair in handler(name, child, prefix): |
| 113 |
yield pair |
| 114 |
|
| 115 |
|
| 116 |
class _NotImplementedUtypeMaker(UtypeMaker): |
| 117 |
def _generPlain(self, name, child, prefix): |
| 118 |
raise common.STCNotImplementedError("Cannot create utypes for %s yet."% |
| 119 |
self.utypeFrag) |
| 120 |
|
| 121 |
|
| 122 |
#################### utype specific makers |
| 123 |
|
| 124 |
|
| 125 |
class _CoordFrameMaker(UtypeMaker): |
| 126 |
@handles(common.stcRefPositions) |
| 127 |
def _refPos(self, name, child, prefix): |
| 128 |
if name!='UNKNOWNRefPos': |
| 129 |
yield utypejoin(prefix, "ReferencePosition"), name |
| 130 |
for pair in self._generPlain("ReferencePosition", child, prefix): |
| 131 |
yield pair |
| 132 |
|
| 133 |
|
| 134 |
class TimeFrameMaker(_CoordFrameMaker): |
| 135 |
rootType = "AstroCoordSystem.TimeFrame" |
| 136 |
@handles(common.stcTimeScales) |
| 137 |
def _timeScale(self, name, child, prefix): |
| 138 |
yield utypejoin(prefix, "TimeScale"), name |
| 139 |
|
| 140 |
|
| 141 |
class SpaceFrameMaker(_CoordFrameMaker): |
| 142 |
rootType = "AstroCoordSystem.SpaceFrame" |
| 143 |
|
| 144 |
@handles(common.stcSpaceRefFrames) |
| 145 |
def _coordFrame(self, name, child, prefix): |
| 146 |
myPrefix = utypejoin(prefix, "CoordRefFrame") |
| 147 |
yield myPrefix, name |
| 148 |
for pair in self._generPlain(None, child, myPrefix): |
| 149 |
yield pair |
| 150 |
|
| 151 |
@handles(common.stcCoordFlavors) |
| 152 |
def _coordFlavor(self, name, child, prefix): |
| 153 |
prefix = utypejoin(prefix, "CoordFlavor") |
| 154 |
yield prefix, name |
| 155 |
if child: |
| 156 |
if child[0].coord_naxes!="2": |
| 157 |
yield utypejoin(prefix, "coord_naxes"), child[0].coord_naxes |
| 158 |
yield utypejoin(prefix, "handedness"), child[0].handedness |
| 159 |
|
| 160 |
|
| 161 |
class RedshiftFrameMaker(_CoordFrameMaker): |
| 162 |
rootType = "AstroCoordSystem.RedshiftFrame" |
| 163 |
|
| 164 |
def iterUtypes(self, node, prefix): |
| 165 |
for pair in _CoordFrameMaker.iterUtypes(self, node, prefix): |
| 166 |
yield pair |
| 167 |
|
| 168 |
|
| 169 |
class SpectralFrameMaker(_CoordFrameMaker): |
| 170 |
rootType = "AstroCoordSystem.SpectralFrame" |
| 171 |
|
| 172 |
|
| 173 |
class _TimeValueMaker(UtypeMaker): |
| 174 |
@handles(["ISOTime", "JDTime", "MJDTime"]) |
| 175 |
def _absoluteTime(self, name, child, prefix): |
| 176 |
yield utypejoin(prefix, "xtype"), name |
| 177 |
for item in child: |
| 178 |
for pair in self.iterUtypes(item, prefix): |
| 179 |
yield pair |
| 180 |
|
| 181 |
|
| 182 |
class TimeInstantMaker(_TimeValueMaker): |
| 183 |
rootType = "AstroCoords.Time.TimeInstant" |
| 184 |
|
| 185 |
class StartTimeMaker(_TimeValueMaker): |
| 186 |
rootType = "AstroCoordArea.TimeInterval.StartTime" |
| 187 |
|
| 188 |
class StopTimeMaker(_TimeValueMaker): |
| 189 |
rootType = "AstroCoordArea.TimeInterval.StopTime" |
| 190 |
|
| 191 |
|
| 192 |
#################### toplevel code |
| 193 |
|
| 194 |
def utypejoin(*utypes): |
| 195 |
return ".".join(u for u in utypes if u) |
| 196 |
|
| 197 |
|
| 198 |
# A resolver of element names to their handling classes. For most |
| 199 |
# elements, this is just a plain UtypeMaker. |
| 200 |
_getUtypeMaker = utils.buildClassResolver( |
| 201 |
UtypeMaker, |
| 202 |
list(globals().values()), |
| 203 |
default=UtypeMaker(), |
| 204 |
instances=True, |
| 205 |
key=lambda obj:obj.rootType) |
| 206 |
|
| 207 |
|
| 208 |
def getUtypes(ast, includeDMURI=False, suppressXtype=True): |
| 209 |
"""returns a lists of utype/value pairs for an STC AST. |
| 210 |
|
| 211 |
If you pass includeDMURI, a utype/value pair for the data model URI will |
| 212 |
be generated in addition to what comes in from ast. |
| 213 |
""" |
| 214 |
cst = stcxgen.astToStan(ast, STC.STCSpec) |
| 215 |
utypes = [] |
| 216 |
for utype, val in _getUtypeMaker("").iterUtypes(cst, ""): |
| 217 |
if val is None or val=='': |
| 218 |
continue |
| 219 |
if suppressXtype: |
| 220 |
if utype.endswith("xtype"): |
| 221 |
continue |
| 222 |
utypes.append(("stc:"+utype, val)) |
| 223 |
if includeDMURI: |
| 224 |
utypes.append(("stc:DataModel.URI", common.STCNamespace)) |
| 225 |
utypes.sort() |
| 226 |
return utypes |