9 NetMsgs Base Data Module 61 from NetMsgsCore
import *
64 NMEncoding = [
'flat',
'itv']
67 NMEndian = [
'big',
'little',
'native']
78 NMBuiltInFieldTypes = {
89 'desc':
"8-bit character",
90 'flen': NMFVAL_LEN_CHAR,
97 'desc':
"unsigned 8-bit integer",
98 'flen': NMFVAL_LEN_U8,
105 'desc':
"signed 8-bit integer",
106 'flen': NMFVAL_LEN_S8,
113 'desc':
"boolean 0=false, 1(non-zero)=true",
114 'flen': NMFVAL_LEN_BOOL,
121 'desc':
"unsigned 16-bit integer",
122 'flen': NMFVAL_LEN_U16,
129 'desc':
"signed 16-bit integer",
130 'flen': NMFVAL_LEN_S16,
137 'desc':
"unsigned 32-bit integer",
138 'flen': NMFVAL_LEN_U32,
145 'desc':
"signed 32-bit integer",
146 'flen': NMFVAL_LEN_S32,
153 'desc':
"unsigned 64-bit integer",
154 'flen': NMFVAL_LEN_U64,
161 'desc':
"signed 64-bit integer",
162 'flen': NMFVAL_LEN_S64,
169 'desc':
"32-bit floating-point number",
170 'flen': NMFVAL_LEN_F32,
177 'desc':
"64-bit floating-point number",
178 'flen': NMFVAL_LEN_F64,
185 'desc':
"32-bit pointer",
186 'flen': NMFVAL_LEN_P32,
193 'desc':
"64-bit pointer",
194 'flen': NMFVAL_LEN_P64,
200 'code': NMFTypeString,
201 'desc':
"char[] string",
208 'code': NMFTypeStruct,
216 'code': NMFTypeVector,
217 'desc':
"vector - one dimensional array",
229 NMBuiltInFieldTypes[
'byte'] = NMBuiltInFieldTypes[
'u8']
230 NMBuiltInFieldTypes[
'schar'] = NMBuiltInFieldTypes[
's8']
231 NMBuiltInFieldTypes[
'ushort'] = NMBuiltInFieldTypes[
'u16']
232 NMBuiltInFieldTypes[
'short'] = NMBuiltInFieldTypes[
's16']
233 NMBuiltInFieldTypes[
'uint'] = NMBuiltInFieldTypes[
'u32']
234 NMBuiltInFieldTypes[
'int'] = NMBuiltInFieldTypes[
's32']
235 NMBuiltInFieldTypes[
'ulonglong'] = NMBuiltInFieldTypes[
'u64']
236 NMBuiltInFieldTypes[
'longlong'] = NMBuiltInFieldTypes[
's64']
237 NMBuiltInFieldTypes[
'pointer'] = NMBuiltInFieldTypes[
'p32']
238 NMBuiltInFieldTypes[
'longpointer'] = NMBuiltInFieldTypes[
'p64']
239 NMBuiltInFieldTypes[
'float'] = NMBuiltInFieldTypes[
'f32']
240 NMBuiltInFieldTypes[
'double'] = NMBuiltInFieldTypes[
'f64']
243 NMFCode =
lambda xmlftype: NMBuiltInFieldTypes[xmlftype][
'code']
247 'byte':
'u8',
'schar':
's8',
'ushort':
'u16',
'short':
'u16',
248 'uint':
'u32',
'int':
's32',
'ulonglong':
'u64',
'longlong':
's64',
249 'pointer':
'p32',
'longpointer':
'p64',
'float':
'f32',
'double':
'f64',
259 NMVectorSuffix =
'[]' 263 'char',
'u8',
's8',
'bool',
'u16',
's16',
'u32',
's32',
'u64',
264 's64',
'f32',
'f64',
'p32',
'p64', ]
267 NMFTypeCodeSimple = [
274 NMFTypeCompound = [
'string',
'struct',
'vector' ]
281 'u8',
's8',
'u16',
's16',
'u32',
's32',
'u64',
's64',
'f32',
'f64' ]
298 'flat': {
'simple': 0,
'string': 0,
'struct': 0,
'vector': 0},
300 'simple': NMITV_FHDR_SIZE_SIMPLE,
'string': NMITV_FHDR_SIZE_STRING,
301 'struct': NMITV_FHDR_SIZE_STRUCT,
'vector': NMITV_FHDR_SIZE_VECTOR},
305 NMFIdNone = NMFID_NONE
311 NMPadFVal = NMFTypePadTr
314 NMStringMaxCount = NMFVAL_LEN_MAX_STRING
317 NMVectorMaxCount = NMFVAL_LEN_MAX_VECTOR
320 space =
lambda indent:
"%*s" % (indent,
'')
324 """ Get the error string describing the NetMsgs error code. 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 331 ecode - NetMsgs error code. 334 The appropriate error code string. 336 sErr = nmStrError(ecode)
347 """ NetMsgs Exception Class. """ 353 msg - Exception message string. 355 Exception.__init__(self, msg)
365 """ Returns True if token is a valid identifier, else False. 368 token - Parsed token. 373 if not c.isalpha()
and c !=
"_":
376 if not c.isalnum()
and c !=
"_":
383 """ Pretty print argument strings aligned to column. 386 cursor - Current column cursor position. 387 args - List of argument (pos, s) 2-tuples. 388 kwargs - Print control keywords. 390 while len(args) >= 2:
391 linecont = kwargs.get(
'linecont',
'')
392 force = kwargs.get(
'force',
False)
396 if (pos <= cursor)
and (cursor > 0):
397 if not force
or cursor > 78:
398 fp.write(
"%s\n" % (linecont))
404 fp.write(
space(pos-cursor))
412 def PrintBuf(buf, count=None, preface='', nlfreq=None, indent=0, col=0,
414 """ Pretty print binary buffer to opened file stream. 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. 426 s =
"%s: " % (preface)
431 if (count > 0)
and (col < indent):
432 fp.write(
space(indent-col))
436 if nlfreq
and ((i % nlfreq) == 0)
and (i > 0):
438 fp.write(
"\n%s" %
space(indent))
440 fp.write(
"0x%02x " % (ord(c)))
446 def PrintBits(val, msbit, count=None, preface='', fp=sys.stderr):
447 """ Pretty print bits in value to opened file stream. 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. 457 s =
"%s: " % (preface)
463 if ((msbit % 8) == 7)
and (i > 0):
465 if (val >> msbit) & 0x01:
475 """ Convert value to string equivaling. 477 String values starting with an '@' are treated as variables, not strings. 486 if (type(val) == str)
and (len(val) > 0)
and (val[0] ==
'@'):
493 _nspaces =
lambda col,indent: col<indent
and indent-col
or 0
497 """ Pretty print name = value. 501 val - Variable value. 502 col - Current column position. 503 indent - Indentation. 504 fp - Opened file pointer. 506 sp = _nspaces(col, indent)
507 lhs =
"%s%s = " % (
space(sp), name)
515 """ Pretty print value. 518 val - Variable value. 519 col - Current column position. 520 indent - Indentation. 521 fp - Opened file pointer. 526 if type(val) == dict:
528 elif type(val) == list:
531 sp = _nspaces(col, indent)
533 fp.write(
"%s%s" % (
space(sp), v))
534 return col + sp + len(v)
539 """ Pretty print dictionary in sorted, indented clarity. 543 col - Current column position. 544 indent - Indentation. 545 fp - Opened file pointer. 550 sp = _nspaces(col, indent)
552 if col + sp + len(s) < 80:
553 fp.write(
'%s{' %
space(sp))
559 fp.write(
"%s:" % key)
567 fp.write(
'%s{\n' %
space(sp))
574 key =
"%s%s: " % (
space(indent), key)
579 fp.write(
"%s}" % (
space(indent)))
585 """ Pretty print list. 589 col - Current column position. 590 indent - Indentation. 591 fp - Opened file pointer. 596 sp = _nspaces(col, indent)
599 if (type(v) == dict)
or (type(v) == list):
603 fp.write(
'%s[ ' %
space(sp))
607 fp.write(
'\n%s ' %
space(indent))
615 fp.write(
'%s[\n' %
space(sp))
622 fp.write(
"%s]" % (
space(indent)))
string space
space quickie
def PrettyPrintAssignExpr(name, val, col=0, indent=0, fp=sys.stderr)
def PrintBuf(buf, count=None, preface='', nlfreq=None, indent=0, col=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 __init__(self, msg='XML Parser Error')
NMFCode
Get NetMsgs field type code given the XML field type.
def PrettyPrintCols(fp, cursor, args, kwargs)
def PrintBits(val, msbit, count=None, preface='', fp=sys.stderr)
def PrettyPrintList(l, col=0, indent=0, fp=sys.stderr)