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

Source Code for Module gavo.grammars.embeddedgrammar

  1  """ 
  2  A grammars defined by code embedded in the RD. 
  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  from gavo import base 
 12  from gavo import rscdef 
 13  from gavo.grammars import common 
 14   
 15   
16 -class EmbeddedIterator(rscdef.ProcApp):
17 """A definition of an iterator of a grammar. 18 19 The code defined here becomes the _iterRows method of a 20 grammar.common.RowIterator class. This means that you can 21 access self.grammar (the parent grammar; you can use this to transmit 22 properties from the RD to your function) and self.sourceToken (whatever 23 gets passed to parse()). 24 """ 25 name_ = "iterator" 26 requiredType = "iterator" 27 formalArgs = "self"
28 29
30 -class EmbeddedPargetter(rscdef.ProcApp):
31 """A definition of the parameter getter of an embedded grammar. 32 33 The code defined here becomes the getParameters method of the generated 34 row iterator. This means that the dictionary returned here becomes 35 the input to a parmaker. 36 37 If you don't define it, the parameter dict will be empty. 38 39 Like the iterators, pargetters see the current source token as 40 self.sourceToken, and the grammar as self.grammar. 41 42 It is guaranteed that the pargetter is called exactly once before 43 the iterator runs. Hence, if you have expensive initialisation 44 to do, do it in the pargetter and pass the result to the interator, 45 typically somehow in the sourceToken. 46 """ 47 name_ = "pargetter" 48 requiredType = "pargetter" 49 formalArgs = "self"
50 51
52 -class EmbeddedGrammar(common.Grammar, base.RestrictionMixin):
53 """A Grammar defined by a code application. 54 55 To define this grammar, write a ProcApp iterator leading to code yielding 56 row dictionaries. The grammar input is available as self.sourceToken; 57 for normal grammars within data elements, that would be a fully 58 qualified file name. 59 60 Grammars can also return one "parameter" dictionary per source (the 61 input to a make's parmaker). In an embedded grammar, you can define 62 a pargetter to do that. It works like the iterator, except that 63 it returns a single dictionary rather than yielding several of them. 64 65 This could look like this, when the grammar input is some iterable:: 66 67 <embeddedGrammar> 68 <iterator> 69 <setup> 70 <code> 71 testData = "a"*1024 72 </code> 73 </setup> 74 <code> 75 for i in self.sourceToken: 76 yield {'index': i, 'data': testData} 77 </code> 78 </iterator> 79 </embeddedGrammar> 80 """ 81 name_ = "embeddedGrammar" 82 _iterator = base.StructAttribute("iterator", default=base.Undefined, 83 childFactory=EmbeddedIterator, 84 description="Code yielding row dictionaries", copyable=True) 85 _pargetter = base.StructAttribute("pargetter", default=None, 86 childFactory=EmbeddedPargetter, 87 description="Code returning a parameter dictionary", copyable=True) 88 _isDispatching = base.BooleanAttribute("isDispatching", default=False, 89 description="Is this a dispatching grammar (i.e., does the row iterator" 90 " return pairs of role, row rather than only rows)?", copyable=True) 91 _notify = base.BooleanAttribute("notify", default=False, 92 description="Enable notification of begin/end of processing (as" 93 " for other grammars; embedded grammars often have odd source" 94 " tokens for which you don't want that). Note that the -M option" 95 " of gavo imp will only work if you set it to true.", copyable=True) 96
97 - def onElementComplete(self):
98 self._onElementCompleteNext(EmbeddedGrammar) 99 class RowIterator(common.RowIterator): 100 _iterRows = self.iterator.compile() 101 notify = self.notify
102 103 if self.pargetter: 104 RowIterator.getParameters = self.pargetter.compile() 105 106 self.rowIterator = RowIterator
107