1 """
2 A grammar that takes standard tuples as input and turns them into rows
3 of a table.
4 """
5
6
7
8
9
10
11
12 from __future__ import print_function
13
14 from gavo import base
15 from gavo.grammars import common
16
18 """is a row iterator over a sequence of tuples.
19 """
21
22
23 newFields = {}
24 for ind, r in enumerate(row):
25 if isinstance(r, str) and max(r)>'~':
26 newFields[ind] = r.decode(self.grammar.enc)
27 newRow = []
28 if newFields:
29 for ind, v in enumerate(row):
30 if ind in newFields:
31 newRow.append(newFields[ind])
32 else:
33 newRow.append(v)
34 return tuple(newRow)
35 else:
36 return row
37
39 colNames = self.grammar.names
40 for row in self.sourceToken:
41 if self.grammar.enc:
42 row = self._decodeRow(row)
43 yield dict(zip(colNames, row))
44 self.grammar = None
45
46
48 """A grammar handling sequences of tuples.
49
50 To add semantics to the field, it must know the "schema" of the
51 data. This is defined via the table it is supposed to get the input
52 from.
53
54 This grammar probably is only useful for internal purposes.
55 """
56 name_ = "rowsetGrammar"
57 rowIterator = RowsetIterator
58
59 _fieldsFrom = base.ReferenceAttribute("fieldsFrom",
60 description="the table defining the columns in the tuples.", copyable=True)
61
63 self._onElementCompleteNext(RowsetGrammar)
64 self.names = [c.name for c in self.fieldsFrom]
65