2
# Copyright (C) 2010-2011 Stephane Graber <stgraber@ubuntu.com>
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
# taken from lp:~weblive-dev/weblive/ltsp-cluster-agent-weblive/client/weblive.py
19
# and put into weblive_pristine.py until a ubuntu package is in main
21
import urllib, urllib2, json
23
class WebLiveJsonError(Exception):
24
def __init__(self, value):
28
return repr(self.value)
30
class WebLiveError(Exception):
31
def __init__(self, value):
35
return repr(self.value)
37
class WebLiveLocale(object):
38
def __init__(self, locale, description):
40
self.description = description
42
class WebLivePackage(object):
43
def __init__(self, pkgname, version):
44
self.pkgname = pkgname
45
self.version = version
47
class WebLiveServer(object):
48
def __init__(self, name, title, description, timelimit, userlimit, users):
51
self.description = description
52
self.timelimit = timelimit
53
self.userlimit = userlimit
54
self.current_users = users
57
return "[WebLiveServer: %s (%s - %s), timelimit=%s, userlimit=%s, current_users=%s" % (
58
self.name, self.title, self.description, self.timelimit, self.userlimit, self.current_users)
60
class WebLiveEverythingServer(WebLiveServer):
61
def __init__(self, name, title, description, timelimit, userlimit, users, locales, packages):
62
self.locales = [WebLiveLocale(x[0], x[1]) for x in locales]
63
self.packages = [WebLivePackage(x[0], x[1]) for x in packages]
65
WebLiveServer.__init__(self, name, title, description, timelimit, userlimit, users)
68
return "[WebLiveServer: %s (%s - %s), timelimit=%s, userlimit=%s, current_users=%s, nr_locales=%s, nr_pkgs=%s" % (
69
self.name, self.title, self.description, self.timelimit, self.userlimit, self.current_users, len(self.locales), len(self.packages))
72
def __init__(self,url,as_object=False):
74
self.as_object=as_object
76
def do_query(self,query):
77
page=urllib2.Request(self.url,urllib.urlencode({'query':json.dumps(query)}))
80
response=urllib2.urlopen(page)
81
except urllib2.HTTPError, e:
82
raise WebLiveJsonError("HTTP return code: %s" % e.code)
83
except urllib2.URLError, e:
84
raise WebLiveJsonError("Failed to reach server: %s" % e.reason)
87
reply=json.loads(response.read())
89
raise WebLiveJsonError("Returned json object is invalid.")
91
if reply['status'] != 'ok':
92
if reply['message'] == -1:
93
raise WebliveJsonError("Missing 'action' field in query.")
94
elif reply['message'] == -2:
95
raise WebLiveJsonError("Missing parameter")
96
elif reply['message'] == -3:
97
raise WebliveJsonError("Function '%s' isn't exported over JSON." % query['action'])
99
raise WebLiveJsonError("Unknown error code: %s" % reply['message'])
101
if 'message' not in reply:
102
raise WebLiveJsonError("Invalid json reply")
106
def create_user(self,serverid,username,fullname,password,session="desktop"):
108
query['action']='create_user'
109
query['serverid']=serverid
110
query['username']=username
111
query['fullname']=fullname
112
query['password']=password
113
query['session']=session
114
reply=self.do_query(query)
116
if type(reply['message']) != type([]):
117
if reply['message'] == 1:
118
raise WebLiveError("Reached user limit, return false.")
119
elif reply['message'] == 2:
120
raise WebLiveError("Different user with same username already exists.")
121
elif reply['message'] == 3:
122
raise WebLiveError("Invalid fullname, must only contain alphanumeric characters and spaces.")
123
elif reply['message'] == 4:
124
raise WebLiveError("Invalid login, must only contain lowercase letters.")
125
elif reply['message'] == 5:
126
raise WebLiveError("Invalid password, must contain only alphanumeric characters.")
127
elif reply['message'] == 7:
128
raise WebLiveError("Invalid server: %s" % serverid)
130
raise WebLiveError("Unknown error code: %s" % reply['message'])
132
return reply['message']
134
def list_everything(self):
136
query['action']='list_everything'
137
reply=self.do_query(query)
139
if type(reply['message']) != type({}):
140
raise WebLiveError("Invalid value, expected '%s' and got '%s'."
141
% (type({}),type(reply['message'])))
143
if not self.as_object:
144
return reply['message']
147
for server in reply['message']:
148
attr=reply['message'][server]
149
servers.append(WebLiveEverythingServer(
160
def list_locales(self,serverid):
162
query['action']='list_locales'
163
query['serverid']=serverid
164
reply=self.do_query(query)
166
if type(reply['message']) != type([]):
167
raise WebLiveError("Invalid value, expected '%s' and got '%s'."
168
% (type({}),type(reply['message'])))
170
if not self.as_object:
171
return reply['message']
173
return [WebLiveLocale(x[0], x[1]) for x in reply['message']]
175
def list_packages(self,serverid):
177
query['action']='list_packages'
178
query['serverid']=serverid
179
reply=self.do_query(query)
181
if type(reply['message']) != type([]):
182
raise WebLiveError("Invalid value, expected '%s' and got '%s'."
183
% (type({}),type(reply['message'])))
185
if not self.as_object:
186
return reply['message']
188
return [WebLivePackage(x[0], x[1]) for x in reply['message']]
190
def list_servers(self):
192
query['action']='list_servers'
193
reply=self.do_query(query)
195
if type(reply['message']) != type({}):
196
raise WebLiveError("Invalid value, expected '%s' and got '%s'."
197
% (type({}),type(reply['message'])))
199
if not self.as_object:
200
return reply['message']
203
for server in reply['message']:
204
attr=reply['message'][server]
205
servers.append(WebLiveServer(