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
22
23
24
25
26
27 from gavo.votable.model import VOTable as V
28
29
30 try:
31 from gavo import stc
32 stcAvailable = True
33 except ImportError:
34 stcAvailable = False
35
36
37
38
43
44
46 """yields groups of utype from below the V.RESOURCE votRes.
47
48 The function recursively searches child TABLE and RESOURCE
49 instances.
50 """
51 stcGroups = []
52 stcGroups.extend(_getUtypedGroupsFromAny(votRes, utype))
53 for child in votRes.iterChildren():
54 if isinstance(child, V.TABLE):
55 stcGroups.extend(_getUtypedGroupsFromAny(child, utype))
56 elif isinstance(child, V.RESOURCE):
57 stcGroups.extend(_getUtypedGroupsFromResource(child, utype))
58 return stcGroups
59
60
62 """returns a list of all groups of utype from a votable.
63
64 Make this available in the votable library?
65 """
66 allGroups = []
67 for res in vot.iterChildrenOfType(V.RESOURCE):
68 allGroups.extend(_getUtypedGroupsFromResource(res, utype))
69 return allGroups
70
71
85
86
87
88
89
91 """iterates over pairs of (colInfo, system) pairs of STC information
92 on tableNode.
93
94 system are STC ASTs; colInfo maps column ids to the column utype in
95 system.
96 """
97 for obsLocGroup in _getUtypedGroupsFromAny(tableNode,
98 "stc:CatalogEntryLocation"):
99 utypes, colInfo = [], {}
100 for utype, value in _extractUtypes(obsLocGroup, stc.ColRef):
101 if isinstance(value, stc.ColRef):
102 colInfo[value.dest] = utype
103 utypes.append((utype, value))
104 yield colInfo, stc.parseFromUtypes(utypes)
105
106
123
124
126 """returns an stc:CatalogEntryLocation group for ast.
127
128 ast is an AST object from GAVO's STC library.
129
130 getIdFor must be a function returning the id for an stc.ColRef object.
131 The main issue here is that the STC library deals with unqualified column
132 names. These may clash when several tables are combined within a VOTable.
133 Thus, you will have to have some mechanism to generate unique ids for
134 the FIELDs (e.g. using utils.IdManagerMixin). getIdFor must resolve
135 ColRefs (with column names in their dest) to unique ids.
136 """
137 container = V.GROUP(utype="stc:CatalogEntryLocation")
138 utypeMap = {}
139 for utype, value in stc.getUtypes(ast, includeDMURI=True):
140 try:
141 container[_makeUtypeContainer(utype, value, getIdFor)]
142 utypeMap[utype.lower()] = value
143 except KeyError:
144 pass
145 return container, utypeMap
146