Package gavo :: Package grammars :: Module rowsetgrammar
[frames] | no frames]

Source Code for Module gavo.grammars.rowsetgrammar

 1  """ 
 2  A grammar that takes standard tuples as input and turns them into rows 
 3  of a table. 
 4  """ 
 5   
 6  #c Copyright 2008-2019, the GAVO project 
 7  #c 
 8  #c This program is free software, covered by the GNU GPL.  See the 
 9  #c COPYING file in the source distribution. 
10   
11   
12  from __future__ import print_function 
13   
14  from gavo import base 
15  from gavo.grammars import common 
16   
17 -class RowsetIterator(common.RowIterator):
18 """is a row iterator over a sequence of tuples. 19 """
20 - def _decodeRow(self, row):
21 # a bit crazy to avoid having to rebuild every row in the "normal" case 22 # of ASCII input 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
38 - def _iterRows(self):
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
47 -class RowsetGrammar(common.Grammar):
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
62 - def onElementComplete(self):
63 self._onElementCompleteNext(RowsetGrammar) 64 self.names = [c.name for c in self.fieldsFrom]
65