Package gavo :: Package svcs :: Module customcore
[frames] | no frames]

Source Code for Module gavo.svcs.customcore

 1  """ 
 2  User-defined cores 
 3   
 4  XXX TODO: Revise this to have events before module replayed. 
 5  """ 
 6   
 7  #c Copyright 2008-2019, the GAVO project 
 8  #c 
 9  #c This program is free software, covered by the GNU GPL.  See the 
10  #c COPYING file in the source distribution. 
11   
12   
13  import os 
14   
15  from gavo import base 
16  from gavo import rsc 
17  from gavo import rscdef 
18  from gavo import utils 
19  from gavo.svcs import core 
20   
21   
22 -class ModuleAttribute(base.UnicodeAttribute):
23 # XXX TODO: this is a bad hack since it assumes id on instance has already 24 # been set. See above on improving all this using an event replay framework. 25 typeDesc = "resdir-relative path to a module; no extension is allowed" 26
27 - def feed(self, ctx, instance, modName):
28 modName = os.path.join(instance.rd.resdir, modName) 29 userModule, _ = utils.loadPythonModule(modName) 30 newCore = userModule.Core(instance.parent) 31 ctx.idmap[instance.id] = newCore 32 raise base.Replace(newCore)
33 34
35 -class CustomCore(core.Core):
36 """A wrapper around a core defined in a module. 37 38 This core lets you write your own cores in modules. 39 40 The module must define a class Core. When the custom core is 41 encountered, this class will be instanciated and will be used 42 instead of the CustomCore, so your code should probably inherit 43 core.Core. 44 45 See `Writing Custom Cores`_ for details. 46 """ 47 name_ = "customCore" 48 49 _module = ModuleAttribute("module", default=base.Undefined, 50 description="Path to the module containing the core definition.")
51 52
53 -class CoreProc(rscdef.ProcApp):
54 """A definition of a pythonCore's functionalty. 55 56 This is a procApp complete with setup and code; you could inherit 57 between these. 58 59 coreProcs see the embedding service, the input table passed, and the 60 query metadata as service, inputTable, and queryMeta, respectively. 61 62 The core itself is available as self. 63 """ 64 name_ = "coreProc" 65 requiredType = "coreProc" 66 formalArgs = "self, service, inputTable, queryMeta" 67 68 additionalNamesForProcs = { 69 "rsc": rsc 70 }
71 72
73 -class PythonCore(core.Core):
74 """A core doing computation using a piece of python. 75 76 See `Python Cores instead of Custom Cores`_ in the reference. 77 """ 78 name_ = "pythonCore" 79 80 _computer = base.StructAttribute("coreProc", default=base.Undefined, 81 childFactory=CoreProc, 82 description="Code making the outputTable from the inputTable.", 83 copyable=True) 84
85 - def expand(self, s):
86 # macro expansion should ideally take place in the service, 87 # but that's impossible in general because a core could be 88 # in use by several services. Hence, we go ask the RD 89 return self.rd.expand(s)
90
91 - def run(self, service, inputTable, queryMeta):
92 return self.coreProc.compile()(self, service, inputTable, queryMeta)
93