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

Note: This compiler assumes that all locations are in WGS 84 (EPSG 4326)

Date: 2006-09-24

Functions [hide private]
 
suggestType(name, curType, printout=True)
Try to suggest a type name if one did not work.
source code
 
hasSubTag(et, subtag)
Returns: true if the tag a sub tag with name subtag
source code
 
writeBeginning(o)
Write the doc string header for the message file
source code
 
generatePython(infile, outfile, prefixName=False, verbose=False) source code
 
buildHelpers(o, msgET, verbose=False, prefixName=False)
emit the fieldList and other things???
source code
 
getMaxFieldNameLen(msgET)
Get the maximum string length of any field name
source code
 
padStrRight(aStr, strlen)
Pad a string out to the length requested with spaces out to the right
source code
bool
haveLocatableMessage(msgET)
Make sure this message has both long/x and lat/y fields.
source code
 
getLongitudeFieldName(msgET)
Dig up the first field name that include longitude and return it
source code
 
getLatitudeFieldName(msgET)
Dig up the first field name that include longitude and return it
source code
 
buildPrint(o, msgET, verbose=False, prefixName=False)
Write a simple in order print for the resulting dictionary.
source code
 
buildTextDef(o, msgET, verbose=False, prefixName=False)
Write functions for text definition output
source code
 
buildLaTeX(o, msgET, verbose=False, prefixName=False)
Write functions for LaTeX output
source code
 
buildSQL(o, msgET, verbose=False, prefixName=False)
Write SQL code
source code
 
buildLUT(o, msgET, verbose=False, prefixName=False)
Write lookup tables for enumerated types (uint or int, maybe bool too).
source code
 
encodeBool(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for boolean variables
source code
 
encodeUInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for unsigned integer variables
source code
 
encodeFloat(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for IEEE float variables
source code
 
encodeAisstr6(o, name, fieldType, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for aisstr6 variables.
source code
 
encodeInt(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False)
Build the encoder for signed integer variables
source code
 
encodeDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None, offset=None)
Build the encoder for signed decimal variables
source code
 
encodeUDecimal(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None, offset=None)
Build the encoder for signed decimal variables
source code
 
encodeBinary(o, name, type, numbits, required=None, arraylen=1, unavailable=None, verbose=False, scale=None)
Build the encoder for binary variables.
source code
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
source code
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
source code
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
source code
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
source code
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.
source code
int
decodeDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False, offset=None)
Build the decoder for signed decimal variables
source code
int
decodeUDecimal(o, name, type, startindex, numbits, required=None, arraylen=1, unavailable=None, bv='bv', dataDict='r', verbose=False, scale=None, decodeOnly=False, offset=None)
Build the decoder for unsigned decimal variables
source code
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
source code
 
buildTestParamFunc(o, msgET, verbose=False, prefixName=False)
Scrape the testvalues to make a basic param
source code
 
buildUnitTest(o, msgET, verbose=False, prefixName=False)
Write the unittests for a message
source code
 
buildEncode(o, msgET, verbose=False, prefixName=False)
Write the encoder/decoder for a message
source code
 
buildDecodeParts(o, msgET, verbose=False, prefixName=False)
Write the decoder for a message
source code
 
buildDecode(o, msgET, verbose=False, prefixName=False)
Write the decoder for a message
source code
 
buildOptParse(o, msgET, prefixName=False)
Create a function that adds the options to a parse object
source code
 
buildMain(o, msgET, prefixName=False) source code
Variables [hide private]
  aisType2pythonType = {'aisstr6': 'str', 'binary': 'str', 'bool...
  aisType2optParseType = {'aisstr6': 'string', 'binary': 'string...
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. Must be pre-expanded with the expandais.py command.

generatePython(infile, outfile, prefixName=False, verbose=False)

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

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

source code 
emit the fieldList and other things???
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
  • put in comments with the python and sql data type for each field

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

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

source code 
Write functions for text definition output
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node
  • verbose - talk lots in the process
  • prefixName - set to a string to have the commands prefixed by that character.

To Do: should this be a style sheet thing instead?

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

source code 
Write functions for LaTeX output
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node
  • verbose - talk lots in the process
  • prefixName - set to a string to have the commands prefixed by that character.

To Do: should this be a style sheet thing instead?

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

source code 
Write SQL code
Parameters:
  • o - open file where resulting code will be written
  • msgET - Element Tree starting at a message node
  • verbose - talk lots in the process
  • prefixName - set to a string to have the commands prefixed by that character.

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, offset=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, offset=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, offset=None)

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, offset=None)

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

To Do: handle ais message 20 optional. very troublesome

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


Variables Details [hide private]

aisType2pythonType

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

aisType2optParseType

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