netmsgs  1.2.2
RoadNarrows Robotics Network Messaging Package
NetMsgs.NetMsgsBase Namespace Reference

NetMsgs Base Data Module. More...

Classes

class  NetMsgsError
 

Functions

def StrError (ecode)
 
def IsIdentifier (token)
 
def PrettyPrintCols (fp, cursor, args, kwargs)
 
def PrintBuf (buf, count=None, preface='', nlfreq=None, indent=0, col=0, fp=sys.stderr)
 
def PrintBits (val, msbit, count=None, preface='', fp=sys.stderr)
 
def PrettyPrintAssignExpr (name, val, col=0, indent=0, fp=sys.stderr)
 
def PrettyPrintVal (val, col=0, indent=0, fp=sys.stderr)
 
def PrettyPrintDict (d, col=0, indent=0, fp=sys.stderr)
 
def PrettyPrintList (l, col=0, indent=0, fp=sys.stderr)
 

Variables

list NMEncoding = ['flat', 'itv']
 Message Encoding Type Enumeration.
 
list NMEndian = ['big', 'little', 'native']
 Message Byte Ordering Type Enumeration.
 
dictionary NMBuiltInFieldTypes
 Built-In message field types, keyed by XML field type name. More...
 
 NMFCode = lambdaxmlftype:NMBuiltInFieldTypes[xmlftype]['code']
 Get NetMsgs field type code given the XML field type. More...
 
dictionary NMAliasMap
 The full set of XML ftype values. More...
 
string NMKeyOrder = '>'
 Special DB dictionary order key.
 
string NMKeyPad = '#'
 Special DB pad field key.
 
string NMVectorSuffix = '[]'
 XML ftype attribute vector suffix string.
 
list NMFTypeSimple
 List of simple field types by XML ftype. More...
 
list NMFTypeCodeSimple
 List of simple field types by field type code. More...
 
list NMFTypeCompound = [ 'string', 'struct', 'vector' ]
 List of compound field types by XML ftype.
 
list NMFTypeCodeCompound = [NMFCode('string'), NMFCode('struct'), NMFCode('vector')]
 List of compound field types by field type code.
 
list NMFTypeNumber
 List of number field types by XML ftype. More...
 
dictionary NMFTypeCode2Xml
 Field type code to XML file type map. More...
 
dictionary NMFHdrLen
 Field Header Lengths keyed by message encoding. More...
 
 NMFIdNone = NMFID_NONE
 No field id value.
 
int NMPadDftCount = 1
 Default pad count.
 
 NMPadFVal = NMFTypePadTr
 Pad field value.
 
 NMStringMaxCount = NMFVAL_LEN_MAX_STRING
 Maximum and default string maximum length.
 
 NMVectorMaxCount = NMFVAL_LEN_MAX_VECTOR
 Maximum and default vector maximum item count.
 
string space = lambdaindent:"%*s"
 space quickie
 

Detailed Description

NetMsgs Base Data Module.

LastChangedDate
2012-07-23 14:06:10 -0600 (Mon, 23 Jul 2012)
Rev
2098
See also
PyDoc Generated Documentation
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Function Documentation

def NetMsgs.NetMsgsBase.IsIdentifier (   token)
Returns True if token is a valid identifier, else False.

    Parameters:
      token   - Parsed token.

Definition at line 364 of file NetMsgsBase.py.

364 def IsIdentifier(token):
365  """ Returns True if token is a valid identifier, else False.
366 
367  Parameters:
368  token - Parsed token.
369  """
370  if not token:
371  return False
372  c = token[0]
373  if not c.isalpha() and c != "_":
374  return False
375  for c in token[1:]:
376  if not c.isalnum() and c != "_":
377  return False
378  return True
def IsIdentifier(token)
Definition: NetMsgsBase.py:364
def NetMsgs.NetMsgsBase.PrettyPrintAssignExpr (   name,
  val,
  col = 0,
  indent = 0,
  fp = sys.stderr 
)
Pretty print name = value.

    Parameters:
      nam     - Variable name
      val     - Variable value.
      col     - Current column position.
      indent  - Indentation.
      fp      - Opened file pointer.

Definition at line 496 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.PrettyPrintVal(), and NetMsgs.NetMsgsBase.space.

496 def PrettyPrintAssignExpr(name, val, col=0, indent=0, fp=sys.stderr):
497  """ Pretty print name = value.
498 
499  Parameters:
500  nam - Variable name
501  val - Variable value.
502  col - Current column position.
503  indent - Indentation.
504  fp - Opened file pointer.
505  """
506  sp = _nspaces(col, indent)
507  lhs = "%s%s = " % (space(sp), name)
508  fp.write(lhs)
509  PrettyPrintVal(val, col=len(lhs), indent=indent, fp=fp)
510  fp.write('\n')
string space
space quickie
Definition: NetMsgsBase.py:320
def PrettyPrintAssignExpr(name, val, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:496
def PrettyPrintVal(val, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:514
def NetMsgs.NetMsgsBase.PrettyPrintCols (   fp,
  cursor,
  args,
  kwargs 
)
Pretty print argument strings aligned to column.

    Parameters:
      cursor    - Current column cursor position.
      args      - List of argument (pos, s) 2-tuples.
      kwargs    - Print control keywords.

Definition at line 382 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.PrintBuf(), and NetMsgs.NetMsgsBase.space.

382 def PrettyPrintCols(fp, cursor, *args, **kwargs):
383  """ Pretty print argument strings aligned to column.
384 
385  Parameters:
386  cursor - Current column cursor position.
387  args - List of argument (pos, s) 2-tuples.
388  kwargs - Print control keywords.
389  """
390  while len(args) >= 2:
391  linecont = kwargs.get('linecont', '')
392  force = kwargs.get('force', False)
393  pos = args[0]
394  s = args[1]
395  args = args[2:]
396  if (pos <= cursor) and (cursor > 0):
397  if not force or cursor > 78:
398  fp.write("%s\n" % (linecont))
399  cursor = 0
400  else:
401  fp.write(" ")
402  cursor += 1
403  if pos > cursor:
404  fp.write(space(pos-cursor))
405  cursor = pos
406  fp.write("%s" % (s))
407  cursor += len(s)
408  return cursor
string space
space quickie
Definition: NetMsgsBase.py:320
def PrettyPrintCols(fp, cursor, args, kwargs)
Definition: NetMsgsBase.py:382
def NetMsgs.NetMsgsBase.PrettyPrintDict (   d,
  col = 0,
  indent = 0,
  fp = sys.stderr 
)
Pretty print dictionary in sorted, indented clarity.

    Parameters:
      d       - The dictionary.
      col     - Current column position.
      indent  - Indentation.
      fp      - Opened file pointer.

    Return:
      New column position

Definition at line 538 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.PrettyPrintVal(), and NetMsgs.NetMsgsBase.space.

Referenced by NetMsgs.NetMsgsBase.PrettyPrintVal().

538 def PrettyPrintDict(d, col=0, indent=0, fp=sys.stderr):
539  """ Pretty print dictionary in sorted, indented clarity.
540 
541  Parameters:
542  d - The dictionary.
543  col - Current column position.
544  indent - Indentation.
545  fp - Opened file pointer.
546 
547  Return:
548  New column position
549  """
550  sp = _nspaces(col, indent)
551  s = repr(d)
552  if col + sp + len(s) < 80:
553  fp.write('%s{' % space(sp))
554  col = col + sp + 1
555  keys = d.keys()
556  keys.sort()
557  for k in keys:
558  key = _atval(k)
559  fp.write("%s:" % key)
560  col += len(key) + 1
561  col = PrettyPrintVal(d[k], col=col, indent=indent, fp=fp)
562  fp.write(', ')
563  col += 2
564  fp.write("}")
565  return col + 1
566  else:
567  fp.write('%s{\n' % space(sp))
568  col = 0
569  indent += 2
570  keys = d.keys()
571  keys.sort()
572  for k in keys:
573  key = _atval(k)
574  key = "%s%s: " % (space(indent), key)
575  fp.write(key)
576  PrettyPrintVal(d[k], col=len(key), indent=indent, fp=fp)
577  fp.write(',\n')
578  indent -= 2
579  fp.write("%s}" % (space(indent)))
580  return indent + 1
string space
space quickie
Definition: NetMsgsBase.py:320
def PrettyPrintVal(val, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:514
def PrettyPrintDict(d, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:538
def NetMsgs.NetMsgsBase.PrettyPrintList (   l,
  col = 0,
  indent = 0,
  fp = sys.stderr 
)
Pretty print list.

    Parameters:
      l       - The list.
      col     - Current column position.
      indent  - Indentation.
      fp      - Opened file pointer.

    Return:
      New column position

Definition at line 584 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.PrettyPrintVal(), and NetMsgs.NetMsgsBase.space.

Referenced by NetMsgs.NetMsgsBase.PrettyPrintVal().

584 def PrettyPrintList(l, col=0, indent=0, fp=sys.stderr):
585  """ Pretty print list.
586 
587  Parameters:
588  l - The list.
589  col - Current column position.
590  indent - Indentation.
591  fp - Opened file pointer.
592 
593  Return:
594  New column position
595  """
596  sp = _nspaces(col, indent)
597  issimple = True
598  for v in l:
599  if (type(v) == dict) or (type(v) == list):
600  issimple = False
601  break
602  if issimple:
603  fp.write('%s[ ' % space(sp))
604  col = col + sp + 2
605  for v in l:
606  if col > 60: # heuristiccally 'safe'
607  fp.write('\n%s ' % space(indent))
608  col = col + sp + 2
609  col = PrettyPrintVal(v, col=col, indent=indent, fp=fp)
610  fp.write(', ')
611  col += 2
612  fp.write(' ]')
613  return col + 2
614  else:
615  fp.write('%s[\n' % space(sp))
616  col = 0
617  indent += 2
618  for v in l:
619  PrettyPrintVal(v, col=col, indent=indent, fp=fp)
620  fp.write(',\n')
621  indent -= 2
622  fp.write("%s]" % (space(indent)))
623  return indent + 1
string space
space quickie
Definition: NetMsgsBase.py:320
def PrettyPrintVal(val, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:514
def PrettyPrintList(l, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:584
def NetMsgs.NetMsgsBase.PrettyPrintVal (   val,
  col = 0,
  indent = 0,
  fp = sys.stderr 
)
Pretty print value.

    Parameters:
      val     - Variable value.
      col     - Current column position.
      indent  - Indentation.
      fp      - Opened file pointer.

    Return:
      New column position

Definition at line 514 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.PrettyPrintDict(), NetMsgs.NetMsgsBase.PrettyPrintList(), and NetMsgs.NetMsgsBase.space.

Referenced by NetMsgs.NetMsgsBase.PrettyPrintAssignExpr(), NetMsgs.NetMsgsBase.PrettyPrintDict(), and NetMsgs.NetMsgsBase.PrettyPrintList().

514 def PrettyPrintVal(val, col=0, indent=0, fp=sys.stderr):
515  """ Pretty print value.
516 
517  Parameters:
518  val - Variable value.
519  col - Current column position.
520  indent - Indentation.
521  fp - Opened file pointer.
522 
523  Return:
524  New column position
525  """
526  if type(val) == dict:
527  return PrettyPrintDict(val, col=col, indent=indent, fp=fp)
528  elif type(val) == list:
529  return PrettyPrintList(val, col=col, indent=indent, fp=fp)
530  else:
531  sp = _nspaces(col, indent)
532  v = _atval(val)
533  fp.write("%s%s" % (space(sp), v))
534  return col + sp + len(v)
string space
space quickie
Definition: NetMsgsBase.py:320
def PrettyPrintVal(val, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:514
def PrettyPrintDict(d, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:538
def PrettyPrintList(l, col=0, indent=0, fp=sys.stderr)
Definition: NetMsgsBase.py:584
def NetMsgs.NetMsgsBase.PrintBits (   val,
  msbit,
  count = None,
  preface = '',
  fp = sys.stderr 
)
Pretty print bits in value to opened file stream. 

    Parameters:
      val     - Bits to print.
      msbit   - Starting most significant bit, zero based.
      count   - Number of bits to print (None = msbit+1).
      preface - Optional buffer preface string.
      fp      - Output file pointer.

Definition at line 446 of file NetMsgsBase.py.

446 def PrintBits(val, msbit, count=None, preface='', fp=sys.stderr):
447  """ Pretty print bits in value to opened file stream.
448 
449  Parameters:
450  val - Bits to print.
451  msbit - Starting most significant bit, zero based.
452  count - Number of bits to print (None = msbit+1).
453  preface - Optional buffer preface string.
454  fp - Output file pointer.
455  """
456  if preface:
457  s = "%s: " % (preface)
458  fp.write(s)
459  if count is None:
460  count = msbit + 1
461  i = 0
462  while i < count:
463  if ((msbit % 8) == 7) and (i > 0):
464  fp.write(' ')
465  if (val >> msbit) & 0x01:
466  fp.write('1')
467  else:
468  fp.write('0')
469  msbit -= 1
470  i += 1
def PrintBits(val, msbit, count=None, preface='', fp=sys.stderr)
Definition: NetMsgsBase.py:446
def NetMsgs.NetMsgsBase.PrintBuf (   buf,
  count = None,
  preface = '',
  nlfreq = None,
  indent = 0,
  col = 0,
  fp = sys.stderr 
)
Pretty print binary buffer to opened file stream.

    Parameters:
      buf     - Buffer to print.
      count   - Number of bytes to print.
      preface - Optional buffer preface string.
      nlfreq  - Newline frequency (None for no newlines).
      ident   - Indentation column alignment.
      col     - Current column position.
      fp      - Output file pointer.

Definition at line 413 of file NetMsgsBase.py.

References NetMsgs.NetMsgsBase.space.

Referenced by NetMsgs.NetMsgsBase.PrettyPrintCols().

413  fp=sys.stderr):
414  """ Pretty print binary buffer to opened file stream.
415 
416  Parameters:
417  buf - Buffer to print.
418  count - Number of bytes to print.
419  preface - Optional buffer preface string.
420  nlfreq - Newline frequency (None for no newlines).
421  ident - Indentation column alignment.
422  col - Current column position.
423  fp - Output file pointer.
424  """
425  if preface:
426  s = "%s: " % (preface)
427  col += len(s)
428  fp.write(s)
429  if count is None:
430  count = len(buf)
431  if (count > 0) and (col < indent):
432  fp.write(space(indent-col))
433  i = 0
434  while i < count:
435  c = buf[i]
436  if nlfreq and ((i % nlfreq) == 0) and (i > 0):
437 
438  fp.write("\n%s" % space(indent))
439  col = indent
440  fp.write("0x%02x " % (ord(c)))
441  i += 1
442  fp.write('\n')
string space
space quickie
Definition: NetMsgsBase.py:320
def NetMsgs.NetMsgsBase.StrError (   ecode)
Get the error string describing the NetMsgs error code.
    
    The absolute value of the error code is taken prior retrieving the
    string. An unknown or out-of-range error code will be mapped to
    NM_ECODE_BADEC.
  
    Parameters:
      ecode     - NetMsgs error code.

    Return:
      The appropriate error code string.

Definition at line 323 of file NetMsgsBase.py.

323 def StrError(ecode):
324  """ Get the error string describing the NetMsgs error code.
325 
326  The absolute value of the error code is taken prior retrieving the
327  string. An unknown or out-of-range error code will be mapped to
328  NM_ECODE_BADEC.
329 
330  Parameters:
331  ecode - NetMsgs error code.
332 
333  Return:
334  The appropriate error code string.
335  """
336  sErr = nmStrError(ecode)
337  if not sErr:
338  sErr = 'Error'
339  return sErr
def StrError(ecode)
Definition: NetMsgsBase.py:323

Variable Documentation

dictionary NetMsgs.NetMsgsBase.NMAliasMap
Initial value:
1 = {
2  'byte': 'u8', 'schar': 's8', 'ushort': 'u16', 'short': 'u16',
3  'uint': 'u32', 'int': 's32', 'ulonglong': 'u64', 'longlong': 's64',
4  'pointer': 'p32', 'longpointer': 'p64', 'float': 'f32', 'double': 'f64',
5 }

The full set of XML ftype values.

Definition at line 246 of file NetMsgsBase.py.

dictionary NetMsgs.NetMsgsBase.NMBuiltInFieldTypes

Built-In message field types, keyed by XML field type name.

code - message field type byte code desc - short description flen - packed message field length (bytes) comp - complexity. one of: simple compound na T - C/C++ type specifier pre - member name prefix (quasi-Hungarian)

Definition at line 78 of file NetMsgsBase.py.

NetMsgs.NetMsgsBase.NMFCode = lambdaxmlftype:NMBuiltInFieldTypes[xmlftype]['code']

Get NetMsgs field type code given the XML field type.

Definition at line 243 of file NetMsgsBase.py.

dictionary NetMsgs.NetMsgsBase.NMFHdrLen
Initial value:
1 = {
2  'flat': {'simple': 0, 'string': 0, 'struct': 0, 'vector': 0},
3  'itv': {
4  'simple': NMITV_FHDR_SIZE_SIMPLE, 'string': NMITV_FHDR_SIZE_STRING,
5  'struct': NMITV_FHDR_SIZE_STRUCT, 'vector': NMITV_FHDR_SIZE_VECTOR},
6 }

Field Header Lengths keyed by message encoding.

Definition at line 297 of file NetMsgsBase.py.

dictionary NetMsgs.NetMsgsBase.NMFTypeCode2Xml
Initial value:
1 = {
2  NMFCode('bool'): 'bool', NMFCode('char'): 'char',
3  NMFCode('u8'): 'u8', NMFCode('s8'): 's8',
4  NMFCode('u16'): 'u16', NMFCode('s16'): 's16',
5  NMFCode('u32'): 'u32', NMFCode('s32'): 's32',
6  NMFCode('u64'): 'u64', NMFCode('s64'): 's64',
7  NMFCode('f32'): 'f32', NMFCode('f64'): 'f64',
8  NMFCode('p32'): 'p32', NMFCode('p64'): 'p64',
9  NMFCode('string'): 'string', NMFCode('pad'): 'pad',
10  NMFCode('struct'): 'struct', NMFCode('vector'): 'vector'
11 }
NMFCode
Get NetMsgs field type code given the XML field type.
Definition: NetMsgsBase.py:243

Field type code to XML file type map.

Definition at line 284 of file NetMsgsBase.py.

list NetMsgs.NetMsgsBase.NMFTypeCodeSimple
Initial value:
1 = [
2  NMFCode('char'), NMFCode('u8'), NMFCode('s8'), NMFCode('bool'),
3  NMFCode('u16'), NMFCode('s16'), NMFCode('u32'), NMFCode('s32'),
4  NMFCode('u64'), NMFCode('s64'), NMFCode('f32'), NMFCode('f64'),
5  NMFCode('p32'), NMFCode('p64') ]
NMFCode
Get NetMsgs field type code given the XML field type.
Definition: NetMsgsBase.py:243

List of simple field types by field type code.

Definition at line 267 of file NetMsgsBase.py.

list NetMsgs.NetMsgsBase.NMFTypeNumber
Initial value:
1 = [
2  'u8', 's8', 'u16', 's16', 'u32', 's32', 'u64', 's64', 'f32', 'f64' ]

List of number field types by XML ftype.

Definition at line 280 of file NetMsgsBase.py.

list NetMsgs.NetMsgsBase.NMFTypeSimple
Initial value:
1 = [
2  'char', 'u8', 's8', 'bool', 'u16', 's16', 'u32', 's32', 'u64',
3  's64', 'f32', 'f64', 'p32', 'p64', ]

List of simple field types by XML ftype.

Definition at line 262 of file NetMsgsBase.py.