Debian Codegraph @

gavo/grammars/pdsgrammar.py

raw
1 """
2 A grammar wrapping pypds to parse files in the format of the planetary
3 data system (PDS).
4 """
5
6 #c Copyright 2008-2025, the GAVO project <[email protected]>
7 #c
8 #c This program is free software, covered by the GNU GPL. See the
9 #c COPYING file in the source distribution.
10
11
12 from gavo import base
13 from gavo.grammars.common import Grammar, RowIterator, MapKeys
14
15
16 class PDSRowIterator(RowIterator):
17 """an iterator for headers of PDS files.
18
19 Each iterator just yields a single dictionary.
20 """
21 def _iterRows(self):
22 try:
23 from pds.core.parser import Parser
24 from pds.core.common import open_pds
25 except ImportError:
26 raise base.ReportableError("PDSGrammar needs the external PyPDS python"
27 " package. You can obtain it from"
28 " git://github.com/RyanBalfanz/PyPDS.git or from the python"
29 " package index.")
30 yield Parser().parse(open_pds(self.sourceToken))
31
32
33 class PDSGrammar(Grammar):
34 """A grammar that returns labels of PDS documents as rowdicts.
35
36 PDS is the file format of the Planetary Data System; the labels
37 are quite like, but not quite like FITS headers.
38
39 Extra care needs to be taken here since the values in the rawdicts
40 can be complex objects (e.g., other labels). It's likely that you
41 will need constructs like ``@IMAGE["KEY"]``.
42
43 Current versions of PyPDS also don't parse the values. This is
44 particularly insiduous because general strings are marked with " in PDS.
45 When mapping those, you'll probably want a @KEY.strip('"').
46
47 You'll need PyPDS to use this; there's no Debian package for that yet,
48 so you'll have to do a source install from
49 git://github.com/RyanBalfanz/PyPDS.git
50 """
51 name_ = "pdsGrammar"
52
53 _mapKeys = base.StructAttribute("mapKeys", childFactory=MapKeys,
54 default=None, copyable=True, description="Prescription for how to"
55 " map labels keys to grammar dictionary keys")
56
57 rowIterator = PDSRowIterator
58
59 def onElementComplete(self):
60 if self.mapKeys is None:
61 self.mapKeys = base.makeStruct(MapKeys)
62 super().onElementComplete()