| 1 |
""" |
| 2 |
User-defined cores, either with an external file or with code within |
| 3 |
the RD. |
| 4 |
""" |
| 5 |
|
| 6 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 7 |
#c |
| 8 |
#c This program is free software, covered by the GNU GPL. See the |
| 9 |
#c COPYING file in the source distribution. |
| 10 |
|
| 11 |
|
| 12 |
from gavo import base |
| 13 |
from gavo import rsc |
| 14 |
from gavo import rscdef |
| 15 |
from gavo import utils |
| 16 |
from gavo.svcs import core |
| 17 |
|
| 18 |
|
| 19 |
class CustomCore(core.Core): |
| 20 |
"""A wrapper around a core defined in a module. |
| 21 |
|
| 22 |
This core lets you write your own cores in modules. |
| 23 |
|
| 24 |
The module must define a class Core. When the custom core is |
| 25 |
encountered, this class will be instantiated and will be used |
| 26 |
instead of the CustomCore, so your code should probably inherit |
| 27 |
core.Core. |
| 28 |
|
| 29 |
See `Writing Custom Cores`_ for details. |
| 30 |
""" |
| 31 |
name_ = "customCore" |
| 32 |
|
| 33 |
_module = rscdef.ResdirRelativeAttribute("module", |
| 34 |
description="Path to the module containing the core definition.", |
| 35 |
copyable=True, default=base.Undefined) |
| 36 |
|
| 37 |
def completeElement(self, ctx): |
| 38 |
overrideInput = self.inputTable is not base.NotGiven |
| 39 |
overrideOutput = self.outputTable is not base.NotGiven |
| 40 |
|
| 41 |
super().completeElement(ctx) |
| 42 |
|
| 43 |
try: |
| 44 |
loaded_module, _ = utils.loadPythonModule(self.module) |
| 45 |
except Exception as msg: |
| 46 |
raise base.StructureError("Cannot load custom core %s: %s"%( |
| 47 |
self.module, msg)) |
| 48 |
innerCore = base.makeStruct(loaded_module.Core, parent_=self.parent) |
| 49 |
innerCore.id = self.id |
| 50 |
|
| 51 |
# let people overwrite inputTable and outputTable from the RD |
| 52 |
# (this may be a questionable feature, but there you go. |
| 53 |
if overrideInput: |
| 54 |
innerCore.inputTable = self.inputTable |
| 55 |
if overrideOutput: |
| 56 |
innerCore.outputTable = self.outputTable |
| 57 |
|
| 58 |
# and we let people override properties on the inner core, too. |
| 59 |
innerCore.properties.update(self.properties) |
| 60 |
|
| 61 |
# now replace the "empty" core from the xml in the id map and in the |
| 62 |
# parent with our inner core |
| 63 |
if ctx.idmap.get(self.id) is self: |
| 64 |
ctx.idmap[self.id] = innerCore |
| 65 |
self.parent.feedObject(self.name_, innerCore) |
| 66 |
|
| 67 |
def onElementComplete(self): |
| 68 |
super().onElementComplete() |
| 69 |
# we have already added the inner core in completeElement, so we |
| 70 |
# should not leave any trace here. |
| 71 |
raise base.Ignore(self) |
| 72 |
|
| 73 |
|
| 74 |
class CoreProc(rscdef.ProcApp): |
| 75 |
"""A definition of a pythonCore's functionality. |
| 76 |
|
| 77 |
This is a procApp complete with setup and code; you could inherit |
| 78 |
between these. |
| 79 |
|
| 80 |
coreProcs see the embedding service, the input table passed, and the |
| 81 |
query metadata as service, inputTable, and queryMeta, respectively. |
| 82 |
|
| 83 |
The core itself is available as self. |
| 84 |
""" |
| 85 |
name_ = "coreProc" |
| 86 |
requiredType = "coreProc" |
| 87 |
formalArgs = "self, service, inputTable, queryMeta" |
| 88 |
|
| 89 |
additionalNamesForProcs = { |
| 90 |
"rsc": rsc |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
class PythonCore(core.Core): |
| 95 |
"""A core doing computation using a piece of python. |
| 96 |
|
| 97 |
See `Python Cores instead of Custom Cores`_ in the reference. |
| 98 |
""" |
| 99 |
name_ = "pythonCore" |
| 100 |
|
| 101 |
_computer = base.StructAttribute("coreProc", default=base.Undefined, |
| 102 |
childFactory=CoreProc, |
| 103 |
description="Code making the outputTable from the inputTable.", |
| 104 |
copyable=True) |
| 105 |
|
| 106 |
def expand(self, s): |
| 107 |
# macro expansion should ideally take place in the service, |
| 108 |
# but that's impossible in general because a core could be |
| 109 |
# in use by several services. Hence, we go ask the RD |
| 110 |
return self.rd.expand(s) |
| 111 |
|
| 112 |
def run(self, service, inputTable, queryMeta): |
| 113 |
return self.coreProc.compile()(self, service, inputTable, queryMeta) |