summaryrefslogtreecommitdiff
path: root/detection/awdb/compat.py
blob: 9952ac4987794fa4b41706e21bbf5b20646a473a (plain)
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
import sys

import ipaddress


if sys.version_info[0] == 2:

    def compat_ip_address(address):
        if isinstance(address, bytes):
            address = address.decode()
        return ipaddress.ip_address(address)

    int_from_byte = ord

    FileNotFoundError = IOError

    def int_from_bytes(b):
        if b:
            return int(b.encode("hex"), 16)
        return 0

    byte_from_int = chr

    string_type = basestring

else:

    def compat_ip_address(address):
        return ipaddress.ip_address(address)

    int_from_byte = lambda x: x

    FileNotFoundError = FileNotFoundError

    int_from_bytes = lambda x: int.from_bytes(x, 'big')

    byte_from_int = lambda x: bytes([x])

    string_type = str