-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
68 lines (60 loc) · 1.44 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# coding=utf-8
import string
def bread(d):
''' read multibyte int (in DL common order)'''
out = d[-1]
for n in reversed(d[:-1]):
out = (out << 8) | n
return out
def rbread(d):
''' read multibyte int (in reversed order)'''
out = d[0]
for n in d[1:]:
out = (out << 8) | n
return out
def sread(d):
''' read string'''
out = ''
for b in d:
if b == 0:
return out
out += chr(b)
return out
def tchars(txt):
''' translate special chars to proper letters'''
#print txt
return txt.replace('|', 'ü').replace('{', 'ö').replace(chr(0x1f), 'æ')
def cstrim(txt):
''' trim C-string '''
return txt[:txt.find(b'\0')]
def itemStr(c, attrs=None):
''' str(struct) '''
items = []
if type(c) is not dict:
c = vars(c)
out = ''
for k, v in c.iteritems():
if attrs and k not in attrs:
continue
out += "%s: "%k
if type(v) == dict:
out += '{\n'
for vk, vv in v.iteritems():
out += "\t%s: %s\n"%(vk, vv)
out += '}'
else:
out += str(v)
out += '\n'
return out
def itemLn(c, attrs=None):
''' render in line '''
out = ''
if not attrs:
attrs = c.keys()
for k in attrs:
l = 5
if type(k) == tuple:
k, l = k
fmt = "%%%ds "%(l)
out += fmt%(str(c[k])[:l])
return out