#!/usr/bin/env python # test script from __future__ import print_function import io import sys import types import importlib if sys.version_info[0] == 2: import kaitaistruct import kshelpers else: from . import kaitaistruct from . import kshelpers NORMAL = '\033[0m' BLACK = '\033[0;30m' RED = '\033[0;31m' GREEN = '\033[0;32m' YELLOW = '\033[0;33m' BLUE = '\033[0;34m' PURPLE = '\033[0;35m' CYAN = '\033[0;36m' GRAY = '\033[0;37m' LBLACK = '\033[1;30m' LRED = '\033[1;31m' LGREEN = '\033[1;32m' LYELLOW = '\033[1;33m' LBLUE = '\033[1;34m' LPURPLE = '\033[1;35m' LCYAN = '\033[1;36m' LGRAY = '\033[1;37m' def dump(obj, depth=0): dump_exceptions = ['_root', '_parent', '_io', 'SEQ_FIELDS'] indent = ' '*depth if isinstance(obj, kaitaistruct.KaitaiStruct): fieldNames = [] for candidate in dir(obj): if candidate != '_debug' and candidate.startswith('_'): continue if candidate in dump_exceptions: continue try: if getattr(obj, candidate, False): fieldNames.append(candidate) except Exception: pass for fieldName in fieldNames: subObj = getattr(obj, fieldName) if type(subObj) == types.MethodType: pass #elif type(subObj) == types.TypeType: elif isinstance(subObj, type): pass elif fieldName == '_debug': print(('%s._debug:'+RED+' %s'+NORMAL) % (indent, repr(subObj))) #elif type(subObj) == types.ListType: elif isinstance(subObj, list): if len(subObj)>0 and isinstance(subObj[0], kaitaistruct.KaitaiStruct): for i in range(len(subObj)): print('%s.%s[%d]:' % (indent, fieldName, i)) dump(subObj[i], depth+1) else: print('%s.%s: %s' % (indent, fieldName, str(subObj))) #elif type(subObj) == types.DictionaryType: elif isinstance(subObj, dict): print('%s.%s: %s' % (indent, fieldName, subObj)) #elif type(subObj) == types.StringType: elif isinstance(subObj, str): if len(subObj) <= 16: print(('%s.%s: '+CYAN+'%s'+NORMAL) % (indent, fieldName, repr(subObj))) else: print(('%s.%s: '+CYAN+'%s...'+NORMAL+' 0x%X (%d) bytes total') % \ (indent, fieldName, repr(subObj[0:16]), len(subObj), len(subObj)) ) elif type(subObj) == int: print(('%s.%s: '+YELLOW+'0x%X '+NORMAL+'('+YELLOW+'%d'+NORMAL+')') % (indent, fieldName, subObj, subObj)) elif str(type(subObj)).startswith('