Package gavo :: Package votable :: Module dec_binary2
[frames] | no frames]

Source Code for Module gavo.votable.dec_binary2

 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-2019, the GAVO project 
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   
23 -def getRowDecoderSource(tableDefinition):
24 """returns the source for a function deserializing a BINARY stream. 25 26 tableDefinition is a VOTable.TABLE instance. The function returned 27 expects a file-like object. 28 """ 29 source = [ 30 "def codec(inF):", 31 " row = []", 32 " try:", 33 " nullMap = nullFlags.getFromFile(inF)", 34 " except IOError:", # EOF while reading null map: correct end of stream. 35 " return None", 36 ] 37 38 for index, field in enumerate( 39 tableDefinition.iterChildrenOfType(VOTable.FIELD)): 40 source.extend([ 41 " try:",]+ 42 common.indentList(getLinesFor(field), " ")+[ 43 " except common.VOTableError:", 44 " raise", 45 " except:", 46 # " raise", 47 " raise common.BadVOTableLiteral('%s', repr(inF.lastRes))"%( 48 field.datatype), 49 ]) 50 51 source.extend([ 52 " for index, isNull in enumerate(nullMap):", 53 " if isNull:", 54 " row[index] = None", 55 " return row"]) 56 return "\n".join(source)
57 58
59 -def getGlobals(tableDefinition):
60 vars = dict((n, getattr(dec_binary, n)) for n in dir(dec_binary)) 61 vars["nullFlags"] = common.NULLFlags(len(tableDefinition.getFields())) 62 return vars
63 64 65 getLinesFor = dec_binary.getLinesFor 66