aboutsummaryrefslogtreecommitdiff
path: root/sandbox/context-proxy
blob: 2b6a366b02c8a66d9e7a6514a65e1138fb59c163 (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
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
#!/usr/bin/python

from sys import stderr, stdin
from subprocess import Popen, PIPE
from select import select
import re
import os

class Program:
    def __init__(self, cline):
        d = dict(os.environ)
        d.update({"CONTEXT_CLI_DISABLE_TYPE_CHECK": "1",
                   "CONTEXT_CLI_IGNORE_COMMANDER": "1"})

        self.__process = Popen(cline, stdin=PIPE, stdout=PIPE, stderr=PIPE,
                               env = d)

    def send(self, string):
        print >>self.__process.stdin, string
        self.__process.stdin.flush()

    def outfd(self):
        return self.__process.stdout.fileno()

    def readline(self):
        return self.__process.stdout.readline()

    def ready(self):
        raise NotImplementedError

class Listen(Program):
    def __init__(self, *properties):
        Program.__init__(self, ["context-listen"] + list(properties))

    def ready(self):
        global provide
        line = self.readline()
        if line:
            print >>stderr, "LISTEN:", line,
            match = re.match("(.*?) = (.*?):(.*)\n", line)
            if match:
                property = match.group(1)
                type = ""
                if match.group(2) == "QString":
                    type = "string"
                elif match.group(2) == "int":
                    type = "int"
                elif match.group(2) == "bool":
                    type = "truth"
                elif match.group(2) == "double":
                    type = "double"
                else:
                    raise RuntimeError("unknown type from client: " + match.group(2))
                value = match.group(3)
                provide.send("add " + type + " " + property + " " + value)
            match = re.match("(.*?) is Unknown\n", line)
            if match:
                property = match.group(1)
                provide.send("add " + type + " " + property)
                provide.send("unset " + property)

            return True
        else:
            raise RuntimeError("context-listen terminated")

class Provide(Program):
    def __init__(self):
        Program.__init__(self, ["context-provide-internal"])

    def ready(self):
        line = self.readline()
        if line:
            print "PROVIDE:", line,
            return True
        else:
            raise RuntimeError("context-provide terminated")

class UserInput():
    def outfd(self):
        return stdin.fileno()

    def ready(self):
        line = self.readline()
        if line:
            match = re.match("(.*?) (.*)\n", line)
            command = match.group(1)
            return True
        else:
            exit(0)

class Select:
    def __init__(self, *tools):
        self.map = dict(map(lambda t: (t.outfd(), t), tools))
        self.rlist = map(lambda t: t.outfd(), tools)

    def select(self):
        ret = select(self.rlist, [], [])[0]
        for i in ret:
            stderr.flush()
            if not self.map[i].ready():
                self.rlist.remove(i)
                del self.map[i]

listen = Listen("test.a", "test.b")
provide = Provide()
provide.send("start")
s = Select(listen, provide)

while True:
    s.select()
    if not s.rlist:
        break