summaryrefslogtreecommitdiff
path: root/python3/vdebug/opts.py
blob: 111f75218441756a1a619b34985da368ae16e76d (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

class Options:
    instance = None

    def __init__(self, options):
        self.options = options

    @classmethod
    def set(cls, options):
        """Create an Options instance with the provided dictionary of
        options"""
        cls.instance = Options(options)

    @classmethod
    def inst(cls):
        """Get the Options instance.
        """
        if cls.instance is None:
            raise OptionsError("No options have been set")
        return cls.instance

    @classmethod
    def get(cls, name, as_type=str):
        """Get an option by name.

        Raises an OptionsError if the option doesn't exist.
        """
        inst = cls.inst()
        if name in inst.options:
            return as_type(inst.options[name])
        raise OptionsError("No option with key '%s'" % name)

    @classmethod
    def get_for_print(cls, name):
        """Get an option by name and for human readable output.

        Raises an OptionsError if the option doesn't exist.
        """
        option = cls.get(name)
        if not option:
            return "<empty>"
        return option

    @classmethod
    def overwrite(cls, name, value):
        inst = cls.inst()
        inst.options[name] = value

    @classmethod
    def isset(cls, name):
        """Checks whether the option exists and is set.

        By set, it means whether the option has length. All the option
        values are strings.
        """
        inst = cls.inst()
        return name in inst.options and len(inst.options[name]) > 0


class OptionsError(Exception):
    pass