summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns/classes.py
blob: 9446f011328a12154801d05044e6ccc1b6040466 (plain) (blame)
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
""" DNS CLASS and QCLASS values

This module contains an Enum of CLASS and QCLASS values. The Enum also contains
a method for converting values to strings. See sections 3.2.4 and 3.2.5 of RFC
1035 for more information.
"""


class Class(object):
    """ Enum of CLASS and QCLASS values

    Usage:
        >>> Class.IN
        1
        >>> Class.ANY
        255
    """

    IN = 1
    CS = 2
    CH = 3
    HS = 4
    ANY = 255

    by_string = {
        "IN": IN,
        "CS": CS,
        "CH": CH,
        "HS": HS,
        "*": ANY
    }

    by_value = dict([(y, x) for x, y in by_string.items()])

    @staticmethod
    def to_string(class_):
        """Convert a class to a string"""
        return Class.by_value[class_]

    @staticmethod
    def from_string(string):
        """Convert a string to a class"""
        return Class.by_string[string]