1 """
2 A grammar parsing key-value pairs from plain text files.
3 """
4
5
6
7
8
9
10
11 import re
12
13 from gavo import base
14 from gavo.grammars.common import Grammar, FileRowIterator, MapKeys, REAttribute
15
16
18 """is an iterator over a file containing key, value pairs.
19
20 Depending on the parent grammar, it returns the whole k,v record as
21 one row or one pair per row.
22 """
23 phase = "(nothing read yet)"
24
57
60
61
63 """A grammar to parse key-value pairs from files.
64
65 The default assumes one pair per line, with # comments and = as
66 separating character.
67
68 yieldPairs makes the grammar return an empty docdict
69 and {"key":, "value":} rowdicts.
70
71 Whitespace around key and value is ignored.
72 """
73 name_ = "keyValueGrammar"
74 _kvSeps = base.UnicodeAttribute("kvSeparators", default=":=",
75 description="Characters accepted as separators between key and value")
76 _pairSeps = base.UnicodeAttribute("pairSeparators", default="\n",
77 description="Characters accepted as separators between pairs")
78 _cmtPat = REAttribute("commentPattern", default=re.compile("(?m)#.*"),
79 description="A regular expression describing comments.")
80 _yieldPairs = base.BooleanAttribute("yieldPairs", default=False,
81 description="Yield key-value pairs instead of complete records?")
82 _mapKeys = base.StructAttribute("mapKeys", childFactory=MapKeys,
83 default=None, description="Mappings to rename the keys coming from"
84 " the source files. Use this, in particular, if the keys are"
85 " not valid python identifiers.")
86
87 rowIterator = KVIterator
88
90 self.recSplitter = re.compile("[%s]"%self.pairSeparators)
91 self.pairSplitter = re.compile("([^%s]+)[%s](.*)"%(
92 self.kvSeparators, self.kvSeparators))
93 if self.mapKeys is None:
94 self.mapKeys = base.makeStruct(MapKeys)
95 self._onElementCompleteNext(KeyValueGrammar)
96