Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 953fdf8ece222c9c3ea6ee7d40ca41c91fe94c7f (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014, 2016 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

from .. import expressions
from ... import channel
from ...channel.Command import Command


class ExpressionsProxy(expressions.ExpressionsService):
    def __init__(self, channel):
        self.channel = channel
        self.listeners = {}

    def assign(self, contextID, value, done):
        done = self._makeCallback(done)
        service = self
        value = bytearray(value)

        class AssignCommand(Command):
            def __init__(self):
                super(AssignCommand, self).__init__(service.channel, service,
                                                    "assign",
                                                    (contextID, value))

            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneAssign(self.token, error)
        return AssignCommand().token

    def create(self, parent_id, language, expression, done):
        done = self._makeCallback(done)
        service = self

        class CreateCommand(Command):
            def __init__(self):
                super(CreateCommand, self).__init__(service.channel, service,
                                                    "create",
                                                    (parent_id, language,
                                                     expression))

            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]:
                        ctx = expressions.Expression(args[1])
                done.doneCreate(self.token, error, ctx)
        return CreateCommand().token

    def dispose(self, contextID, done):
        done = self._makeCallback(done)
        service = self

        class DisposeCommand(Command):
            def __init__(self):
                super(DisposeCommand, self).__init__(service.channel, service,
                                                     "dispose", (contextID,))

            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneDispose(self.token, error)
        return DisposeCommand().token

    def evaluate(self, contextID, done):
        done = self._makeCallback(done)
        service = self

        class EvalCommand(Command):
            def __init__(self):
                super(EvalCommand, self).__init__(service.channel, service,
                                                  "evaluate", (contextID,))

            def done(self, error, args):
                value = None
                props = {}
                if not error:
                    assert len(args) == 3
                    value = channel.toByteArray(args[0])
                    error = self.toError(args[1])
                    props = args[2]
                done.doneEvaluate(self.token, error,
                                  expressions.Value(value, props))
        return EvalCommand().token

    def getChildren(self, parent_context_id, done):
        done = self._makeCallback(done)
        service = self

        class GetChildrenCommand(Command):
            def __init__(self):
                super(GetChildrenCommand, self).__init__(service.channel,
                                                         service,
                                                         "getChildren",
                                                         (parent_context_id,))

            def done(self, error, args):
                contexts = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    contexts = args[1]
                done.doneGetChildren(self.token, error, contexts)
        return GetChildrenCommand().token

    def getContext(self, contextID, done):
        done = self._makeCallback(done)
        service = self

        class GetContextCommand(Command):
            def __init__(self):
                super(GetContextCommand, self).__init__(service.channel,
                                                        service, "getContext",
                                                        (contextID,))

            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]:
                        ctx = expressions.Expression(args[1])
                done.doneGetContext(self.token, error, ctx)
        return GetContextCommand().token

    def addListener(self, listener):
        l = ChannelEventListener(self, listener)
        self.channel.addEventListener(self, l)
        self.listeners[listener] = l

    def removeListener(self, listener):
        l = self.listeners.get(listener)
        if l:
            del self.listeners[listener]
            self.channel.removeEventListener(self, l)


class ChannelEventListener(channel.EventListener):
    def __init__(self, service, listener):
        self.service = service
        self.listener = listener

    def event(self, name, data):
        try:
            args = channel.fromJSONSequence(data)
            if name == "valueChanged":
                assert len(args) == 1
                self.listener.valueChanged(args[0])
            else:
                raise IOError("Expressions service: unknown event: " + name)
        except Exception as x:
            import sys
            x.tb = sys.exc_info()[2]
            self.service.channel.terminate(x)

Back to the top