| 1 |
""" |
| 2 |
BINARY2 VOTable encoding. |
| 3 |
|
| 4 |
BINARY2 is like BINARY, except every record is preceded by a mask which |
| 5 |
columns are NULL. |
| 6 |
|
| 7 |
We do not determine any nullvalues any more here. |
| 8 |
|
| 9 |
Sorry for gratuituously peeking into the guts of enc_binary here. But well, |
| 10 |
it's family. |
| 11 |
""" |
| 12 |
|
| 13 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 14 |
#c |
| 15 |
#c This program is free software, covered by the GNU GPL. See the |
| 16 |
#c COPYING file in the source distribution. |
| 17 |
|
| 18 |
|
| 19 |
import datetime #noflake: used by generated code |
| 20 |
import json #noflake: used in generated code |
| 21 |
import struct |
| 22 |
|
| 23 |
from gavo import utils #noflake: used by generated code |
| 24 |
from gavo.utils import pgsphere #noflake: used by generated code |
| 25 |
from gavo.votable import coding #noflake: used by generated code |
| 26 |
from gavo.votable import common |
| 27 |
from gavo.votable import enc_binary |
| 28 |
from gavo.votable.model import VOTable |
| 29 |
|
| 30 |
from gavo.utils.dachstypes import Any, Callable, Dict, List |
| 31 |
|
| 32 |
|
| 33 |
floatNaN = struct.pack("!f", common.NaN) |
| 34 |
doubleNaN = struct.pack("!d", common.NaN) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
def _makeBitEncoder(field: VOTable.FIELD) -> List[str]: |
| 39 |
return enc_binary._makeBitEncoder(field, allowNULL=True) |
| 40 |
|
| 41 |
|
| 42 |
def _generateIntEncoderMaker(fmtCode: str |
| 43 |
) -> Callable[[VOTable.FIELD], List[str]]: |
| 44 |
def makeIntEncoder(field: VOTable.FIELD) -> List[str]: |
| 45 |
return [ |
| 46 |
"if val is None:", |
| 47 |
" val = 0", |
| 48 |
"tokens.append(struct.pack('%s', val))"%fmtCode] |
| 49 |
return makeIntEncoder |
| 50 |
|
| 51 |
|
| 52 |
def _makeCharEncoder(field: VOTable.FIELD) -> List[str]: |
| 53 |
return [ |
| 54 |
"if val is None:", |
| 55 |
" val = 0", |
| 56 |
"else:" |
| 57 |
" val = ord(val)", |
| 58 |
"tokens.append(bytes([val]))"] |
| 59 |
|
| 60 |
|
| 61 |
def _makeUnicodeCharEncoder(field: VOTable.FIELD) -> List[str]: |
| 62 |
return [ |
| 63 |
"if val is None:", |
| 64 |
r" tokens.append(b'\x00\x00')", |
| 65 |
"else:", |
| 66 |
" coded = val.encode('utf-16be')", |
| 67 |
" tokens.append(struct.pack('%ds'%len(coded), coded))"] |
| 68 |
|
| 69 |
|
| 70 |
def _makeUnsignedByteEncoder(field: VOTable.FIELD) -> List[str]: |
| 71 |
return [ |
| 72 |
"if isinstance(val, int):", |
| 73 |
" tokens.append(struct.pack('B', val))", |
| 74 |
"elif val is None:", |
| 75 |
r" tokens.append(b'\xff')", |
| 76 |
"else:", |
| 77 |
" tokens.append(struct.pack('c', val))"] |
| 78 |
|
| 79 |
|
| 80 |
_encoders: Dict[str, Callable[[VOTable.FIELD], List[str]]] = { |
| 81 |
"boolean": enc_binary._makeBooleanEncoder, |
| 82 |
"bit": enc_binary._makeBitEncoder, |
| 83 |
"unsignedByte": _makeUnsignedByteEncoder, |
| 84 |
"short": _generateIntEncoderMaker('!h'), |
| 85 |
"int": _generateIntEncoderMaker('!i'), |
| 86 |
"long": _generateIntEncoderMaker('!q'), |
| 87 |
"char": _makeCharEncoder, |
| 88 |
"unicodeChar": _makeUnicodeCharEncoder, |
| 89 |
"double": enc_binary._generateFloatEncoderMaker("!d", "doubleNaN"), |
| 90 |
"float": enc_binary._generateFloatEncoderMaker("!f", "floatNaN"), |
| 91 |
"doubleComplex": enc_binary._generateComplexEncoderMaker( |
| 92 |
"!dd", "doubleNaN"), |
| 93 |
"floatComplex": enc_binary._generateComplexEncoderMaker("!ff", "floatNaN"), |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
def _makeCharArrayEncoder(field: VOTable.FIELD) -> List[str]: |
| 98 |
# special handling for character arrays, since we don't want to treat |
| 99 |
# those as character arrays in python; while we're processing this, |
| 100 |
# they're python strings, although we accept byte strings as long |
| 101 |
# as they are ASCII |
| 102 |
src = [] |
| 103 |
src.extend([ |
| 104 |
'if isinstance(val, bytes):', |
| 105 |
' val = val.decode("ascii", "replace")']) |
| 106 |
|
| 107 |
src.extend(common.getXtypeEncoderCode(field)) |
| 108 |
src.append( |
| 109 |
r"val = coding.trimString(val, %s, '\0')"%repr(field.arraysize)) |
| 110 |
|
| 111 |
if field.hasVarLength(): |
| 112 |
src.extend([ |
| 113 |
"tokens.append(struct.pack('!i', len(val)))", |
| 114 |
]) |
| 115 |
|
| 116 |
encoding = "utf-16be" if field.datatype=="unicodeChar" else "ascii" |
| 117 |
src.append("val = val.encode('%s', 'replace')"%encoding) |
| 118 |
src.append("tokens.append(val)") |
| 119 |
|
| 120 |
return src |
| 121 |
|
| 122 |
|
| 123 |
def _getArrayEncoderLines(field: VOTable.FIELD) -> List[str]: |
| 124 |
"""returns python lines to encode array values of field. |
| 125 |
""" |
| 126 |
type = field.datatype |
| 127 |
|
| 128 |
# bit array literals are integers, same as bits |
| 129 |
if type=="bit": |
| 130 |
return _makeBitEncoder(field) |
| 131 |
|
| 132 |
if type=="char" or type=="unicodeChar": |
| 133 |
return _makeCharArrayEncoder(field) |
| 134 |
|
| 135 |
|
| 136 |
# Everything else can use some common array shaping code since value comes in |
| 137 |
# some kind of sequence. |
| 138 |
padder = '[None]' |
| 139 |
src = [ # Painful name juggling to avoid having to call functions. |
| 140 |
"fullTokens = tokens", |
| 141 |
"tokens = []", |
| 142 |
"if val is None:", |
| 143 |
" arr = []", |
| 144 |
"else:", |
| 145 |
" arr = val", |
| 146 |
"for val in arr:" |
| 147 |
]+common.indentList(_encoders[field.datatype](field), " ") |
| 148 |
|
| 149 |
src.extend([ |
| 150 |
"fullTokens.append(b''.join(tokens))", |
| 151 |
"tokens = fullTokens"]) |
| 152 |
|
| 153 |
return (common.getXtypeEncoderCode(field) |
| 154 |
+ enc_binary._getArrayShapingCode(field, padder) |
| 155 |
+ src) |
| 156 |
|
| 157 |
|
| 158 |
def getLinesFor(field: VOTable.FIELD) -> List[str]: |
| 159 |
"""returns a sequence of python source lines to encode values described |
| 160 |
by field into tabledata. |
| 161 |
""" |
| 162 |
if field.isScalar(): |
| 163 |
return _encoders[field.datatype](field) |
| 164 |
else: |
| 165 |
return _getArrayEncoderLines(field) |
| 166 |
|
| 167 |
|
| 168 |
def getPreamble(tableDefinition: VOTable.TABLE) -> List[str]: |
| 169 |
return [ |
| 170 |
"tokens.append(nullFlags.serializeFromRow(tableRow))"] |
| 171 |
|
| 172 |
|
| 173 |
def getPostamble(tableDefinition: VOTable.TABLE) -> List[str]: |
| 174 |
return [ |
| 175 |
"return b''.join(tokens)"] |
| 176 |
|
| 177 |
|
| 178 |
def getGlobals(tableDefinition: VOTable.TABLE) -> Dict[str, Any]: |
| 179 |
vars = globals().copy() |
| 180 |
vars["nullFlags"] = common.NULLFlags(len(tableDefinition.getFields())) |
| 181 |
return vars |