| 1 |
""" |
| 2 |
Cores are the standard object for computing things in the DC. |
| 3 |
|
| 4 |
This module also contains the registry for cores. If you want to |
| 5 |
be able to refer to cores from within an RD, you need to enter your |
| 6 |
core here. |
| 7 |
|
| 8 |
Cores return pairs of a type and a payload. Renderers should normally |
| 9 |
be prepared to receive (None, OutputTable) and (mime/str, data/str), |
| 10 |
though individual cores might return other stuff (and then only |
| 11 |
work with select renderers). |
| 12 |
|
| 13 |
In general, the input table definition is mandatory, i.e., you have |
| 14 |
to come up with it. Services do this either ad-hoc from input from |
| 15 |
the web or using an inputDD. |
| 16 |
|
| 17 |
The output table, on the other hand, is "advisory", mainly for registry |
| 18 |
and documentation purposes ("I *could* return this"). In particular |
| 19 |
DBBasedCores will usually adapt to the service's wishes when it comes |
| 20 |
to the output table structure, not to mention the ADQL core. This doesn't |
| 21 |
hurt since whatever is returned comes with structure documentation of |
| 22 |
its own. |
| 23 |
|
| 24 |
Cores may also want to adapt to renderers. This is a bit of a hack to |
| 25 |
support, e.g., form and scs within a single service, which avoids |
| 26 |
two different VOResource records just because of slightly differning |
| 27 |
input definitions. |
| 28 |
|
| 29 |
The interface here is the adaptForRenderer method. It takes a single |
| 30 |
argument, the renderer, either as a simple name resolvable in the renderer |
| 31 |
registry, or as a renderer instance or class. It must return a new |
| 32 |
core instance. |
| 33 |
|
| 34 |
Currently, two mechanisms for core adaptation are in place: |
| 35 |
|
| 36 |
(1) generically, inputKeys can have properties onlyForRenderer and |
| 37 |
notForRenderer with the obvious semantics. |
| 38 |
|
| 39 |
(2) cores that have condDescs may adapt by virtue of condDesc's |
| 40 |
adaptForRenderer method. |
| 41 |
""" |
| 42 |
|
| 43 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 44 |
#c |
| 45 |
#c This program is free software, covered by the GNU GPL. See the |
| 46 |
#c COPYING file in the source distribution. |
| 47 |
|
| 48 |
|
| 49 |
from gavo import base |
| 50 |
from gavo import rsc |
| 51 |
from gavo import rscdef |
| 52 |
from gavo import utils |
| 53 |
from gavo.svcs import inputdef |
| 54 |
from gavo.svcs import outputdef |
| 55 |
|
| 56 |
|
| 57 |
CORE_REGISTRY = { |
| 58 |
# elementName -> module (without gavo.), class name |
| 59 |
"adqlCore": ("protocols.adqlglue", "ADQLCore"), |
| 60 |
"biblinksCore": ("protocols.biblinks", "BiblinksCore"), |
| 61 |
"customCore": ("svcs.customcore", "CustomCore"), |
| 62 |
"datalinkCore": ("protocols.datalink", "DatalinkCore"), |
| 63 |
"dbCore": ("svcs.standardcores", "DBCore"), |
| 64 |
"debugCore": ("svcs.core", "DebugCore"), |
| 65 |
"fancyQueryCore": ("svcs.standardcores", "FancyQueryCore"), |
| 66 |
"fixedQueryCore": ("svcs.standardcores", "FixedQueryCore"), |
| 67 |
"nullCore": ("svcs.standardcores", "NullCore"), |
| 68 |
"productCore": ("protocols.products", "ProductCore"), |
| 69 |
"pythonCore": ("svcs.customcore", "PythonCore"), |
| 70 |
"registryCore": ("registry.oaiinter", "RegistryCore"), |
| 71 |
"scsCore": ("protocols.scs", "SCSCore"), |
| 72 |
"siapCutoutCore": ("protocols.siap", "SIAPCutoutCore"), |
| 73 |
"ssapCore": ("protocols.ssap", "SSAPCore"), |
| 74 |
"tapCore": ("protocols.tap", "TAPCore"), |
| 75 |
"uploadCore": ("svcs.uploadcores", "UploadCore"), |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
def getCore(name): |
| 80 |
if name not in CORE_REGISTRY: |
| 81 |
raise base.NotFoundError(name, "core", "registered cores") |
| 82 |
cls = utils.loadInternalObject(*CORE_REGISTRY[name]) |
| 83 |
if cls.name_!=name: |
| 84 |
raise base.ReportableError("Internal Error: Core %s is registered" |
| 85 |
" under the wrong name."%name, |
| 86 |
hint="This is probably a typo in svcs.core; it needs" |
| 87 |
" to be fixed there") |
| 88 |
return cls |
| 89 |
|
| 90 |
|
| 91 |
class _OutputTableFactory(object): |
| 92 |
"""The childFactory for a core's outputTable. |
| 93 |
|
| 94 |
On call, it will return a slightly amended copy of the |
| 95 |
queried table. |
| 96 |
|
| 97 |
This means that you must set the queried table before mentioning the |
| 98 |
outputs. |
| 99 |
""" |
| 100 |
name_ = "outputTable" |
| 101 |
|
| 102 |
def __call__(self, parent): |
| 103 |
if parent._ot_prototype is None: |
| 104 |
outputTD = outputdef.OutputTableDef(parent) |
| 105 |
parent._ot_prototype = base.parseFromString(outputTD, |
| 106 |
parent.outputTableXML or "<outputTable/>") |
| 107 |
return parent._ot_prototype.copy(parent) |
| 108 |
|
| 109 |
|
| 110 |
class Core(base.Structure): |
| 111 |
"""A definition of the "active" part of a service. |
| 112 |
|
| 113 |
A core will receive input from a renderer in the form of a ``svcs.CoreArgs`` |
| 114 |
(see `Core Args`_). A core will return a table or perhaps directly |
| 115 |
data as discussed in `DaCHS' Service Interface`_ . |
| 116 |
|
| 117 |
The abstract core element will never occur in resource descriptors. See |
| 118 |
`Cores Available`_ for concrete cores. Use the names of the concrete |
| 119 |
cores in RDs. |
| 120 |
""" |
| 121 |
name_ = "core" |
| 122 |
|
| 123 |
inputTableXML = None |
| 124 |
outputTableXML = None |
| 125 |
|
| 126 |
# the cached prototype of the output table, filled in by |
| 127 |
# _OutputTableFactory |
| 128 |
_ot_prototype = None |
| 129 |
|
| 130 |
_rd = rscdef.RDAttribute() |
| 131 |
_inputTable = base.StructAttribute("inputTable", |
| 132 |
default=base.NotGiven, |
| 133 |
childFactory=inputdef.InputTD, |
| 134 |
description="Description of the input data", |
| 135 |
copyable=True) |
| 136 |
|
| 137 |
_outputTable = base.StructAttribute("outputTable", |
| 138 |
default=base.NotGiven, |
| 139 |
childFactory=_OutputTableFactory(), |
| 140 |
description="Table describing what fields are available from this core", |
| 141 |
copyable=True) |
| 142 |
|
| 143 |
_original = base.OriginalAttribute() |
| 144 |
|
| 145 |
_properties = base.PropertyAttribute(copyable=True) |
| 146 |
|
| 147 |
def __init__(self, parent, **kwargs): |
| 148 |
if self.inputTableXML is not None: |
| 149 |
if "inputTable" not in kwargs: |
| 150 |
kwargs["inputTable"] = base.parseFromString( |
| 151 |
inputdef.InputTD, self.inputTableXML) |
| 152 |
|
| 153 |
base.Structure.__init__(self, parent, **kwargs) |
| 154 |
|
| 155 |
def __repr__(self): |
| 156 |
return "<%s at %s>"%(self.__class__.__name__, id(self)) |
| 157 |
|
| 158 |
def __str__(self): |
| 159 |
return repr(self) |
| 160 |
|
| 161 |
def completeElement(self, ctx): |
| 162 |
super().completeElement(ctx) |
| 163 |
self.initialize() |
| 164 |
if self.inputTable is base.NotGiven: |
| 165 |
self.inputTable = base.makeStruct(inputdef.InputTD) |
| 166 |
if self.outputTable is base.NotGiven: |
| 167 |
self.outputTable = self._outputTable.childFactory(self) |
| 168 |
|
| 169 |
def initialize(self): |
| 170 |
"""override to configure the custom core before use. |
| 171 |
|
| 172 |
This is typically where you pull input or output tables |
| 173 |
from the RD in customCores. Actual DaCHS code should use |
| 174 |
completeElement as usual. |
| 175 |
""" |
| 176 |
|
| 177 |
def adaptForRenderer(self, renderer, queryMeta): |
| 178 |
"""returns a core object tailored for renderer. |
| 179 |
""" |
| 180 |
newIT = self.inputTable.adaptForRenderer(renderer) |
| 181 |
if newIT is self.inputTable: |
| 182 |
return self |
| 183 |
else: |
| 184 |
return self.change(inputTable=newIT) |
| 185 |
|
| 186 |
def run(self, service, inputData, queryMeta): |
| 187 |
raise NotImplementedError("%s cores are missing the run method"% |
| 188 |
self.__class__.__name__) |
| 189 |
|
| 190 |
def makeUserDoc(self): |
| 191 |
return ("Polymorphous core element. May contain any of the cores" |
| 192 |
" mentioned in `Cores Available`_ .") |
| 193 |
|
| 194 |
def getRelevantTables(self): |
| 195 |
"""returns a sequence of "published tables". |
| 196 |
|
| 197 |
This is for VOSI tables, and hence what's a "published table" is a |
| 198 |
bit fuzzy. By default we return the output table if it looks non-trivial, |
| 199 |
or else nothing at all. |
| 200 |
""" |
| 201 |
if self.outputTable and self.outputTable.columns: |
| 202 |
return [self.outputTable] |
| 203 |
return [] |
| 204 |
|
| 205 |
|
| 206 |
class DebugCore(Core): |
| 207 |
"""a core that returns its arguments stringified in a table. |
| 208 |
|
| 209 |
You need to provide an external input tables for these. |
| 210 |
""" |
| 211 |
name_ = "debugCore" |
| 212 |
|
| 213 |
outputTableXML = """ |
| 214 |
<outputTable> |
| 215 |
<outputField name="arg_key" type="text" |
| 216 |
description="Name of an input parameter"/> |
| 217 |
<outputField name="arg_value" type="text" |
| 218 |
description="(First) value passed (or None)"/> |
| 219 |
</outputTable>""" |
| 220 |
|
| 221 |
def run(self, service, inputTable, queryMeta): |
| 222 |
rows = [] |
| 223 |
for par in inputTable.inputTD: |
| 224 |
if par.type=="file": |
| 225 |
value = inputTable.args[par.name][1].read() |
| 226 |
else: |
| 227 |
value = inputTable.args[par.name] |
| 228 |
rows.append({"arg_key": par.name, "arg_value": utils.safe_str(value)}) |
| 229 |
return rsc.TableForDef(self.outputTable, rows=rows) |
| 230 |
|
| 231 |
|