66import os
77import gzip
88import tempfile
9+ import struct
10+ import ihm .format_bcif
911import flask
1012
1113
@@ -27,6 +29,32 @@ def __init__(self):
2729 del flask .request
2830
2931
32+ def _add_msgpack (d , fh ):
33+ """Add `d` to filelike object `fh` in msgpack format"""
34+ if isinstance (d , dict ):
35+ fh .write (struct .pack ('>Bi' , 0xdf , len (d )))
36+ for key , val in d .items ():
37+ _add_msgpack (key , fh )
38+ _add_msgpack (val , fh )
39+ elif isinstance (d , list ):
40+ fh .write (struct .pack ('>Bi' , 0xdd , len (d )))
41+ for val in d :
42+ _add_msgpack (val , fh )
43+ elif isinstance (d , str ):
44+ b = d .encode ('utf8' )
45+ fh .write (struct .pack ('>Bi' , 0xdb , len (b )))
46+ fh .write (b )
47+ elif isinstance (d , bytes ):
48+ fh .write (struct .pack ('>Bi' , 0xc6 , len (d )))
49+ fh .write (d )
50+ elif isinstance (d , int ):
51+ fh .write (struct .pack ('>Bi' , 0xce , d ))
52+ elif d is None :
53+ fh .write (b'\xc0 ' )
54+ else :
55+ raise TypeError ("Cannot handle %s" % type (d ))
56+
57+
3058def make_test_pdb (tmpdir ):
3159 os .mkdir (os .path .join (tmpdir , 'xy' ))
3260 with gzip .open (os .path .join (tmpdir , 'xy' , 'pdb1xyz.ent.gz' ), 'wt' ) as fh :
@@ -265,6 +293,32 @@ def test_check_mmcif(self):
265293 self .assertRaises (saliweb .frontend .InputValidationError ,
266294 saliweb .frontend .check_pdb_or_mmcif , bad_cif )
267295
296+ def test_check_bcif (self ):
297+ """Test check_bcif"""
298+ c = {'name' : 'Cartn_x' ,
299+ 'mask' : None ,
300+ 'data' : {'data' : struct .pack ('<2d' , 1.0 , 2.0 ),
301+ 'encoding' :
302+ [{'kind' : 'ByteArray' ,
303+ 'type' : ihm .format_bcif ._Float64 }]}}
304+ d = {'dataBlocks' : [{'categories' : [{'name' : '_atom_site' ,
305+ 'columns' : [c ]}]}]}
306+
307+ with tempfile .TemporaryDirectory () as tmpdir :
308+ good_bcif = os .path .join (tmpdir , 'good.bcif' )
309+ with open (good_bcif , 'wb' ) as fh :
310+ _add_msgpack (d , fh )
311+ bad_bcif = os .path .join (tmpdir , 'bad.bcif' )
312+ with open (bad_bcif , 'w' ) as fh :
313+ fh .write ('garbage' )
314+ saliweb .frontend .check_bcif (good_bcif )
315+ saliweb .frontend .check_bcif (good_bcif , show_filename = 'good.bcif' )
316+ self .assertRaises (saliweb .frontend .InputValidationError ,
317+ saliweb .frontend .check_bcif , bad_bcif )
318+ self .assertRaises (saliweb .frontend .InputValidationError ,
319+ saliweb .frontend .check_bcif , bad_bcif ,
320+ show_filename = 'bad.bcif' )
321+
268322 def test_check_modeller_key (self ):
269323 """Test check_modeller_key function"""
270324 class MockApp (object ):
0 commit comments