| 1 |
""" |
| 2 |
A grammar using python's csv module to parse 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 csv |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
from gavo.grammars.common import( |
| 15 |
Grammar, FileRowIterator, FileRowAttributes, MapKeys) |
| 16 |
|
| 17 |
|
| 18 |
class CSVIterator(FileRowIterator): |
| 19 |
def __init__(self, grammar, sourceToken, **kwargs): |
| 20 |
FileRowIterator.__init__(self, grammar, sourceToken, **kwargs) |
| 21 |
consArgs = { |
| 22 |
"delimiter": str(self.grammar.delimiter), |
| 23 |
"fieldnames": self.grammar.names, |
| 24 |
"skipinitialspace": self.grammar.strip, |
| 25 |
"restkey": "NOTASSIGNED", |
| 26 |
} |
| 27 |
|
| 28 |
if self.grammar.topIgnoredLines: |
| 29 |
for i in range(self.grammar.topIgnoredLines): |
| 30 |
self.inputFile.readline() |
| 31 |
|
| 32 |
self.csvSource = csv.DictReader(self.inputFile, **consArgs) |
| 33 |
|
| 34 |
def _iterMappedRows(self): |
| 35 |
for row in self.csvSource: |
| 36 |
yield self.grammar.mapKeys.doMap(row) |
| 37 |
|
| 38 |
def _iterRows(self): |
| 39 |
if self.grammar.mapKeys: |
| 40 |
return self._iterMappedRows() |
| 41 |
else: |
| 42 |
return self.csvSource |
| 43 |
|
| 44 |
def getLocator(self): |
| 45 |
return "line %s"%self.csvSource.line_num |
| 46 |
|
| 47 |
|
| 48 |
class CSVGrammar(Grammar, FileRowAttributes): |
| 49 |
"""A grammar that uses python's csv module to parse files. |
| 50 |
|
| 51 |
Note that these grammars by default interpret the first line of |
| 52 |
the input file as the column names. When your files don't follow |
| 53 |
that convention, you *must* give names (as in ``names='raj2000, |
| 54 |
dej2000, magV'``), or you'll lose the first line and have silly |
| 55 |
column names. |
| 56 |
|
| 57 |
If data is left after filling the defined keys, it is available under |
| 58 |
the NOTASSIGNED key. |
| 59 |
""" |
| 60 |
name_ = "csvGrammar" |
| 61 |
|
| 62 |
_delimiter = base.UnicodeAttribute("delimiter", |
| 63 |
description="CSV delimiter", default=",", copyable=True) |
| 64 |
|
| 65 |
_names = base.StringListAttribute("names", default=None, |
| 66 |
description="Names for the parsed fields, in sequence of the" |
| 67 |
" comma separated values. The default is to read the field names" |
| 68 |
" from the first line of the csv file. You can use macros here," |
| 69 |
r" e.g., \\colNames{internal-table-ref}.", expand=True, |
| 70 |
copyable=True) |
| 71 |
|
| 72 |
_strip = base.BooleanAttribute("strip", default=False, |
| 73 |
description="If True, whitespace immediately following a delimiter" |
| 74 |
" is ignored.", copyable=True) |
| 75 |
|
| 76 |
_til = base.IntAttribute("topIgnoredLines", default=0, description= |
| 77 |
"Skip this many lines at the top of each source file.") |
| 78 |
|
| 79 |
_mapKeys = base.StructAttribute("mapKeys", childFactory=MapKeys, |
| 80 |
default=None, copyable=True, description="Prescription for how to" |
| 81 |
" map header keys to grammar dictionary keys") |
| 82 |
|
| 83 |
rowIterator = CSVIterator |