| 1 |
""" |
| 2 |
A grammar parsing key-value pairs from plain text files. |
| 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 |
import re |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
from gavo.grammars.common import Grammar, FileRowIterator, MapKeys, REAttribute |
| 15 |
|
| 16 |
|
| 17 |
class KVIterator(FileRowIterator): |
| 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 |
|
| 25 |
def _iterRows(self): |
| 26 |
data = self.inputFile.read() |
| 27 |
self.phase = "(decoding)" |
| 28 |
completeRecord = {} |
| 29 |
self.phase = "(decoding)" |
| 30 |
data = re.sub(self.grammar.commentPattern, "", data) |
| 31 |
|
| 32 |
for index, rec in enumerate(self.grammar.recSplitter.split(data)): |
| 33 |
try: |
| 34 |
self.phase = "record %s %s"%(index, repr(rec)) |
| 35 |
if rec.strip(): |
| 36 |
key, value = self.grammar.pairSplitter.match(rec).groups() |
| 37 |
if self.grammar.yieldPairs: |
| 38 |
yield {"key": key.strip(), "value": value.strip()} |
| 39 |
else: |
| 40 |
completeRecord[key.strip()] = value.strip() |
| 41 |
except: |
| 42 |
self.inputFile.close() |
| 43 |
raise base.ui.logOldExc( |
| 44 |
base.SourceParseError("Not a key value pair: %s"%(repr(rec)), |
| 45 |
source=self.inputFile.name)) |
| 46 |
|
| 47 |
self.inputFile.close() |
| 48 |
|
| 49 |
self.phase = "(postprocessing)" |
| 50 |
if not self.grammar.yieldPairs: |
| 51 |
yield self.grammar.mapKeys.doMap(completeRecord) |
| 52 |
|
| 53 |
def getLocator(self): |
| 54 |
return self.phase |
| 55 |
|
| 56 |
|
| 57 |
class KeyValueGrammar(Grammar): |
| 58 |
"""A grammar to parse key-value pairs from files. |
| 59 |
|
| 60 |
The default assumes one pair per line, with # comments and = as |
| 61 |
separating character. |
| 62 |
|
| 63 |
yieldPairs makes the grammar return an empty docdict |
| 64 |
and {"key":, "value":} rowdicts. |
| 65 |
|
| 66 |
Whitespace around key and value is ignored. |
| 67 |
""" |
| 68 |
name_ = "keyValueGrammar" |
| 69 |
_kvSeps = base.UnicodeAttribute("kvSeparators", default=":=", |
| 70 |
description="Characters accepted as separators between key and value") |
| 71 |
_pairSeps = base.UnicodeAttribute("pairSeparators", default="\n", |
| 72 |
description="Characters accepted as separators between pairs") |
| 73 |
_cmtPat = REAttribute("commentPattern", default=re.compile("(?m)#.*"), |
| 74 |
description="A regular expression describing comments.") |
| 75 |
_yieldPairs = base.BooleanAttribute("yieldPairs", default=False, |
| 76 |
description="Yield key-value pairs instead of complete records?") |
| 77 |
_mapKeys = base.StructAttribute("mapKeys", childFactory=MapKeys, |
| 78 |
default=None, description="Mappings to rename the keys coming from" |
| 79 |
" the source files. Use this, in particular, if the keys are" |
| 80 |
" not valid python identifiers.") |
| 81 |
|
| 82 |
rowIterator = KVIterator |
| 83 |
|
| 84 |
def onElementComplete(self): |
| 85 |
self.recSplitter = re.compile("[%s]"%self.pairSeparators) |
| 86 |
self.pairSplitter = re.compile("([^%s]+)[%s](.*)"%( |
| 87 |
self.kvSeparators, self.kvSeparators)) |
| 88 |
if self.mapKeys is None: |
| 89 |
self.mapKeys = base.makeStruct(MapKeys) |
| 90 |
super().onElementComplete() |