lkcl / pyjamas-desktop

Google's Webkit, ported to Python, ported to Desktops.

This URL has Read+Write access

pyjamas-desktop / pyjamas-web / examples / jsonrpc / JSONRPCExample.py.orig
100644 130 lines (101 sloc) 4.206 kb
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
from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox
from JSONService import JSONProxy
from Timer import Timer
 
class JsonHelloButtonHandler:
def __init__(self, example):
self.example = example
 
def onClick(self, sender):
self.example.text_area.setText("sending request...")
username = self.example.login_text_area.getText()
self.example.remote_py.get_messages(username, self)
 
def onRemoteResponse(self, response, request_info):
txt = ''
for msg in response:
txt += msg.get("time") + " "
txt += msg.get("user_name") + ": "
txt += msg.get("message") + '\n'
 
self.example.text_area.setText(txt)
 
def onRemoteError(self, code, message, request_info):
self.example.status.setHTML("error: "+message)
 
class JSONRPCExample:
def onModuleLoad(self):
self.TEXT_WAITING = "Waiting for response..."
self.TEXT_ERROR = "Server Error"
self.METHOD_LOGIN = "Login"
self.METHOD_POST_MSG = "POST_MSG"
self.METHOD_GET_MSGS = "GET_MSGS"
self.METHOD_LOWERCASE = "lowercase"
self.methods = [self.METHOD_LOGIN, self.METHOD_POST_MSG, self.METHOD_GET_MSGS, self.METHOD_LOWERCASE]
 
self.remote_py = EchoServicePython()
 
self.status=Label()
self.login_text_area = TextArea()
self.login_text_area.setText(r"lkcl")
self.login_text_area.setCharacterWidth(20)
self.login_text_area.setVisibleLines(1)
 
self.input_area = TextArea()
self.input_area.setCharacterWidth(80)
self.input_area.setVisibleLines(2)
 
self.text_area = TextArea()
self.text_area.setText()
self.text_area.setCharacterWidth(80)
self.text_area.setVisibleLines(8)
 
self.method_list = ListBox()
self.method_list.setVisibleItemCount(1)
for method in self.methods:
self.method_list.addItem(method)
self.method_list.setSelectedIndex(0)
 
method_panel = HorizontalPanel()
method_panel.add(HTML("Remote string method to call: "))
method_panel.add(self.method_list)
method_panel.setSpacing(8)
 
self.button_py = Button("Send to Python Service", self)
self.button_handler = JsonHelloButtonHandler(self)
self.button_msg = Button("Send Message", self.button_handler)
 
buttons = HorizontalPanel()
buttons.add(self.button_py)
buttons.add(self.button_msg)
buttons.setSpacing(8)
 
info = r"<h2>JSON-RPC Example</h2><p>This example demonstrates the calling of server services with <a href=\"http://json-rpc.org/\">JSON-RPC</a>."
info += "<p>Enter some text below, and press a button to send the text to an Echo service on your server. An echo service simply sends the exact same text back that it receives."
 
panel = VerticalPanel()
panel.add(HTML(info))
panel.add(self.login_text_area)
panel.add(self.input_area)
panel.add(self.text_area)
panel.add(method_panel)
panel.add(buttons)
panel.add(self.status)
 
RootPanel().add(panel)
 
self.isActive = True
self.onTimer()
 
def onTimer(self):
if not self.isActive:
return
 
Timer(5000, self)
 
self.button_handler.onClick(self)
 
def onClick(self, sender):
self.status.setText(self.TEXT_WAITING)
method = self.methods[self.method_list.getSelectedIndex()]
text = self.input_area.getText()
username = self.login_text_area.getText()
 
# demonstrate proxy & callMethod()
if sender == self.button_py:
if method == self.METHOD_LOGIN:
id = self.remote_py.login(username, self)
elif method == self.METHOD_POST_MSG:
id = self.remote_py.post_message(username, text, self)
self.input_area.setText("")
elif method == self.METHOD_GET_MSGS:
id = self.remote_py.get_messages(username, self)
elif method == self.METHOD_LOWERCASE:
id = self.remote_py.lowercase(text, self)
if id<0:
self.status.setText(self.TEXT_ERROR)
 
def onRemoteResponse(self, response, request_info):
self.status.setText(response)
 
def onRemoteError(self, code, message, request_info):
self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)
 
 
class EchoServicePython(JSONProxy):
    def __init__(self):
JSONProxy.__init__(self, "services/EchoService.py", ["echo", "reverse", "uppercase", "lowercase"])
 
        #JSONProxy.__init__(self, "chatservice/", ["login", "post_message", "get_messages", "lowercase"])