Package gavo :: Package rsc :: Module common
[frames] | no frames]

Source Code for Module gavo.rsc.common

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