1 import sys 2 from pyasn1.compat.octets import octs2ints 3 from pyasn1 import error 4 from pyasn1 import __version__ 5 6 flagNone = 0x0000 7 flagEncoder = 0x0001 8 flagDecoder = 0x0002 9 flagAll = 0xffff 10 11 flagMap = { 12 'encoder': flagEncoder, 13 'decoder': flagDecoder, 14 'all': flagAll 15 } 16 17 class Debug: 18 defaultPrinter = sys.stderr.write 19 def __init__(self, *flags): 20 self._flags = flagNone 21 self._printer = self.defaultPrinter 22 self('running pyasn1 version %s' % __version__) 23 for f in flags: 24 if f not in flagMap: 25 raise error.PyAsn1Error('bad debug flag %s' % (f,)) 26 self._flags = self._flags | flagMap[f] 27 self('debug category \'%s\' enabled' % f) 28 29 def __str__(self): 30 return 'logger %s, flags %x' % (self._printer, self._flags) 31 32 def __call__(self, msg): 33 self._printer('DBG: %s\n' % msg) 34 35 def __and__(self, flag): 36 return self._flags & flag 37 38 def __rand__(self, flag): 39 return flag & self._flags 40 41 logger = 0 42 43 def setLogger(l): 44 global logger 45 logger = l 46 47 def hexdump(octets): 48 return ' '.join( 49 [ '%s%.2X' % (n%16 == 0 and ('\n%.5d: ' % n) or '', x) 50 for n,x in zip(range(len(octets)), octs2ints(octets)) ] 51 ) 52 53 class Scope: 54 def __init__(self): 55 self._list = [] 56 57 def __str__(self): return '.'.join(self._list) 58 59 def push(self, token): 60 self._list.append(token) 61 62 def pop(self): 63 return self._list.pop() 64 65 scope = Scope() 66