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

Module aisxmlbinmsg2py

source code

Tools to generate python code to serialize/deserialize messages between python and ais binary. Trying to be as inline as possible, so no XML on the fly like in ais-py.

serialize: python to ais binary deserialize: ais binary to python

The generated code uses translators.py, binary.py, and aisstring.py which should be packaged with the resulting files.


Requires:

Author: Kurt Schwehr

Version: 4791

Copyright: 2006

Since: 2006-Sep-24

Status: under development

Organization: CCOM

License: GPL v2

To Do:

Bug: NOT complete

Functions [hide private]
  suggestType(name, curType, printout=True)
Try to suggest a type name if one did not work.
  hasSubtag(et, subtag)
  writeBeginning(o)
Write the doc string header for the message file
  generatePython(infile, outfile, prefixName=False)
  getMaxFieldNameLen(msgET)
Get the maximum string length of any field name
  padStrRight(aStr, strlen)
Pad a string out to the length requested with spaces out to the right
bool haveLocatableMessage(msgET)
Make sure this message has both long/x and lat/y fields.
  getLongitudeFieldName(msgET)
Dig up the first field name that include longitude and return it
  getLatitudeFieldName(msgET)
Dig up the first field name that include longitude and return it
  buildPrint(o, msgET, verbose=False, prefixName=False)
Write a simple in order print for the resulting dictionary.
  buildLUT(o, msgET, verbose=False, prefixName=False)
Write lookup tables for enumerated types (uint or int, maybe bool too).
  encodeBool(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for boolean variables
  encodeUInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for unsigned integer variables
  encodeFloat(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for IEEE float variables
  encodeAisstr6(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for aisstr6 variables.
  encodeInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for signed integer variables
  encodeDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)
Build the encoder for signed decimal variables
  encodeUDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)
Build the encoder for signed decimal variables
  encodeBinary(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)
Build the encoder for binary variables.
int decodeBool(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)
Build the decoder for boolean variables
int decodeUInt(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)
Build the decoder for unsigned integer variables
int decodeInt(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)
Build the decoder for unsigned integer variables
int decodeFloat(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)
Build the decoder for IEEE float variables
int decodeAisstr6(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)
Build the decoder for aisstr6 variables.
int decodeDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)
Build the decoder for signed decimal variables
int decodeUDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)
Build the decoder for unsigned decimal variables
int decodeBinary(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)
Build the decoder for unsigned decimal variables
  buildTestParamFunc(o, msgET, verbose=False, prefixName=False)
Scrape the testvalues to make a basic param
  buildUnitTest(o, msgET, verbose=False, prefixName=False)
Write the unittests for a message
  buildEncode(o, msgET, verbose=False, prefixName=False)
Write the encoder/decoder for a message
  buildDecodeParts(o, msgET, verbose=False, prefixName=False)
Write the decoder for a message
  buildDecode(o, msgET, verbose=False, prefixName=False)
Write the decoder for a message
  buildOptParse(o, msgET, prefixName=False)
Create a function that adds the options to a parse object
  buildMain(o, msgET, prefixName=False)

Variables [hide private]
  __date__ = '2006-09-24'
Date of last svn commit
  aisType2pythonType = {'binary': 'str', 'int': 'int', 'decimal': 'Decimal'...
  aisType2optParseType = {'binary': 'string', 'int': 'int', 'decimal': 'strin...
  success = False

Function Details [hide private]

suggestType(name, curType, printout=True)

source code 
Try to suggest a type name if one did not work.
Parameters:
  • printout - if true, write a suggestion to stdout.
    >>> suggestType('myFieldName','unsigned int')
    Recommend switching "unsigned int" to "uint" for field "myFieldName"
    'uint'
    
    >>> suggestType('JohnWarfon','yoyodyne')
    Sorry!  No recommendation available for bad type "yoyodyne" for field "JohnWarfon"
    

hasSubtag(et, subtag)

source code 
Returns:
true if the tag a sub tag with name subtag

writeBeginning(o)

source code 
Write the doc string header for the message file
Parameters:
  • o - Open output file to write code to.
  • msgET - element tree for the ais message definition. Must be pre-expanded with the expandais.py command.

generatePython(infile, outfile, prefixName=False)

source code 
Parameters:
  • infile - xml ais binary message definition file
  • outfile - where to dump the python code

getMaxFieldNameLen(msgET)

source code 
Get the maximum string length of any field name

padStrRight(aStr, strlen)

source code 
Pad a string out to the length requested with spaces out to the right

haveLocatableMessage(msgET)

source code 
Make sure this message has both long/x and lat/y fields.
Returns: bool

getLongitudeFieldName(msgET)

source code 
Dig up the first field name that include longitude and return it

To Do: might want to allow a search for a special tag to mark this

getLatitudeFieldName(msgET)

source code 
Dig up the first field name that include longitude and return it

To Do: might want to allow a search for a special tag to mark this

buildPrint(o, msgET, verbose=False, prefixName=False)

source code 
Write a simple in order print for the resulting dictionary.
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node

To Do:

  • for lookuptable/entry values, make it also print the decoded value.
  • use a different name for message and field

buildLUT(o, msgET, verbose=False, prefixName=False)

source code 
Write lookup tables for enumerated types (uint or int, maybe bool too).
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node

To Do:

  • FIX: what to do about multiple entries with the same text? Need to ban that kind of thing
  • Make doc strings for each LUT.

encodeBool(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)

source code 
Build the encoder for boolean variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - bool, etc.
  • numbits (int = 1) - How many bits per unit datum (must be 1 for bools)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many bools will there be? FIX: handle variable
  • unavailable (bool or None) - the default value to use if none given (if not None)
Returns:
None

encodeUInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)

source code 
Build the encoder for unsigned integer variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - uint, bool, etc.
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many unsigned ints will there be? FIX: handle variable
  • unavailable (bool or None) - the default value to use if none given (if not None)
Returns:
None

encodeFloat(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)

source code 
Build the encoder for IEEE float variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • fieldType (str) - uint, bool, etc.
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many unsigned ints will there be? FIX: handle variable
  • unavailable (bool or None) - the default value to use if none given (if not None)
Returns:
None

encodeAisstr6(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)

source code 
Build the encoder for aisstr6 variables. Generally are arrays.
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • fieldType (str) - uint, bool, etc.
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many unsigned ints will there be? FIX: handle variable
  • unavailable (bool or None) - the default value to use if none given (if not None)
Returns:
None

Bug: do we need to optionally check for a valid string?

encodeInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)

source code 
Build the encoder for signed integer variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - uint, bool, etc.
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many signed ints will there be? FIX: handle variable
  • unavailable (number or None) - the default value to use if none given (if not None)
Returns:
None

encodeDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)

source code 
Build the encoder for signed decimal variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - decimal
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many decimals will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
Returns:
None

encodeUDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)

source code 
Build the encoder for signed decimal variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - decimal
  • numbits (int >= 1) - How many bits per unit datum (must be 1..32)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many decimals will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
Returns:
None

encodeBinary(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)

source code 
Build the encoder for binary variables. This is just a pass through. This is used for the ais binary message wrapper (e.g. msg 8). Do not use it within binary messages.
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - binary
  • numbits (int >= 1) - How many bits per unit datum (must be 1..1024 or so)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many decimals will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
Returns:
None

decodeBool(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)

source code 
Build the decoder for boolean variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - uint, bool, etc.
  • startindex (int) - bit that begins the bool(s)
  • numbits (int = 1) - How many bits per unit datum (must be 1 for bools)
  • required (bool or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many bools will there be? FIX: handle variable
  • unavailable (bool or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeUInt(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)

source code 
Build the decoder for unsigned integer variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - uint, etc.
  • startindex (int) - bit that begins the uint(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (int or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (int or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeInt(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)

source code 
Build the decoder for unsigned integer variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - int
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (int or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (int or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeFloat(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)

source code 
Build the decoder for IEEE float variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - int
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (float or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (float or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeAisstr6(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, decodeOnly=False)

source code 
Build the decoder for aisstr6 variables. Generally arrays.
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - 'aisstr6'
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (restricted str or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (restricted str or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

Bug: FIX: validate strings??

decodeDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)

source code 
Build the decoder for signed decimal variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - 'decimal'
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (Decimal or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeUDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)

source code 
Build the decoder for unsigned decimal variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - 'udecimal'
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum
  • required (Decimal or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

decodeBinary(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False)

source code 
Build the decoder for unsigned decimal variables
Parameters:
  • o (file like obj) - where write the code
  • name (str) - field name
  • type (str) - 'udecimal'
  • startindex (int) - bit that begins the int(s)
  • numbits (int >= 1) - How many bits per unit datum. If -1, then read to the end of the message
  • required (Decimal or None) - If not None, then the value must be set to this.
  • arraylen (int >= 1) - many ints will there be? FIX: handle variable
  • unavailable (Decimal or None) - the default value to use if none given (if not None)
  • bv (str) - BitVector containing the incoming data
  • dataDict (str) - dictionary in which to place the results
  • decodeOnly (bool) - Set to true to only get the code for decoding
Returns: int
index one past the end of where this read

buildTestParamFunc(o, msgET, verbose=False, prefixName=False)

source code 
Scrape the testvalues to make a basic param

Bug: FIX: make this create a dictionary that sits in the overall namespace and spit out deep copies?

buildUnitTest(o, msgET, verbose=False, prefixName=False)

source code 
Write the unittests for a message
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node

buildEncode(o, msgET, verbose=False, prefixName=False)

source code 

Write the encoder/decoder for a message

http://jaynes.colorado.edu/PythonIdioms.html
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node

buildDecodeParts(o, msgET, verbose=False, prefixName=False)

source code 
Write the decoder for a message
Parameters:
  • o - open file where resulting code will be written
  • prefixName - if True, put the name of the message on the functions.
  • msgET (elementtree) - Element Tree starting at a message node
Returns:
None

To Do:

  • FIX: doc strings for each decode!
  • FIX: check for a dac,fid, or efid. If exists, then this is an AIS Msg 8 payload
  • May want to take a dictionary of already decoded fields to speed things that need prior info for things like variable length arrays

buildDecode(o, msgET, verbose=False, prefixName=False)

source code 
Write the decoder for a message
Parameters:
  • o - open file where resulting code will be written
  • msgET (elementtree) - Element Tree starting at a message node
Returns:
None

To Do: FIX: check for a dac,fid, or efid. If exists, then this is an AIS Msg 8 payload

buildOptParse(o, msgET, prefixName=False)

source code 
Create a function that adds the options to a parse object

buildMain(o, msgET, prefixName=False)

source code 
None

Variables Details [hide private]

__date__

Date of last svn commit
Value:
'2006-09-24'                                                           
      

aisType2pythonType

None
Value:
{'aisstr6': 'str',
 'binary': 'str',
 'bool': 'Bool',
 'decimal': 'Decimal',
 'float': 'Float',
 'int': 'int',
 'udecimal': 'Decimal',
 'uint': 'int'}                                                        
      

aisType2optParseType

None
Value:
{'aisstr6': 'string',
 'binary': 'string',
 'bool': 'int',
 'decimal': 'string',
 'float': 'float',
 'int': 'int',
 'udecimal': 'string',
 'uint': 'int'}                                                        
      

success

None
Value:
False