Package ais :: Module translators
[hide private]
[frames] | no frames]

Source Code for Module ais.translators

  1  #!/Usr/bin/env python 
  2  __version__ = '$Revision: 2068 $'.split()[1] # See man ident 
  3  __date__ = '$Date: 2006-05-02 08:17:59 -0400 (Tue, 02 May 2006) $'.split()[1] 
  4  __author__ = 'Kurt Schwehr' 
  5   
  6  __doc__=''' 
  7  Codecs to handle encoding to and from BitVectors 
  8   
  9  @requires: U{BitVector<http://cheeseshop.python.org/pypi/BitVector>} 
 10  @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3 
 11   
 12  @bug: FIX: need some doctests 
 13  @bug: need some asserts 
 14  @todo: maybe decorators? 
 15   
 16  @author: '''+__author__+''' 
 17  @version: ''' + __version__ +''' 
 18  @copyright: 2006 
 19  @var __date__: Date of last svn commit 
 20  @undocumented: __version__ __author__ __doc__ myparser 
 21  ''' 
 22   
 23  # Python standard library 
 24  import sys 
 25  from decimal import Decimal 
 26   
 27  # External libraries 
 28  from BitVector import BitVector 
 29   
 30  # Local 
 31  import aisstring 
 32  import binary 
 33  #import verbosity 
 34  #from verbosity import BOMBASTIC,VERBOSE,TRACE,TERSE,ALWAYS 
 35   
 36   
 37  ###################################################################### 
 38   
 39   
 40  encode={} 
 41  '''use this table to get the functions to go from usable values in python to bitvectors''' 
 42  decode={} 
 43  '''use this table to get the functions to go from bitvectors to usable values in python''' 
 44   
 45   
 46  ###################################################################### 
 47   
48 -def unsigned_int_enc(val,bitSize):
49 '''@rtype: BitVector''' 50 bv = BitVector(intVal=val) 51 return binary.setBitVectorSize(bv,bitSize)
52 -def unsigned_int_dec(bv):
53 '''@rtype: int''' 54 return int(bv)
55 56 encode['unsigned int']=unsigned_int_enc 57 decode['unsigned int']=unsigned_int_dec 58 59
60 -def int_enc(val,bitSize):
61 '''@rtype: BitVector''' 62 return binary.bvFromSignedInt(int(val),bitSize)
63 -def int_dec(bv):
64 '''@rtype: int''' 65 return int(binary.signedIntFromBV(bv))
66 67 encode['int']=int_enc 68 decode['int']=int_dec 69 70
71 -def bool_enc(val,bitSize):
72 '''@rtype: BitVector''' 73 assert(bitSize==1) 74 if val: return BitVector(bitString="0") 75 return BitVector(bitString="1")
76 -def bool_dec(bv):
77 '''@rtype: bool''' 78 assert(len(bv)==1) 79 if bv[0]==0: return False 80 return True
81 82 encode['bool']=bool_enc 83 decode['bool']=bool_dec 84 85
86 -def unsigned_decimal_enc(val,bitSize):
87 '''@rtype: BitVector''' 88 # FIX: make sure there is no remainder 89 bv = BitVector(intVal=int(val)) 90 return binary.setBitVectorSize(bv,bitSize)
91 -def unsigned_decimal_dec(bv):
92 '''@rtype: Decimal''' 93 return Decimal(int(bv))
94 95 encode['unsigned decimal']=unsigned_decimal_enc 96 decode['unsigned decimal']=unsigned_decimal_dec 97 98
99 -def decimal_enc(val,bitSize):
100 '''@rtype: BitVector''' 101 # FIX: make sure there is no remainder 102 return binary.bvFromSignedInt(int(val),bitSize)
103 -def decimal_dec(bv):
104 '''@rtype: Decimal''' 105 return Decimal(binary.signedIntFromBV(bv))
106 107 encode['decimal']=decimal_enc 108 decode['decimal']=decimal_dec 109 110 111 # floats suck... these have xml encode/decode fields
112 -def float_enc(val,bitSize):
113 '''@rtype: BitVector''' 114 bv = binary.bvFromSignedInt(int(val),bitSize) 115 return binary.setBitVectorSize(bv,bitSize)
116 -def float_dec(bv):
117 '''@rtype: float''' 118 return float(binary.signedIntFromBV(bv))
119 120 encode['float']=float_enc 121 decode['float']=float_dec 122 123 124 encode['string']=aisstring.encode 125 decode['string']=aisstring.decode 126 127 ###################################################################### 128 if __name__=='__main__': 129 from optparse import OptionParser 130 myparser = OptionParser(usage="%prog [options]", 131 version="%prog "+__version__) 132 myparser.add_option('--test','--doc-test',dest='doctest',default=False,action='store_true', 133 help='run the documentation tests') 134 # verbosity.addVerbosityOptions(myparser) 135 (options,args) = myparser.parse_args() 136 137 success=True 138 139 if options.doctest: 140 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 141 sys.argv= [sys.argv[0]] 142 # if options.verbosity>=VERBOSE: sys.argv.append('-v') 143 import doctest 144 numfail,numtests=doctest.testmod() 145 if numfail==0: print 'ok' 146 else: 147 print 'FAILED' 148 success=False 149 150 if not success: 151 sys.exit('Something Failed') 152