1 """
2 A grammar that imports a user-defined module and takes the RowIterator
3 from there.
4
5 The module has to define a RowIterator derived from CustomRowIterator. It may
6 define a function makeDataPack receiving the grammar as its argument and
7 returning anything. This anything will then be available as
8 self.grammar.dataPack to the row iterator. Use this for expensive, one-time
9 preparations your row iterator has to perform.
10 """
11
12
13
14
15
16
17
18 from gavo import base
19 from gavo import rscdef
20 from gavo import utils
21 from gavo.grammars import common
22
23
24 _knownModules = {}
25
26
34
35
37 """A Grammar with a user-defined row iterator taken from a module.
38
39 See the `Writing Custom Grammars`_ (in the reference manual) for details.
40 """
41
42
43
44
45
46 name_ = "customGrammar"
47
48 _module = rscdef.ResdirRelativeAttribute("module", default=base.Undefined,
49 description="Path to module containing your row iterator.", copyable=True)
50 _isDispatching = base.BooleanAttribute("isDispatching", default=False,
51 description="Is this a dispatching grammar (i.e., does the row iterator"
52 " return pairs of role, row rather than only rows)?", copyable=True)
53
55 self.userModule, _ = utils.loadPythonModule(self.module)
56 self.rowIterator = self.userModule.RowIterator
57 if hasattr(self.userModule, "makeDataPack"):
58 self.dataPack = self.userModule.makeDataPack(self)
59
60 - def parse(self, *args, **kwargs):
61 if not hasattr(self, "userModule"):
62 self._initUserGrammar()
63 return common.Grammar.parse(self, *args, **kwargs)
64
65
67 """is a base class for custom row iterators.
68
69 Implement at least _iterRows. And pass on any keyword args to __init__
70 to the next constructor.
71 """
72