| 1 |
""" |
| 2 |
Legacy data model related code. |
| 3 |
|
| 4 |
This is legacy stuff. Cool folks would use the stuff from gavo.dm. When |
| 5 |
there's actually something useful to be done with it. |
| 6 |
|
| 7 |
This is intended for STC groups and possibly similarly handled data models. |
| 8 |
|
| 9 |
Basically, these come in groups with certain utypes; these in turn contain |
| 10 |
FIELDRefs and INFOs specifying utype-value pairs. |
| 11 |
|
| 12 |
The idea here is to have [un]marshal_DMNAME functions in here that expect |
| 13 |
some defined top-level element. In the case of STC, that top-level |
| 14 |
element is the table, but other elements are conceivable. |
| 15 |
|
| 16 |
To keep the library working even without the STC package, the stc import is |
| 17 |
protected by a try...except clause. You can check the availability of |
| 18 |
STC by inspecting modelgroups.stcAvailable. |
| 19 |
""" |
| 20 |
|
| 21 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 22 |
#c |
| 23 |
#c This program is free software, covered by the GNU GPL. See the |
| 24 |
#c COPYING file in the source distribution. |
| 25 |
|
| 26 |
|
| 27 |
from gavo.votable.model import VOTable as V |
| 28 |
from gavo.votable.model import VOTable |
| 29 |
from typing import List |
| 30 |
|
| 31 |
from gavo.utils.dachstypes import Callable, Dict, Iterator, Tuple, Type, Union |
| 32 |
|
| 33 |
|
| 34 |
try: |
| 35 |
from gavo import stc |
| 36 |
stcAvailable = True |
| 37 |
except ImportError: |
| 38 |
stcAvailable = False |
| 39 |
|
| 40 |
|
| 41 |
######################## Helpers |
| 42 |
|
| 43 |
def _getUtypedGroupsFromAny( |
| 44 |
votObj: Union[V.TABLE, V.RESOURCE, V.VOTABLE], utype: str |
| 45 |
) -> List[V.GROUP]: |
| 46 |
return [g |
| 47 |
for g in votObj.iterChildrenOfType(V.GROUP) |
| 48 |
if g.utype and g.utype.startswith(utype)] |
| 49 |
|
| 50 |
|
| 51 |
def _getUtypedGroupsFromResource(votRes: V.RESOURCE, utype: str |
| 52 |
) -> List[V.GROUP]: |
| 53 |
"""yields groups of utype from below the V.RESOURCE votRes. |
| 54 |
|
| 55 |
The function recursively searches child TABLE and RESOURCE |
| 56 |
instances. |
| 57 |
""" |
| 58 |
stcGroups = [] |
| 59 |
stcGroups.extend(_getUtypedGroupsFromAny(votRes, utype)) |
| 60 |
for child in votRes.iterChildren(): |
| 61 |
if isinstance(child, V.TABLE): |
| 62 |
stcGroups.extend(_getUtypedGroupsFromAny(child, utype)) |
| 63 |
elif isinstance(child, V.RESOURCE): |
| 64 |
stcGroups.extend(_getUtypedGroupsFromResource(child, utype)) |
| 65 |
return stcGroups |
| 66 |
|
| 67 |
|
| 68 |
def _getUtypedGroupsFromVOTable(vot: V.VOTABLE, utype: str |
| 69 |
) -> List[V.GROUP]: |
| 70 |
"""returns a list of all groups of utype from a votable. |
| 71 |
|
| 72 |
Make this available in the votable library? |
| 73 |
""" |
| 74 |
allGroups = [] |
| 75 |
for res in vot.iterChildrenOfType(V.RESOURCE): |
| 76 |
allGroups.extend(_getUtypedGroupsFromResource(res, utype)) |
| 77 |
return allGroups |
| 78 |
|
| 79 |
|
| 80 |
def _extractUtypes(group: VOTable.GROUP, refClass: Type[stc.ColRef] |
| 81 |
) -> Iterator[Tuple[str, Union[stc.ColRef, str]]]: |
| 82 |
"""yields utype-value pairs extracted from the children of group. |
| 83 |
|
| 84 |
refClass is the class to be used for column references. This will |
| 85 |
usually be a stanxml.Stub derived class. |
| 86 |
""" |
| 87 |
for child in group.iterChildren(): |
| 88 |
if isinstance(child, V.PARAM): |
| 89 |
yield child.utype, child.value |
| 90 |
elif isinstance(child, V.FIELDref): |
| 91 |
yield child.utype, refClass(child.ref) |
| 92 |
else: |
| 93 |
pass # other children are ignored. |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
########################## STC |
| 98 |
|
| 99 |
def unmarshal_STC(tableNode: V.TABLE |
| 100 |
) -> Iterator[Tuple[Dict, stc.STCSpec]]: |
| 101 |
"""iterates over pairs of (colInfo, system) pairs of STC information |
| 102 |
on tableNode. |
| 103 |
|
| 104 |
system are STC ASTs; colInfo maps column ids to the column utype in |
| 105 |
system. |
| 106 |
""" |
| 107 |
for obsLocGroup in _getUtypedGroupsFromAny(tableNode, |
| 108 |
"stc:CatalogEntryLocation"): |
| 109 |
utypes, colInfo = [], {} |
| 110 |
for utype, value in _extractUtypes(obsLocGroup, stc.ColRef): |
| 111 |
if isinstance(value, stc.ColRef): |
| 112 |
colInfo[value.dest] = utype |
| 113 |
utypes.append((utype, value)) |
| 114 |
yield colInfo, stc.parseFromUtypes(utypes) |
| 115 |
|
| 116 |
|
| 117 |
def _makeUtypeContainer( |
| 118 |
utype: str, value: str, getIdFor: Callable[[stc.ColRef], str] |
| 119 |
) -> Union[V.PARAM, V.FIELDref, V.PARAMref]: |
| 120 |
"""returns a PARAM or FIELDref element serializing the utype, value pair. |
| 121 |
|
| 122 |
If the value is a ColRef, the result will be a FIELDref. |
| 123 |
|
| 124 |
see marshal_STC for info on getIdFor |
| 125 |
""" |
| 126 |
if isinstance(value, stc.ColRef): |
| 127 |
destId = getIdFor(value) |
| 128 |
if value.toParam: |
| 129 |
return V.PARAMref(utype=utype, ref=destId) |
| 130 |
else: |
| 131 |
return V.FIELDref(utype=utype, ref=destId) |
| 132 |
else: |
| 133 |
return V.PARAM(name=utype.split(".")[-1], utype=utype, value=value, |
| 134 |
datatype="char", arraysize="*") |
| 135 |
|
| 136 |
|
| 137 |
def marshal_STC(ast: stc.STCSpec, getIdFor: Callable[[stc.ColRef], str] |
| 138 |
) -> Tuple[VOTable.GROUP, Dict[str, str]]: |
| 139 |
"""returns an stc:CatalogEntryLocation group for ast. |
| 140 |
|
| 141 |
ast is an AST object from GAVO's STC library. |
| 142 |
|
| 143 |
getIdFor must be a function returning the id for an stc.ColRef object. |
| 144 |
The main issue here is that the STC library deals with unqualified column |
| 145 |
names. These may clash when several tables are combined within a VOTable. |
| 146 |
Thus, you will have to have some mechanism to generate unique ids for |
| 147 |
the FIELDs (e.g. using utils.IdManagerMixin). getIdFor must resolve |
| 148 |
ColRefs (with column names in their dest) to unique ids. |
| 149 |
""" |
| 150 |
container = V.GROUP(utype="stc:CatalogEntryLocation") |
| 151 |
utypeMap = {} |
| 152 |
for utype, value in stc.getUtypes(ast, includeDMURI=True): |
| 153 |
try: |
| 154 |
container[_makeUtypeContainer(utype, value, getIdFor)] |
| 155 |
utypeMap[utype.lower()] = value |
| 156 |
except KeyError: # column referenced is not in result |
| 157 |
pass |
| 158 |
return container, utypeMap |