aboutsummaryrefslogtreecommitdiff
blob: 872e0a36d667adae9495ab49a7ba404c196ec705 (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
"""
A module to (non)interactively query the user for impure values
"""
from pomu.util.result import Result

class _query:
    def __init__(self):
        self.map = {}
        self.interactive = True

    def __call__(self, name, prompt=None, default=None):
        """
        Queries the impure world for name
        Parameters:
            name - the name
            prompt - prompt text
            default - default value used for errors, forced non-interactive etc.
        TODO: non-interactive
        """
        if name in self.map:
            return Result.Ok(self.map[name])
        if not prompt:
            prompt = 'Please enter ' + name
        if default: prompt += ' ({})'.format(default)
        prompt += ' > '
        res = None
        if self.interactive:
            try:
                res = input(prompt)
            except EOFError: pass
        if not res:
            res = default
        if not res:
            return Result.Err('No {} or default provided'.format(name))
        return Result.Ok(res)

    def set(self, name, val):
        old = None
        if name in self.map:
            old = self.map[name]
        if val is None:
            self.unset(name)
        self.map[name] = val
        return old

    def unset(self, name):
        if name in self.map:
            del self.map[name]

query = _query()

class QueryContext:
    def __init__(self, **kwargs):
        self.map = kwargs

    def __enter__(self):
        self.map_old = {x: query.set(x, self.map[x]) for x in self.map}

    def __exit__(self):
        for x, y in self.map_old.items():
            query.set(x, y)