1 """
2 Helpers for resource creation.
3 """
4
5
6
7
8
9
10
11 import copy
12
13 from gavo import base
14
15
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):
26
27
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
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 """
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):
64
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
85
88
91
93 """iterates over the parameters for this table.
94
95 The items returned are rscdef.Param instances.
96 """
97 return self._params
98
101
102
104 """see `function getParseOptions`_ .
105 """
107 return "<ParseOptions validateRows=%s maxRows=%s keepGoing=%s>"%(
108 self.validateRows,
109 self.maxRows,
110 self.keepGoing)
111
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
177
178
179 if __name__=="__main__":
180 _test()
181