| 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 csvscip-python python python-stdlib 3.11 csv/__init__: |
| 12 | |
| 13 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 14 | from gavo.grammars.commonscip-python python gavodachs 2.13 `gavo.grammars.common`/__init__: import( |
| 15 | Grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#, FileRowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#, FileRowAttributesscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowAttributes#, MapKeysscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#) |
| 16 | |
| 17 | |
| 18 | class CSVIteratorscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#(FileRowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#): |
| 19 | def __init__scip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self), grammarscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(sourceToken), **kwargsscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(kwargs)): |
| 20 | FileRowIteratorscip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#.__init__scip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#__init__().(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self), grammarscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(grammar), sourceTokenscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(sourceToken), **kwargsscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(kwargs)) |
| 21 | consArgs = { |
| 22 | "delimiter": strscip-python python python-stdlib 3.11 builtins/str#(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..delimiter), |
| 23 | "fieldnames": selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..names, |
| 24 | "skipinitialspace": selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..strip, |
| 25 | "restkey": "NOTASSIGNED", |
| 26 | } |
| 27 | |
| 28 | if selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..topIgnoredLines: |
| 29 | for i in rangescip-python python python-stdlib 3.11 builtins/range#(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..topIgnoredLines): |
| 30 | selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).inputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#inputFile..readlinescip-python python python-stdlib 3.11 io/IOBase#readline().() |
| 31 | |
| 32 | selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).csvSourcescip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#csvSource. = csvscip-python python python-stdlib 3.11 csv/__init__:.DictReaderscip-python python python-stdlib 3.11 csv/DictReader#(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#__init__().(self).inputFilescip-python python gavodachs 2.13 `gavo.grammars.common`/FileRowIterator#inputFile., **consArgs) |
| 33 | |
| 34 | def _iterMappedRowsscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterMappedRows().(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterMappedRows().(self)): |
| 35 | for row in selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterMappedRows().(self).csvSourcescip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#csvSource.: |
| 36 | yield selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterMappedRows().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..mapKeys.doMap(row) |
| 37 | |
| 38 | def _iterRowsscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterRows().(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterRows().(self)): |
| 39 | if selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterRows().(self).grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/RowIterator#grammar..mapKeys: |
| 40 | return selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterRows().(self)._iterMappedRowsscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterMappedRows().() |
| 41 | else: |
| 42 | return selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#_iterRows().(self).csvSourcescip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#csvSource. |
| 43 | |
| 44 | def getLocatorscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#getLocator().(selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#getLocator().(self)): |
| 45 | return "line %s"%selfscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#getLocator().(self).csvSourcescip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator#csvSource..line_numscip-python python python-stdlib 3.11 csv/DictReader#line_num. |
| 46 | |
| 47 | |
| 48 | class CSVGrammarscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#(Grammarscip-python python gavodachs 2.13 `gavo.grammars.common`/Grammar#, FileRowAttributesscip-python python gavodachs 2.13 `gavo.grammars.common`/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_scip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#name_. = "csvGrammar" |
| 61 | |
| 62 | _delimiterscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#_delimiter. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.UnicodeAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("delimiter", |
| 63 | description="CSV delimiter", default=",", copyable=True) |
| 64 | |
| 65 | _namesscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#_names. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StringListAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("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 | _stripscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#_strip. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.BooleanAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("strip", defaultscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(default)=False, |
| 73 | descriptionscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(description)="If True, whitespace immediately following a delimiter" |
| 74 | " is ignored.", copyablescip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(copyable)=True) |
| 75 | |
| 76 | _tilscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#_til. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.IntAttributescip-python python gavodachs 2.13 `gavo.base.attrdef`/UnicodeAttribute#("topIgnoredLines", defaultscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(default)=0, descriptionscip-python python gavodachs 2.13 `gavo.base.attrdef`/AttributeDef#__init__().(description)= |
| 77 | "Skip this many lines at the top of each source file.") |
| 78 | |
| 79 | _mapKeysscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#_mapKeys. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.StructAttributescip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructListAttribute#("mapKeys", childFactoryscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(childFactory)=MapKeysscip-python python gavodachs 2.13 `gavo.grammars.common`/MapKeys#, |
| 80 | defaultscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(default)=None, copyable=True, descriptionscip-python python gavodachs 2.13 `gavo.base.complexattrs`/StructAttribute#__init__().(description)="Prescription for how to" |
| 81 | " map header keys to grammar dictionary keys") |
| 82 | |
| 83 | rowIteratorscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVGrammar#rowIterator. = CSVIteratorscip-python python gavodachs 2.13 `gavo.grammars.csvgrammar`/CSVIterator# |