| 1 | """ |
| 2 | Writing annotations in RDs. |
| 3 | |
| 4 | This module provides the glue between annotations (typically in SIL) |
| 5 | and the rest of the RDs. It provides the ResAnnotation struct, which |
| 6 | contains the SIL, and the makeAttributeAnnotation function that is a factory |
| 7 | for attribute annotations. |
| 8 | """ |
| 9 | |
| 10 | #c Copyright 2008-2025, the GAVO project <[email protected]> |
| 11 | #c |
| 12 | #c This program is free software, covered by the GNU GPL. See the |
| 13 | #c COPYING file in the source distribution. |
| 14 | |
| 15 | |
| 16 | import functoolsscip-python python python-stdlib 3.11 functools/__init__: |
| 17 | import itertoolsscip-python python python-stdlib 3.11 itertools/__init__: |
| 18 | |
| 19 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 20 | from gavo.dmscip-python python gavodachs 2.13 `gavo.dm`/__init__: import annotationsscip-python python gavodachs 2.13 `gavo.dm.annotations`/__init__: |
| 21 | from gavo.dmscip-python python gavodachs 2.13 `gavo.dm`/__init__: import commonscip-python python gavodachs 2.13 `gavo.dm.common`/__init__: |
| 22 | from gavo.dmscip-python python gavodachs 2.13 `gavo.dm`/__init__: import silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__: |
| 23 | from gavo.utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__: import stanxmlscip-python python gavodachs 2.13 `gavo.utils.stanxml`/__init__: |
| 24 | from gavo.utilsscip-python python gavodachs 2.13 `gavo.utils`/__init__: import algotricksscip-python python gavodachs 2.13 `gavo.utils.algotricks`/__init__: |
| 25 | |
| 26 | |
| 27 | stanxmlscip-python python gavodachs 2.13 `gavo.utils.stanxml`/__init__:.registerPrefixscip-python python gavodachs 2.13 `gavo.utils.stanxml`/registerPrefix.("vo-dml", "http://www.ivoa.net/xml/VODML/v1", |
| 28 | stanxmlscip-python python gavodachs 2.13 `gavo.utils.stanxml`/__init__:.schemaURLscip-python python gavodachs 2.13 `gavo.utils.stanxml`/schemaURL().("vo-dml.xsd")) |
| 29 | |
| 30 | |
| 31 | class SynthesizedRolesscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.Structurescip-python python gavodachs 2.13 `gavo.base.structure`/Structure#): |
| 32 | """DM annotation copied and adapted to a new table. |
| 33 | |
| 34 | This is a stand-in for DataModelRoles in tables not parsed from |
| 35 | XMLs. Their DM structure is defined through references the columns and |
| 36 | params make to the annotations their originals had. |
| 37 | |
| 38 | These have no attributes but just arrange for the new annotations |
| 39 | to be generated. |
| 40 | """ |
| 41 | name_scip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#name_. = "_synthesizedRoles" |
| 42 | |
| 43 | def synthesizeAnnotationsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self), rdscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(rd), ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(ctx)): |
| 44 | # don't overwrite existing annotations (usually, they |
| 45 | # will be the result of a previous run when multiple |
| 46 | # annotations are present). |
| 47 | if selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent..annotations: |
| 48 | return |
| 49 | |
| 50 | annotationMap = {} |
| 51 | |
| 52 | # First, construct a map from column/param annotations in |
| 53 | # the old annotations to new columns and params. As a |
| 54 | # side effect, the dmRoles of the new items are cleared. |
| 55 | for item in itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent..params, selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent..columns): |
| 56 | if item.parent!=selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent.: |
| 57 | # Don't annotate things that don't belong to us. We need to |
| 58 | # be careful there because in the current architecture, we |
| 59 | # would be changing dmRoles |
| 60 | continue |
| 61 | |
| 62 | if isinstance(item.dmRoles, listscip-python python python-stdlib 3.11 builtins/list#): |
| 63 | # item has been processed before, probably as part of another |
| 64 | # DM declaration. No need to repeat that. |
| 65 | continue |
| 66 | |
| 67 | for annotation in item.dmRoles.oldRoles: |
| 68 | # annotation is a weakref here; skip the annotation if the |
| 69 | # referenced thing has gone away |
| 70 | resolved = annotation() |
| 71 | if resolved is not None: |
| 72 | annotationMap[resolved] = item |
| 73 | item.dmRoles = [] |
| 74 | |
| 75 | # Then, collect the annotations we have to copy |
| 76 | oldInstances = setscip-python python python-stdlib 3.11 builtins/set#(ann.instance() for ann in annotationMap) |
| 77 | |
| 78 | # tell the instances to copy themselves, replacing references |
| 79 | # accordingly. |
| 80 | newInstances = [] |
| 81 | for oldInstance in oldInstances: |
| 82 | newInstances.appendscip-python python python-stdlib 3.11 builtins/list#append().( |
| 83 | oldInstance.copyWithAnnotationMap( |
| 84 | annotationMap, selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent., None)) |
| 85 | selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent..annotations = newInstances |
| 86 | |
| 87 | def completeElementscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(self), ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(ctx)): |
| 88 | ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(ctx).addExitFuncscip-python python gavodachs 2.13 `gavo.base.parsecontext`/ParseContext#addExitFunc().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(self).synthesizeAnnotationsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().) |
| 89 | superscip-python python python-stdlib 3.11 builtins/super#().completeElementscip-python python gavodachs 2.13 `gavo.base.common`/StructCallbacks#completeElement().(ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#completeElement().(ctx)) |
| 90 | |
| 91 | |
| 92 | class DataModelRolesscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.Structurescip-python python gavodachs 2.13 `gavo.base.structure`/Structure#): |
| 93 | """an annotation of a table in terms of data models. |
| 94 | |
| 95 | The content of this element is a Simple Instance Language clause. |
| 96 | """ |
| 97 | |
| 98 | # We used to defer the SIL parsing until the end of the XML parse |
| 99 | # in order to be relaxed about forward references. That, however, |
| 100 | # is not a good idea because later elements in the RD may copy the table, |
| 101 | # and its annotations will be incomplete then. Instead, we're now |
| 102 | # building the annotation when the element is complete and worry |
| 103 | # about cross-table forward references when they turn up. |
| 104 | # |
| 105 | # There's an additional complication in that we may want to |
| 106 | # access parsed annotations while parsing other annotations |
| 107 | # (e.g., when processing foreign keys). |
| 108 | # To allow the thing to "parse itself" in such situations, we do |
| 109 | # all the crazy magic with the _buildAnnotation function. |
| 110 | name_scip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#name_. = "dm" |
| 111 | |
| 112 | _silscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#_sil. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.DataContentscip-python python gavodachs 2.13 `gavo.base.structure`/DataContent#(descriptionscip-python python gavodachs 2.13 `gavo.base.structure`/DataContent#__init__().(description)="SIL (simple instance language)" |
| 113 | " annotation.", copyable=True) |
| 114 | |
| 115 | def getAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(self), roleNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(roleName), containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(container), instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(instance)): |
| 116 | return annotationsscip-python python gavodachs 2.13 `gavo.dm.annotations`/__init__:.GroupRefAnnotationscip-python python gavodachs 2.13 `gavo.dm.annotations`/GroupRefAnnotation#(roleNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(roleName), selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(self).parsescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(), instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#getAnnotation().(instance)) |
| 117 | |
| 118 | def onParentCompletescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#onParentComplete().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#onParentComplete().(self)): |
| 119 | selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#onParentComplete().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent..annotations.append(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#onParentComplete().(self).parsescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().()) |
| 120 | |
| 121 | def parsescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self)): |
| 122 | """returns a parsed version of the embedded annotation. |
| 123 | |
| 124 | The parse really is only run once, but you should only call it once |
| 125 | the embedding table is complete, because this needs to resolve table |
| 126 | params and columns (and possibly other dm elements). |
| 127 | """ |
| 128 | if hasattr(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self), "_parsedAnnotation"): |
| 129 | return selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self)._parsedAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#_parsedAnnotation. |
| 130 | |
| 131 | try: |
| 132 | selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self)._parsedAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#_parsedAnnotation. = silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.getAnnotationscip-python python gavodachs 2.13 `gavo.dm.sil`/getAnnotation().( |
| 133 | selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self).content_, getAnnotationMakerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/getAnnotationMaker().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self).parentscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#parent.)) |
| 134 | except Exceptionscip-python python python-stdlib 3.11 builtins/Exception# as ex: |
| 135 | basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifyInfoscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifyInfo().("While parsing the SIL below: %s"%strscip-python python python-stdlib 3.11 builtins/str#(ex)) |
| 136 | basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..notifyInfoscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#notifyInfo().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self).content_) |
| 137 | raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..logOldExcscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#logOldExc().(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructureErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#(strscip-python python python-stdlib 3.11 builtins/str#(ex), |
| 138 | posscip-python python gavodachs 2.13 `gavo.utils.excs`/StructureError#__init__().(pos)=selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self).getSourcePositionscip-python python gavodachs 2.13 `gavo.base.structure`/StructureBase#getSourcePosition().())) |
| 139 | return selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#parse().(self)._parsedAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#_parsedAnnotation. |
| 140 | |
| 141 | |
| 142 | def copyscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(self), newParentscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(newParent), ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(ctx)): |
| 143 | # we use the general mechanism used for recovering annotations from |
| 144 | # columns and params in tables here so we're independent of |
| 145 | # changes in columns. |
| 146 | return SynthesizedRolesscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#(newParentscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(newParent)).finishElement(ctxscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#copy().(ctx)) |
| 147 | |
| 148 | |
| 149 | class DataModelRolesAttributescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructListAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#): |
| 150 | """an attribute allowing data model annotation using SIL. |
| 151 | |
| 152 | It will also give an annotations attribute on the instance, and a |
| 153 | getAnnotationsOfType method letting you pull out a specific annotation. |
| 154 | |
| 155 | For situation where an existing annotation should be copied from |
| 156 | annotations coming in through columns and/or params, the attribute |
| 157 | also gives an updateAnnotationFromChildren method. It will do |
| 158 | nothing if some annotation already exists. |
| 159 | """ |
| 160 | def __init__scip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#__init__().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#__init__().(self)): |
| 161 | basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructListAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#.__init__scip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#__init__().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#__init__().(self), |
| 162 | "dm", |
| 163 | childFactoryscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#__init__().(childFactory)=DataModelRolesscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRoles#, |
| 164 | descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#__init__().(description)="Annotations for data models.", |
| 165 | copyable=True) |
| 166 | |
| 167 | def iterParentMethodsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterParentMethods().(selfscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterParentMethods().(self)): |
| 168 | def iterAnnotationsOfTypescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().(instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().(instance), typeNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().(typeName)): |
| 169 | """returns the first annotation of the type passed. |
| 170 | """ |
| 171 | for ann in instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().(instance).annotations: |
| 172 | if ann.type==typeNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().(typeName): |
| 173 | yield ann |
| 174 | |
| 175 | yield ("iterAnnotationsOfType", iterAnnotationsOfTypescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#iterAnnotationsOfType().) |
| 176 | |
| 177 | def updateAnnotationFromChildrenscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#updateAnnotationFromChildren().(instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#updateAnnotationFromChildren().(instance)): |
| 178 | roleSynthesizer = SynthesizedRolesscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#(instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#updateAnnotationFromChildren().(instance)) |
| 179 | roleSynthesizer.synthesizeAnnotationsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/SynthesizedRoles#synthesizeAnnotations().(None, None) |
| 180 | yield ("updateAnnotationFromChildren", updateAnnotationFromChildrenscip-python python gavodachs 2.13 `gavo.dm.dmrd`/DataModelRolesAttribute#updateAnnotationFromChildren().) |
| 181 | |
| 182 | |
| 183 | def makeAttributeAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container), instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(instance), attNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attName), attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue)): |
| 184 | """returns a typed annotation for attValue within container. |
| 185 | |
| 186 | When attValue is a literal, this is largely trivial. If it's a reference, |
| 187 | this figures out what it points to and creates an annotation of |
| 188 | the appropriate type (e.g., ColumnAnnotation, ParamAnnotation, etc). |
| 189 | |
| 190 | container in current DaCHS should be a TableDef or something similar; |
| 191 | this function expects at least a getByName function and an rd attribute. |
| 192 | |
| 193 | instance is the root of the current annotation. Complex objects should |
| 194 | keep a (weak) reference to that. We don't have parent links in |
| 195 | our dm trees, and without a reference to the root there's no |
| 196 | way we can go "up". |
| 197 | |
| 198 | This is usually used as a callback from within sil.getAnnotation and |
| 199 | expects Atom and Reference instances as used there. |
| 200 | """ |
| 201 | if isinstance(attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue), silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.Atomscip-python python gavodachs 2.13 `gavo.dm.sil`/Atom#): |
| 202 | return commonscip-python python gavodachs 2.13 `gavo.dm.common`/__init__:.AtomicAnnotationscip-python python gavodachs 2.13 `gavo.dm.common`/AtomicAnnotation#(attNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attName), attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue), instancescip-python python gavodachs 2.13 `gavo.dm.common`/AtomicAnnotation#__init__().(instance)=instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(instance)) |
| 203 | |
| 204 | elif isinstance(attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue), silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.Referencescip-python python gavodachs 2.13 `gavo.dm.sil`/Reference#): |
| 205 | # try name-resolving first (resolveId only does id resolving on |
| 206 | # unadorned strings) |
| 207 | try: |
| 208 | res = containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container).getByName(attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue)) |
| 209 | except basescip-python python gavodachs 2.13 `gavo.base`/__init__:.NotFoundErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#: |
| 210 | if containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container).rd: |
| 211 | res = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.resolveIdscip-python python gavodachs 2.13 `gavo.base.parsecontext`/resolveId().(containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container).rd, attValuescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attValue), instancescip-python python gavodachs 2.13 `gavo.base.parsecontext`/resolveId().(instance)=containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container)) |
| 212 | else: |
| 213 | raise |
| 214 | |
| 215 | if not hasattr(res, "getAnnotation"): |
| 216 | raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructureErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#("Element %s cannot be referenced" |
| 217 | " within a data model."%repr(res)) |
| 218 | |
| 219 | return res.getAnnotation(attNamescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(attName), containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(container), instancescip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation().(instance)) |
| 220 | |
| 221 | else: |
| 222 | assert False |
| 223 | |
| 224 | |
| 225 | def getAnnotationMakerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/getAnnotationMaker().(containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/getAnnotationMaker().(container)): |
| 226 | """wraps makeAttributeAnnotationMaker such that names are resolved |
| 227 | within container. |
| 228 | """ |
| 229 | return functoolsscip-python python python-stdlib 3.11 functools/__init__:.partialscip-python python python-stdlib 3.11 functools/partial#(makeAttributeAnnotationscip-python python gavodachs 2.13 `gavo.dm.dmrd`/makeAttributeAnnotation()., containerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/getAnnotationMaker().(container)) |
| 230 | |
| 231 | |
| 232 | def _splitPathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(pathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path):strscip-python python python-stdlib 3.11 builtins/str#): |
| 233 | """returns a pair of (prefix)/(stuff-without-slash) for path. |
| 234 | """ |
| 235 | splitpoint = pathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path).rfindscip-python python python-stdlib 3.11 builtins/str#rfind().("/") |
| 236 | if splitpoint==-1: |
| 237 | return "", pathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path) |
| 238 | else: |
| 239 | return pathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path)[:splitpoint], pathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path)[splitpoint+1:] |
| 240 | |
| 241 | |
| 242 | def _makeDMValscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(stcObjscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(stcObj)): |
| 243 | """returns a value palatable to makeAttributeAnnotation from stcObj |
| 244 | |
| 245 | (which essentially will be an set.ColRef or some literal). |
| 246 | """ |
| 247 | if hasattr(stcObjscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(stcObj), "dest"): |
| 248 | return silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.Referencescip-python python gavodachs 2.13 `gavo.dm.sil`/Reference#(stcObjscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(stcObj).dest) |
| 249 | |
| 250 | else: |
| 251 | return silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.Atomscip-python python gavodachs 2.13 `gavo.dm.sil`/Atom#(stcObjscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(stcObj)) |
| 252 | |
| 253 | |
| 254 | def buildAdhocSTCscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(tablescip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(table), valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)): |
| 255 | """builds a modern adhoc STC annotation in table based on a mapping |
| 256 | of attribute paths to annotation targets in vals. |
| 257 | |
| 258 | This is only necessary as long as we want to support <stc> specs. The |
| 259 | keys here are, in effect defined in rscdef.tabledef; see _utypeToModern. |
| 260 | """ |
| 261 | if valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals).pop("!!!flavor!!!", None)!="SPHERICAL": |
| 262 | # anything else would need a lot more thought, and that's certainly not |
| 263 | # worth it. STC-S should die. |
| 264 | return |
| 265 | |
| 266 | # Sigh. STC1 split off the J/B from the epoch value. Let's cope in |
| 267 | # some way. |
| 268 | if valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals).pop("!!!yearDef!!!", "J")!="J": |
| 269 | raise NotImplementedErrorscip-python python python-stdlib 3.11 builtins/NotImplementedError#("We can only deal with Julian epochs") |
| 270 | if "/space/frame/epoch" in valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals): |
| 271 | if isinstance(valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)["/space/frame/epoch"], strscip-python python python-stdlib 3.11 builtins/str#): |
| 272 | valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)["/space/frame/epoch"] = "J"+valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)["/space/frame/epoch"] |
| 273 | |
| 274 | annMaker = getAnnotationMakerscip-python python gavodachs 2.13 `gavo.dm.dmrd`/getAnnotationMaker().(tablescip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(table)) |
| 275 | # astid is the id of the STC1 AST that has produced the original utypes. |
| 276 | # We want to record this because in votablewrite we want to re-use the |
| 277 | # COOSYS that already exists when there is an STC1 annotation. |
| 278 | astid = None |
| 279 | objs = {} |
| 280 | |
| 281 | for path in algotricksscip-python python gavodachs 2.13 `gavo.utils.algotricks`/__init__:.pathSortscip-python python gavodachs 2.13 `gavo.utils.algotricks`/pathSort().(valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals).keys()): |
| 282 | prefix, attname = _splitPathscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_splitPath().(path) |
| 283 | parent = objs[prefix] if path else None |
| 284 | |
| 285 | if path in buildAdhocSTCscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC()..typesForPaths: |
| 286 | # it's an intermediate object: create it |
| 287 | objs[path] = commonscip-python python gavodachs 2.13 `gavo.dm.common`/__init__:.ObjectAnnotationscip-python python gavodachs 2.13 `gavo.dm.common`/ObjectAnnotation#( |
| 288 | attname, buildAdhocSTCscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC()..typesForPaths[path], objs.getscip-python python python-stdlib 3.11 builtins/dict#get().("")) |
| 289 | if parent is not None: |
| 290 | parent.add(objs[path]) |
| 291 | |
| 292 | else: |
| 293 | # it's an attribute; translate stc's references and literals |
| 294 | parent.add(annMaker(objs[""], attname, _makeDMValscip-python python gavodachs 2.13 `gavo.dm.dmrd`/_makeDMVal().(valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)[path]))) |
| 295 | if astid is None: |
| 296 | # if value is a ColRef, whatever it refers to will define our |
| 297 | # STC1 AST |
| 298 | try: |
| 299 | destCol = tablescip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(table).getElementForName(valsscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(vals)[path].dest) |
| 300 | astid = strscip-python python python-stdlib 3.11 builtins/str#(id(destCol.stc)) |
| 301 | objs[""].add(annMaker(objs[""], "astid", silscip-python python gavodachs 2.13 `gavo.dm.sil`/__init__:.Atomscip-python python gavodachs 2.13 `gavo.dm.sil`/Atom#(astid))) |
| 302 | except (basescip-python python gavodachs 2.13 `gavo.base`/__init__:.NotFoundErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#, AttributeErrorscip-python python python-stdlib 3.11 builtins/AttributeError#): |
| 303 | # not a ColRef, apparently, try next val |
| 304 | pass |
| 305 | |
| 306 | tablescip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC().(table).annotations.append(objs[""]) |
| 307 | |
| 308 | |
| 309 | buildAdhocSTCscip-python python gavodachs 2.13 `gavo.dm.dmrd`/buildAdhocSTC()..typesForPaths = { |
| 310 | "": "votable:Coords", |
| 311 | "/space": "votable:SphericalCoordinate", |
| 312 | "/space/frame": "votable:SpaceFrame", |
| 313 | "/time": "votable:TimeCoordinate", |
| 314 | "/time/frame": "votable:TimeFrame", |
| 315 | } |