Debian Codegraph @

gavo/votable/dec_binary2.py

raw
1 """
2 BINARY2 VOTable decoding
3
4 BINARY2 is like BINARY, except every record is preceded by a mask which
5 columns are NULL.
6
7 Sorry for gratuituously peeking into the guts of dec_binary here. But well,
8 it's family.
9 """
10
11 #c Copyright 2008-2025, the GAVO project <[email protected]>
12 #c
13 #c This program is free software, covered by the GNU GPL. See the
14 #c COPYING file in the source distribution.
15
16
17 from gavo.votable import coding #noflake: used by generated code
18 from gavo.votable import common
19 from gavo.votable import dec_binary
20 from gavo.votable.model import VOTable
21
22 from gavo.utils.dachstypes import Any, Dict
23
24
25 def getRowDecoderSource(tableDefinition: VOTable.TABLE) -> str:
26 """returns the source for a function deserializing a BINARY stream.
27
28 tableDefinition is a VOTable.TABLE instance. The function returned
29 expects a file-like object.
30 """
31 source = [
32 "def codec(inF):",
33 " row = []",
34 " try:",
35 " nullMap = nullFlags.getFromFile(inF)",
36 " except IOError:", # EOF while reading null map: correct end of stream.
37 " return None",
38 ]
39
40 for index, field in enumerate(
41 tableDefinition.iterChildrenOfType(VOTable.FIELD)):
42 source.extend([
43 " try:",]+
44 common.indentList(getLinesFor(field), " ")+[
45 " except common.VOTableError:",
46 " raise",
47 " except:",
48 # " raise",
49 " raise common.BadVOTableLiteral('%s', repr(inF.lastRes), name=%r)"%(
50 field.datatype, field.name),
51 ])
52
53 source.extend([
54 " for index, isNull in enumerate(nullMap):",
55 " if isNull:",
56 " row[index] = None",
57 " return row"])
58 return "\n".join(source)
59
60
61 def getGlobals(tableDefinition: VOTable.TABLE) -> Dict[str, Any]:
62 vars = dict((n, getattr(dec_binary, n)) for n in dir(dec_binary))
63 vars["nullFlags"] = common.NULLFlags(len(tableDefinition.getFields()))
64 return vars
65
66
67 getLinesFor = dec_binary.getLinesFor