Package gavo :: Package votable :: Module modelgroups
[frames] | no frames]

Source Code for Module gavo.votable.modelgroups

  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-2019, the GAVO project 
 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   
 29   
 30  try: 
 31          from gavo import stc 
 32          stcAvailable = True 
 33  except ImportError: 
 34          stcAvailable = False 
 35   
 36   
 37  ######################## Helpers 
 38   
39 -def _getUtypedGroupsFromAny(votObj, utype):
40 return [g 41 for g in votObj.iterChildrenOfType(V.GROUP) 42 if g.utype and g.utype.startswith(utype)]
43 44
45 -def _getUtypedGroupsFromResource(votRes, utype):
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
61 -def _getUtypedGroupsFromVOTable(vot, utype):
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
72 -def _extractUtypes(group, refClass):
73 """yields utype-value pairs extracted from the children of group. 74 75 refClass is the class to be used for column references. This will 76 usually be a stanxml.Stub derived class. 77 """ 78 for child in group.iterChildren(): 79 if isinstance(child, V.PARAM): 80 yield child.utype, child.value 81 elif isinstance(child, V.FIELDref): 82 yield child.utype, refClass(child.ref) 83 else: 84 pass # other children are ignored.
85 86 87 88 ########################## STC 89
90 -def unmarshal_STC(tableNode):
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
107 -def _makeUtypeContainer(utype, value, getIdFor):
108 """returns a PARAM or FIELDref element serializing the utype, value pair. 109 110 If the value is a ColRef, the result will be a FIELDref. 111 112 see marshal_STC for info on getIdFor 113 """ 114 if isinstance(value, stc.ColRef): 115 destId = getIdFor(value) 116 if value.toParam: 117 return V.PARAMref(utype=utype, ref=destId) 118 else: 119 return V.FIELDref(utype=utype, ref=destId) 120 else: 121 return V.PARAM(name=utype.split(".")[-1], utype=utype, value=value, 122 datatype="char", arraysize="*")
123 124
125 -def marshal_STC(ast, getIdFor):
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: # column referenced is not in result 144 pass 145 return container, utypeMap
146