-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtools.py
116 lines (88 loc) · 2.83 KB
/
tools.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# tools.py
#
# Utility functions
import struct
import six
import socket
import IPy
def EncodeString(str):
if len(str) > 253:
raise ValueError('Can only encode strings of <= 253 characters')
if isinstance(str, six.text_type):
return str.encode('utf-8')
else:
return str
def EncodeOctets(str):
if len(str) > 253:
raise ValueError('Can only encode strings of <= 253 characters')
return str
def EncodeAddress(addr):
if not isinstance(addr, six.string_types):
raise TypeError('Address has to be a string')
(a, b, c, d) = map(int, addr.split('.'))
return struct.pack('BBBB', a, b, c, d)
def EncodeIPv6Address(addr):
return socket.inet_pton(socket.AF_INET6, addr)
def EncodeIPv6Prefix(addr):
addr = str(IPy.IP(addr).make_net(64).net())
address = socket.inet_pton(socket.AF_INET6, addr)
return struct.pack('!BB16s', 0,64, address)
def EncodeInteger(num):
if not isinstance(num, six.integer_types):
raise TypeError('Can not encode non-integer as integer')
return struct.pack('!I', num)
def EncodeDate(num):
if not isinstance(num, int):
raise TypeError('Can not encode non-integer as date')
return struct.pack('!I', num)
def DecodeString(str):
try:
return str.decode('utf-8')
except:
return str
def DecodeOctets(str):
return str
def DecodeAddress(addr):
return '.'.join(map(str, struct.unpack('BBBB', addr)))
def DecodeIPv6Address(addr):
return socket.inet_ntop(socket.AF_INET6, addr)
def DecodeInteger(num):
return (struct.unpack('!I', num))[0]
def DecodeDate(num):
return (struct.unpack('!I', num))[0]
def DecodeIPv6Prefix(addr):
return str(addr)
def EncodeAttr(datatype, value):
if datatype == 'string':
return EncodeString(value)
elif datatype == 'octets':
return EncodeOctets(value)
elif datatype == 'ipaddr':
return EncodeAddress(value)
elif datatype == 'ipv6addr':
return EncodeIPv6Address(value)
elif datatype == 'ipv6prefix':
return EncodeIPv6Prefix(value)
elif datatype == 'integer':
return EncodeInteger(value)
elif datatype == 'date':
return EncodeDate(value)
else:
raise ValueError('Unknown attribute type %s' % datatype)
def DecodeAttr(datatype, value):
if datatype == 'string':
return DecodeString(value)
elif datatype == 'octets':
return DecodeOctets(value)
elif datatype == 'ipaddr':
return DecodeAddress(value)
elif datatype == 'ipv6addr':
return DecodeIPv6Address(value)
elif datatype == 'ipv6prefix':
return DecodeIPv6Prefix(value)
elif datatype == 'integer':
return DecodeInteger(value)
elif datatype == 'date':
return DecodeDate(value)
else:
raise ValueError('Unknown attribute type %s' % datatype)