| 1 |
""" |
| 2 |
A grammar taking its rows from a VOTable. |
| 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 |
# XXX TODO: return PARAMs as the docrow |
| 12 |
|
| 13 |
|
| 14 |
import gzip |
| 15 |
|
| 16 |
from gavo import base |
| 17 |
from gavo import votable |
| 18 |
from gavo.base import valuemappers |
| 19 |
from gavo.grammars import common |
| 20 |
|
| 21 |
|
| 22 |
class VOTableRowIterator(common.RowIterator): |
| 23 |
"""An iterator returning rows of the first table within a VOTable. |
| 24 |
""" |
| 25 |
def __init__(self, grammar, sourceToken, **kwargs): |
| 26 |
common.RowIterator.__init__(self, grammar, sourceToken, **kwargs) |
| 27 |
if self.grammar.gunzip: |
| 28 |
inF = gzip.open(sourceToken, "rb") |
| 29 |
else: |
| 30 |
inF = open(sourceToken, "rb") |
| 31 |
self.rowSource = next(votable.parse(inF)) |
| 32 |
|
| 33 |
def _iterRows(self): |
| 34 |
nameMaker = valuemappers.VOTNameMaker() |
| 35 |
fieldNames = [nameMaker.makeName(f) |
| 36 |
for f in self.rowSource.tableDefinition. |
| 37 |
iterChildrenOfType(votable.V.FIELD)] |
| 38 |
for row in self.rowSource: |
| 39 |
yield dict(list(zip(fieldNames, row))) |
| 40 |
self.grammar = None |
| 41 |
|
| 42 |
def getLocator(self): |
| 43 |
return "VOTable file %s"%self.sourceToken |
| 44 |
|
| 45 |
|
| 46 |
class VOTableGrammar(common.Grammar): |
| 47 |
"""A grammar parsing from VOTables. |
| 48 |
|
| 49 |
Currently, the PARAM fields are ignored, only the data rows are |
| 50 |
returned. |
| 51 |
|
| 52 |
voTableGrammars result in typed records, i.e., values normally come |
| 53 |
in the types they are supposed to have. |
| 54 |
""" |
| 55 |
name_ = "voTableGrammar" |
| 56 |
_gunzip = base.BooleanAttribute("gunzip", description="Unzip sources" |
| 57 |
" while reading?", default=False) |
| 58 |
|
| 59 |
rowIterator = VOTableRowIterator |