~mmcg069/software-center/star-tweaks

« back to all changes in this revision

Viewing changes to softwarecenter/backend/weblive_pristine.py

  • Committer: Matthew McGowan
  • Date: 2011-03-23 10:45:21 UTC
  • mfrom: (1556.1.62 trunk)
  • Revision ID: matthew.joseph.mcgowan@gmail.com-20110323104521-0lysrjm8kudev4nb
merge w trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Copyright (C) 2010-2011 Stephane Graber <stgraber@ubuntu.com>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
17
#
 
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
 
20
 
 
21
import urllib, urllib2, json
 
22
 
 
23
class WebLiveJsonError(Exception):
 
24
    def __init__(self, value):
 
25
        self.value = value
 
26
 
 
27
    def __str__(self):
 
28
        return repr(self.value)
 
29
 
 
30
class WebLiveError(Exception):
 
31
    def __init__(self, value):
 
32
        self.value = value
 
33
 
 
34
    def __str__(self):
 
35
        return repr(self.value)
 
36
 
 
37
class WebLiveLocale(object):
 
38
    def __init__(self, locale, description):
 
39
        self.locale = locale
 
40
        self.description = description
 
41
 
 
42
class WebLivePackage(object):
 
43
    def __init__(self, pkgname, version):
 
44
        self.pkgname = pkgname
 
45
        self.version = version
 
46
 
 
47
class WebLiveServer(object):
 
48
    def __init__(self, name, title, description, timelimit, userlimit, users):
 
49
        self.name = name
 
50
        self.title = title
 
51
        self.description = description
 
52
        self.timelimit = timelimit
 
53
        self.userlimit = userlimit
 
54
        self.current_users = users
 
55
 
 
56
    def __repr__(self):
 
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)
 
59
 
 
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]
 
64
 
 
65
        WebLiveServer.__init__(self, name, title, description, timelimit, userlimit, users)
 
66
 
 
67
    def __repr__(self):
 
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))
 
70
 
 
71
class WebLive:
 
72
    def __init__(self,url,as_object=False):
 
73
        self.url=url
 
74
        self.as_object=as_object
 
75
 
 
76
    def do_query(self,query):
 
77
        page=urllib2.Request(self.url,urllib.urlencode({'query':json.dumps(query)}))
 
78
 
 
79
        try:
 
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)
 
85
 
 
86
        try:
 
87
            reply=json.loads(response.read())
 
88
        except ValueError:
 
89
            raise WebLiveJsonError("Returned json object is invalid.")
 
90
 
 
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'])
 
98
            else:
 
99
                raise WebLiveJsonError("Unknown error code: %s" % reply['message'])
 
100
 
 
101
        if 'message' not in reply:
 
102
            raise WebLiveJsonError("Invalid json reply")
 
103
 
 
104
        return reply
 
105
 
 
106
    def create_user(self,serverid,username,fullname,password,session="desktop"):
 
107
        query={}
 
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)
 
115
 
 
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)
 
129
            else:
 
130
                raise WebLiveError("Unknown error code: %s" % reply['message'])
 
131
 
 
132
        return reply['message']
 
133
 
 
134
    def list_everything(self):
 
135
        query={}
 
136
        query['action']='list_everything'
 
137
        reply=self.do_query(query)
 
138
 
 
139
        if type(reply['message']) != type({}):
 
140
            raise WebLiveError("Invalid value, expected '%s' and got '%s'."
 
141
                % (type({}),type(reply['message'])))
 
142
 
 
143
        if not self.as_object:
 
144
            return reply['message']
 
145
        else:
 
146
            servers=[]
 
147
            for server in reply['message']:
 
148
                attr=reply['message'][server]
 
149
                servers.append(WebLiveEverythingServer(
 
150
                    server,
 
151
                    attr['title'],
 
152
                    attr['description'],
 
153
                    attr['timelimit'],
 
154
                    attr['userlimit'],
 
155
                    attr['users'],
 
156
                    attr['locales'],
 
157
                    attr['packages']))
 
158
            return servers
 
159
 
 
160
    def list_locales(self,serverid):
 
161
        query={}
 
162
        query['action']='list_locales'
 
163
        query['serverid']=serverid
 
164
        reply=self.do_query(query)
 
165
 
 
166
        if type(reply['message']) != type([]):
 
167
            raise WebLiveError("Invalid value, expected '%s' and got '%s'."
 
168
                % (type({}),type(reply['message'])))
 
169
 
 
170
        if not self.as_object:
 
171
            return reply['message']
 
172
        else:
 
173
            return [WebLiveLocale(x[0], x[1]) for x in reply['message']]
 
174
 
 
175
    def list_packages(self,serverid):
 
176
        query={}
 
177
        query['action']='list_packages'
 
178
        query['serverid']=serverid
 
179
        reply=self.do_query(query)
 
180
 
 
181
        if type(reply['message']) != type([]):
 
182
            raise WebLiveError("Invalid value, expected '%s' and got '%s'."
 
183
                % (type({}),type(reply['message'])))
 
184
 
 
185
        if not self.as_object:
 
186
            return reply['message']
 
187
        else:
 
188
            return [WebLivePackage(x[0], x[1]) for x in reply['message']]
 
189
 
 
190
    def list_servers(self):
 
191
        query={}
 
192
        query['action']='list_servers'
 
193
        reply=self.do_query(query)
 
194
 
 
195
        if type(reply['message']) != type({}):
 
196
            raise WebLiveError("Invalid value, expected '%s' and got '%s'."
 
197
                % (type({}),type(reply['message'])))
 
198
 
 
199
        if not self.as_object:
 
200
            return reply['message']
 
201
        else:
 
202
            servers=[]
 
203
            for server in reply['message']:
 
204
                attr=reply['message'][server]
 
205
                servers.append(WebLiveServer(
 
206
                    server,
 
207
                    attr['title'],
 
208
                    attr['description'],
 
209
                    attr['timelimit'],
 
210
                    attr['userlimit'],
 
211
                    attr['users']))
 
212
            return servers