Merge pull request #5 from cuihantao/master
[pylon.git] / contrib / pylon_client.py
blob5cda118a2be47f03f6fd25ea1c5c41880861d16f
1 # Copyright (C) 2010 Richard Lincoln
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """ Pyjamas frontend for Pylon. """
17 import pyjd # dummy in pyjs
18 from pyjamas.ui.RootPanel import RootPanel
19 from pyjamas.ui.Label import Label
20 from pyjamas.ui.Button import Button
21 from pyjamas.ui.HTML import HTML
22 from pyjamas.ui.ListBox import ListBox
23 from pyjamas.ui.Tree import Tree
24 from pyjamas.ui.TreeItem import TreeItem
25 from pyjamas.ui.VerticalPanel import VerticalPanel
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
27 from pyjamas.ui.FormPanel import FormPanel
28 from pyjamas.ui.FileUpload import FileUpload
29 from pyjamas.ui.TabPanel import TabPanel
30 from pyjamas.ui.decoratorpanel import DecoratedTabPanel, DecoratorPanel
31 from pyjamas.ui.decoratorpanel import DecoratorTitledPanel
32 from pyjamas import Window
33 from pyjamas.JSONService import JSONProxy
36 class Pylon:
37 def __init__(self):
38 self.remote_case = CaseService()
40 self.base_panel = VerticalPanel()
41 self.tab_panel = TabPanel()
42 self.base_panel.add(self.tab_panel)
43 self.tab_panel.add(self.get_case_panel(), "Case")
44 self.tab_panel.add(self.get_buses_panel(), "Buses")
45 self.tab_panel.selectTab(0)
46 self.status = Label()
47 self.base_panel.add(self.status)
48 RootPanel().add(self.base_panel)
51 def get_case_panel(self):
52 panel = VerticalPanel()
53 title = HTML("""Case""")
54 panel.add(title)
55 tree = self.get_case_tree()
56 panel.add(tree)
57 return panel
60 def get_case_tree(self):
61 tree = self.tree = Tree()
62 case_item = TreeItem("Case_1")
63 tree.addItem(case_item)
64 buses = self.buses = TreeItem("Buses")
65 case_item.addItem(buses)
66 id = self.remote_case.buses("name", self)
67 return tree
70 def create_item(self, proto):
71 proto.item = TreeItem(proto.text)
72 proto.item.setUserObject(proto)
73 if len(proto.children) > 0:
74 proto.item.addItem(PendingItem())
78 def get_buses_panel(self):
79 panel = VerticalPanel()
80 title = HTML("""Buses""")
81 panel.add(title)
82 return panel
84 # JSON handler interface --------------------------------------------------
86 # The handler object should implement
87 # onRemoteResponse(value, requestInfo)
88 # to accept the return value of the remote method, and
89 # onRemoteError(code, error_dict, requestInfo)
90 # code = http-code or 0
91 # error_dict is an jsonrpc 2.0 error dict:
92 # {
93 # 'code': jsonrpc-error-code (integer) ,
94 # 'message': jsonrpc-error-message (string) ,
95 # 'data' : extra-error-data
96 # }
97 # to handle errors.
99 def onRemoteResponse(self, response, request_info):
100 print "RESPONSE:", response
101 # self.tree.clear()
102 self.buses.removeItems()
103 for bus_name in response:
104 proto = Proto(str(bus_name))
105 self.create_item(proto)
106 self.buses.addItem(proto.item)
109 def onRemoteError(self, code, errobj, request_info):
110 print "ERROR:", errobj['message']
112 message = errobj['message']
113 if code != 0:
114 self.status.setText("HTTP error %d: %s" % (code, message))
115 else:
116 code = errobj['code']
117 self.status.setText("JSONRPC Error %s: %s" % (code, message))
120 class Proto:
121 def __init__(self, text, children=None):
122 self.children = []
123 self.item = None
124 self.text = text
126 if children is not None:
127 self.children = children
130 class PendingItem(TreeItem):
131 def __init__(self):
132 TreeItem.__init__(self, "Please wait...")
134 def isPendingItem(self):
135 return True
138 class CaseService(JSONProxy):
139 def __init__(self):
140 JSONProxy.__init__(self, "/json", ["buses"])
143 if __name__ == "__main__":
144 # For pyjd, set up a web server and load the HTML from there.
145 pyjd.setup("http://0.0.0.0:8080/static/pylon.html")
146 # pyjd.setup("public/pylon.html")
147 app = Pylon()
148 pyjd.run() # dummy in pyjs
150 # EOF -------------------------------------------------------------------------