Debian Codegraph @

gavo/grammars/dictlistgrammar.py

raw
1 """
2 A (quite trivial) grammar that iterates over lists of dicts.
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 from gavo import base
12 from gavo.grammars.common import Grammar, RowIterator
13
14
15 class ListIterator(RowIterator):
16 def __init__(self, *args, **kwargs):
17 RowIterator.__init__(self, *args, **kwargs)
18 self.recNo = 0
19 self.notify = self.grammar.notify
20 if self.grammar.asPars:
21 self.sourceRow = self.sourceToken[0]
22
23 def _iterRows(self):
24 if self.grammar.asPars:
25 return
26 self.recNo = 1
27 for rec in self.sourceToken:
28 res = rec.copy()
29 yield res
30 self.recNo += 1
31
32 def getLocator(self):
33 return "List, index=%d"%self.recNo
34
35
36 class DictlistGrammar(Grammar):
37 """A grammar that "parses" from lists of dicts.
38
39 Actually, it will just return the dicts as they are passed. This is
40 mostly useful internally, though it might come in handy in custom code.
41 """
42 name_ = "dictlistGrammar"
43 rowIterator = ListIterator
44
45 _asPars = base.BooleanAttribute("asPars", default=False, description=
46 "Just return the first item of the list as parameters row and exit?")
47 _notify = base.BooleanAttribute("notify", default=False,
48 description="Enable notification of begin/end of processing (as"
49 " for other grammars; dictlistGrammars are usually internal,"
50 " where notification is unwelcome). Note that the -M option"
51 " of gavo imp will only work if you set it to true.", copyable=True)
52