| 1 |
""" |
| 2 |
Helpers for resource creation. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import copy |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
|
| 15 |
|
| 16 |
class FLUSH(object): |
| 17 |
"""A sentinel that grammars can yield to flush out records to the |
| 18 |
Database. |
| 19 |
|
| 20 |
This probably is only necessary in updating dispatched grammars to |
| 21 |
enforce dropping of rows dependent on some table. |
| 22 |
""" |
| 23 |
|
| 24 |
|
| 25 |
class ParamMixin(object): |
| 26 |
"""A mixin providing param processing. |
| 27 |
|
| 28 |
This is for tables and data elements. If you mix this in, you have |
| 29 |
to call _initParams(rscdefObject, params=None) |
| 30 |
|
| 31 |
rscdefObject is a TableDef or DataDef, params, if given, a dictionary |
| 32 |
mapping param names to param values. |
| 33 |
""" |
| 34 |
def _initParams(self, paramsDef, params=None): |
| 35 |
self.paramsDef = paramsDef |
| 36 |
self._params = self.paramsDef.params.deepcopy(self.paramsDef) |
| 37 |
if self.paramsDef.id: |
| 38 |
self._params.withinId = "%s %s"%( |
| 39 |
self.paramsDef.__class__.__name__, self.paramsDef.id) |
| 40 |
else: |
| 41 |
self._params.withinId = "anonymous "+self.paramsDef.__class__.__name__ |
| 42 |
if params is not None: |
| 43 |
self.setParams(params) |
| 44 |
|
| 45 |
def setParams(self, parDict, raiseOnBadKeys=True): |
| 46 |
for k, v in parDict.items(): |
| 47 |
try: |
| 48 |
self.setParam(k, v) |
| 49 |
except base.NotFoundError: |
| 50 |
if raiseOnBadKeys: |
| 51 |
raise |
| 52 |
|
| 53 |
def setParam(self, parName, value): |
| 54 |
"""sets a parameter to a value. |
| 55 |
|
| 56 |
String-typed values will be parsed, everything else is just entered |
| 57 |
directly. Trying to write to non-existing params will raise a |
| 58 |
NotFoundError. |
| 59 |
|
| 60 |
Do not write to params directly, you'll break things. |
| 61 |
""" |
| 62 |
self._params.getColumnByName(parName).set(value) |
| 63 |
|
| 64 |
def getParam(self, parName, default=base.NotGiven): |
| 65 |
"""retrieves a parameter (python) value. |
| 66 |
""" |
| 67 |
try: |
| 68 |
return self._params.getColumnByName(parName).value |
| 69 |
except base.NotFoundError: |
| 70 |
if default is not base.NotGiven: |
| 71 |
return default |
| 72 |
raise |
| 73 |
|
| 74 |
def getParamByName(self, parName): |
| 75 |
return self._params.getColumnByName(parName) |
| 76 |
|
| 77 |
def getParamByUtype(self, utype): |
| 78 |
return self._params.getColumnByUtype(utype) |
| 79 |
|
| 80 |
def iterParams(self): |
| 81 |
"""iterates over the parameters for this table. |
| 82 |
|
| 83 |
The items returned are rscdef.Param instances. |
| 84 |
""" |
| 85 |
return self._params |
| 86 |
|
| 87 |
def getParamDict(self): |
| 88 |
return dict((p.name, p.value) for p in self.iterParams()) |
| 89 |
|
| 90 |
|
| 91 |
class ParseOptions(object): |
| 92 |
"""see `function getParseOptions`_ . |
| 93 |
""" |
| 94 |
def __repr__(self): |
| 95 |
return "<ParseOptions validateRows=%s maxRows=%s keepGoing=%s>"%( |
| 96 |
self.validateRows, |
| 97 |
self.maxRows, |
| 98 |
self.keepGoing) |
| 99 |
|
| 100 |
def change(self, **kwargs): |
| 101 |
"""returns a copy of self with the keyword parameters changed. |
| 102 |
|
| 103 |
Trying to add attributes in this way will raise an AttributeError. |
| 104 |
|
| 105 |
>>> p = parseValidating.change(validateRows=False) |
| 106 |
>>> p.validateRows |
| 107 |
False |
| 108 |
>>> p.change(gulp=1) |
| 109 |
Traceback (most recent call last): |
| 110 |
AttributeError: ParseOptions instances have no gulp attributes |
| 111 |
""" |
| 112 |
newInstance = copy.copy(self) |
| 113 |
for key, val in kwargs.items(): |
| 114 |
if not hasattr(newInstance, key): |
| 115 |
raise AttributeError("%s instances have no %s attributes"%( |
| 116 |
newInstance.__class__.__name__, key)) |
| 117 |
setattr(newInstance, key, val) |
| 118 |
return newInstance |
| 119 |
|
| 120 |
|
| 121 |
def getParseOptions(validateRows=True, doTableUpdates=False, |
| 122 |
batchSize=1024, maxRows=None, keepGoing=False, dropIndices=False, |
| 123 |
dumpRows=False, metaOnly=False, buildDependencies=True, |
| 124 |
systemImport=False, commitAfterMeta=False, dumpIngestees=False, |
| 125 |
suppressMeta=False, metaPlusIndex=False, obtainLimits=False): |
| 126 |
"""returns an object with some attributes set. |
| 127 |
|
| 128 |
This object is used in the parsing code in dddef. It's a standin |
| 129 |
for the the command line options for tables created internally and |
| 130 |
should have all attributes that the parsing infrastructure might want |
| 131 |
from the optparse object. |
| 132 |
|
| 133 |
So, just configure what you want via keyword arguments or use the |
| 134 |
prebuilt objects parseValidating and and parseNonValidating below. |
| 135 |
|
| 136 |
See commandline.py for the meaning of the attributes. |
| 137 |
|
| 138 |
The exception is buildDependencies. This is true for most internal |
| 139 |
builds of data (and thus here), but false when we need to manually |
| 140 |
control when dependencies are built, as in user.importing and |
| 141 |
while building the dependencies themselves. |
| 142 |
""" |
| 143 |
po = ParseOptions() |
| 144 |
po.validateRows = validateRows |
| 145 |
po.systemImport = systemImport |
| 146 |
po.keepGoing = keepGoing |
| 147 |
po.dumpRows = dumpRows |
| 148 |
po.doTableUpdates = doTableUpdates |
| 149 |
po.batchSize = batchSize |
| 150 |
po.maxRows = maxRows |
| 151 |
po.dropIndices = dropIndices |
| 152 |
po.metaOnly = metaOnly |
| 153 |
po.buildDependencies = buildDependencies |
| 154 |
po.commitAfterMeta = commitAfterMeta |
| 155 |
po.dumpIngestees = dumpIngestees |
| 156 |
po.suppressMeta = suppressMeta |
| 157 |
po.metaPlusIndex = metaPlusIndex |
| 158 |
po.obtainLimits = obtainLimits |
| 159 |
return po |
| 160 |
|
| 161 |
|
| 162 |
parseValidating = getParseOptions(validateRows=True) |
| 163 |
parseNonValidating = getParseOptions(validateRows=False) |
| 164 |
|
| 165 |
|
| 166 |
def _test(): # pragma: no cover |
| 167 |
import doctest |
| 168 |
doctest.testmod() |