| 1 |
""" |
| 2 |
The specialised annotations for the various entities of VO-DML. |
| 3 |
|
| 4 |
As it's needed for the definition of models, the annotation of immediate |
| 5 |
atoms is already defined in common; also see there for the base class |
| 6 |
of these. |
| 7 |
""" |
| 8 |
|
| 9 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 |
#c |
| 11 |
#c This program is free software, covered by the GNU GPL. See the |
| 12 |
#c COPYING file in the source distribution. |
| 13 |
|
| 14 |
|
| 15 |
# topnote[1]: copying TableRelativeAnnotations is probably not useful in |
| 16 |
# the typical case; when you copy an annotation, that's probably because |
| 17 |
# you copied a table, and hence your columns and params are different |
| 18 |
# from the original table. When copying the annotations, they will |
| 19 |
# still point to the old instances. Still, for consistency, I'm |
| 20 |
# implementing the copy methods here. |
| 21 |
|
| 22 |
|
| 23 |
import weakref |
| 24 |
|
| 25 |
from gavo.dm import common |
| 26 |
from gavo.votable import V |
| 27 |
|
| 28 |
|
| 29 |
class ParamLikeAnnotation(common.TableRelativeAnnotation): |
| 30 |
def __init__(self, name, paramlike, instance): |
| 31 |
super().__init__(name, instance) |
| 32 |
self.weakref = weakref.ref(paramlike) |
| 33 |
paramlike.dmRoles.append(weakref.ref(self)) |
| 34 |
|
| 35 |
def __repr__(self): |
| 36 |
dest = self.weakref() |
| 37 |
if dest is None: |
| 38 |
return f"<defunct {self.name} annotation>" |
| 39 |
else: |
| 40 |
return f"<{self.name} {self.__class__.__name__} of {dest.name}>" |
| 41 |
|
| 42 |
@property |
| 43 |
def value(self): |
| 44 |
return self.weakref() |
| 45 |
|
| 46 |
def copy(self, newInstance): |
| 47 |
# see topnote(1) |
| 48 |
return self.__class__(self.name, self.weakref(), newInstance) |
| 49 |
|
| 50 |
|
| 51 |
class ColumnAnnotation(ParamLikeAnnotation): |
| 52 |
"""An annotation of a table column. |
| 53 |
|
| 54 |
These reference DaCHS columns. |
| 55 |
""" |
| 56 |
def getVOT(self, ctx, instance): |
| 57 |
ctx.curDMAttr( |
| 58 |
ref=ctx.getOrMakeIdFor(self.value, |
| 59 |
suggestion=getattr(instance, "name", None))) |
| 60 |
return None # no new element generated |
| 61 |
|
| 62 |
|
| 63 |
class ParamAnnotation(ParamLikeAnnotation): |
| 64 |
"""An annotation of a table param. |
| 65 |
|
| 66 |
NOTE: in getVOT, container MUST be the table itself, as the table has |
| 67 |
params of its own and does *not* share tableDef's one. |
| 68 |
""" |
| 69 |
def getVOT(self, ctx, container): |
| 70 |
referenced = container.getParamByName(self.value.name) |
| 71 |
ctx.curDMAttr(ref=ctx.getOrMakeIdFor(referenced, |
| 72 |
suggestion=getattr(self.value, "name", None))) |
| 73 |
return None # no new element generated |
| 74 |
|
| 75 |
|
| 76 |
def _the(gen): |
| 77 |
"""returns the first thing the generator gen spits out and makes sure |
| 78 |
there's nothing more |
| 79 |
""" |
| 80 |
res = next(gen) |
| 81 |
try: |
| 82 |
extra = next(gen) |
| 83 |
except StopIteration: |
| 84 |
return res |
| 85 |
raise TypeError("Generator expected to only return one thing returned" |
| 86 |
" extra %s"%repr(extra)) |
| 87 |
|
| 88 |
|
| 89 |
class GroupRefAnnotation(common.TableRelativeAnnotation): |
| 90 |
"""An annotation always referencing a group that's not lexically |
| 91 |
within the parent. |
| 92 |
""" |
| 93 |
def __init__(self, name, objectReferenced, instance): |
| 94 |
common.TableRelativeAnnotation.__init__(self, name, instance) |
| 95 |
self.objectReferenced = objectReferenced |
| 96 |
|
| 97 |
def copy(self, newInstance): |
| 98 |
# see topnote(1) |
| 99 |
return self.__class__(self.name, self.objectReferenced, newInstance) |
| 100 |
|
| 101 |
def getVOT(self, ctx, instance): |
| 102 |
if id(self.objectReferenced) not in ctx.groupIdsInTree: |
| 103 |
ctx.vodmlTemplates[ |
| 104 |
self.objectReferenced.getVOT(ctx, instance)( |
| 105 |
ID=ctx.getOrMakeIdFor(self.objectReferenced))] |
| 106 |
ctx.groupIdsInTree.add(id(self.objectReferenced)) |
| 107 |
|
| 108 |
return V.REFERENCE(dmref=ctx.getIdFor(self.objectReferenced)) |
| 109 |
|
| 110 |
|
| 111 |
class ForeignKeyAnnotation(common.TableRelativeAnnotation): |
| 112 |
"""An annotation pointing to an annotation in a different table. |
| 113 |
|
| 114 |
These are constructed with the attribute name and the foreign key RD |
| 115 |
object. |
| 116 |
""" |
| 117 |
def __init__(self, name, fk, instance): |
| 118 |
common.TableRelativeAnnotation.__init__(self, name, instance) |
| 119 |
self.value = weakref.proxy(fk) |
| 120 |
|
| 121 |
def copy(self, newInstance): |
| 122 |
return self.__class__(self.name, self.value, newInstance) |
| 123 |
|
| 124 |
def getVOT(self, ctx, instance): |
| 125 |
# the main trouble here is: What if there's multiple foreign keys |
| 126 |
# into destTD? To prevent multiple inclusions of a single |
| 127 |
# table, we add a reference to our serialised VOTable stan in |
| 128 |
# destTD's _FKR_serializedVOT attribute. That will fail |
| 129 |
# if we produce two VOTables from the same table at the same time, |
| 130 |
# but let's worry about that later. |
| 131 |
|
| 132 |
destTD = self.value.inTable |
| 133 |
srcTD = self.value.parent |
| 134 |
|
| 135 |
raise NotImplementedError("Do not know how to annotate a foreign key") |
| 136 |
pkDecl = V.GROUP(dmrole="vo-dml:ObjectTypeInstance.ID")[[ |
| 137 |
V.FIELDref(ref=ctx.getOrMakeIdFor( |
| 138 |
destTD.tableDef.getColumnByName(colName))) |
| 139 |
for colName in self.foreignKey.dest]] |
| 140 |
pkDecl(ID=ctx.getOrMakeIdFor(pkDecl)) |
| 141 |
|
| 142 |
fkDecl = V.GROUP(ref=ctx.getOrMakeIdFor(pkDecl), |
| 143 |
dmtype="vo-dml:ORMReference")[ |
| 144 |
[V.FIELDref(ref=ctx.getIdFor(srcTD.getColumnByName(colName))) |
| 145 |
for colName in self.foreignKey.source]] |
| 146 |
|
| 147 |
targetVOT = getattr(destTD, "_FKR_serializedVOT", |
| 148 |
lambda: None)() |
| 149 |
# weakrefs are None if expired |
| 150 |
if targetVOT is None: |
| 151 |
targetVOT = ctx.makeTable(destTD) |
| 152 |
destTD._FKR_serializedVOT = weakref.ref(targetVOT) |
| 153 |
ctx.getEnclosingResource()[targetVOT] |
| 154 |
|
| 155 |
targetVOT[pkDecl] |
| 156 |
|
| 157 |
return fkDecl |