~ubuntu-branches/ubuntu/maverick/coherence/maverick

« back to all changes in this revision

Viewing changes to .pc/02_string_exception_fix/coherence/extern/galleryremote/gallery.py

  • Committer: Bazaar Package Importer
  • Author(s): Charlie Smotherman
  • Date: 2010-06-30 22:43:12 UTC
  • mfrom: (3.2.12 sid)
  • Revision ID: james.westby@ubuntu.com-20100630224312-0yfxnxrmtcjd0n2c
Tags: 0.6.6.2-5
* debian/rules made use of ${CURDIR} variable in remove statement.
* debian/install added README install statement. Closes: #572801
* Removed debian/coherence.docs as it was not installing README into 
  /usr/share/doc/python-coherence/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Jean-Michel Sizun <jm.sizun AT gmail>
 
2
#
 
3
# Copyright (C) 2008 Brent Woodruff
 
4
#   http://www.fprimex.com
 
5
#
 
6
# Copyright (C) 2004 John Sutherland <garion@twcny.rr.com>
 
7
#   http://garion.tzo.com/python/
 
8
#
 
9
# This library is free software; you can redistribute it and/or
 
10
# modify it under the terms of the GNU Lesser General Public
 
11
# License as published by the Free Software Foundation; either
 
12
# version 2.1 of the License, or (at your option) any later version.
 
13
#
 
14
# This library is distributed in the hope that it will be useful,
 
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
# Lesser General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU Lesser General Public
 
20
# License along with this library; if not, write to the Free Software
 
21
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
#
 
23
#
 
24
# Change log:
 
25
#
 
26
# 16-Nov-08 - Migrated from urllib to coherence infrastructure
 
27
#
 
28
# 04-Aug-08 - Added Gallery2 compatibility
 
29
#             Changed fetch_albums and fetch_albums_prune to return dicts
 
30
#             Added docstrings
 
31
#             Created package and registered with Pypi
 
32
#
 
33
# 09-Jun-04 - Removed self.cookie='' from _doRequest to allow multiple
 
34
#             transactions for each login.
 
35
#             Fixed cut paste error in newAlbum.
 
36
#             (both patches from Yuti Takhteyev
 
37
 
 
38
 
 
39
from coherence.upnp.core.utils import getPage
 
40
 
 
41
import StringIO
 
42
import string
 
43
 
 
44
class Gallery:
 
45
    """
 
46
    The Gallery class implements the Gallery Remote protocol as documented
 
47
    here:
 
48
    http://codex.gallery2.org/Gallery_Remote:Protocol
 
49
 
 
50
    The Gallery project is an open source web based photo album organizer
 
51
    written in php. Gallery's web site is:
 
52
    http://gallery.menalto.com/
 
53
 
 
54
    This class is a 3rd party product which is not maintained by the
 
55
    creators of the Gallery project.
 
56
 
 
57
    Example usage:
 
58
    from galleryremote import Gallery
 
59
    my_gallery = Gallery('http://www.yoursite.com/gallery2', 2)
 
60
    my_gallery.login('username','password')
 
61
    albums = my_gallery.fetch_albums()
 
62
    """
 
63
 
 
64
    def __init__(self, url, version=2):
 
65
        """
 
66
        Create a Gallery for remote access.
 
67
        url - base address of the gallery
 
68
        version - version of the gallery being connected to (default 2),
 
69
                  either 1 for Gallery1 or 2 for Gallery2
 
70
        """
 
71
        self.version = version # Gallery1 or Gallery2
 
72
        if version == 1:
 
73
            self.url = url + '/gallery_remote2.php'
 
74
        else:
 
75
            # default to G2
 
76
            self.url = url + '/main.php'
 
77
        self.auth_token = None
 
78
        self.logged_in = 0
 
79
        self.cookie = ''
 
80
        self.protocol_version = '2.5'
 
81
 
 
82
    def _do_request(self, request):
 
83
        """
 
84
        Send a request, encoded as described in the Gallery Remote protocol.
 
85
        request - a dictionary of protocol parameters and values
 
86
        """
 
87
        if self.auth_token != None:
 
88
            request['g2_authToken'] = self.auth_token
 
89
 
 
90
        url = self.url
 
91
        if (len(request) > 0) :
 
92
            url += '?'
 
93
            for key,value in request.iteritems():
 
94
                url += '%s=%s&' % (key,value)
 
95
        headers = None
 
96
        if self.cookie != '':
 
97
             headers = {'Cookie' : self.cookie}
 
98
 
 
99
        def gotPage(result):
 
100
            data,headers = result
 
101
            response = self._parse_response( data )
 
102
            if response['status'] != '0':
 
103
                raise response['status_text']
 
104
            try:
 
105
                self.auth_token = response['auth_token']
 
106
            except:
 
107
                pass
 
108
 
 
109
            if headers.has_key('set-cookie'):
 
110
                cookie_info = headers['set-cookie'][-1]
 
111
                self.cookie = cookie_info.split(';')[0]
 
112
 
 
113
            return response
 
114
 
 
115
        def gotError(error):
 
116
            print "Unable to process Gallery2 request: %s" % url
 
117
            print "Error: %s" % error
 
118
            return None
 
119
 
 
120
        d = getPage(url, headers=headers)
 
121
        d.addCallback(gotPage)
 
122
        d.addErrback(gotError)
 
123
        return d
 
124
 
 
125
 
 
126
    def _parse_response(self, response):
 
127
        """
 
128
        Decode the response from a request, returning a request dict
 
129
        response - The response from a gallery request, encoded according
 
130
                   to the gallery remote protocol
 
131
        """
 
132
        myStr = StringIO.StringIO(response)
 
133
 
 
134
        for line in myStr:
 
135
            if string.find( line, '#__GR2PROTO__' ) != -1:
 
136
                break
 
137
 
 
138
        # make sure the 1st line is #__GR2PROTO__
 
139
        if string.find( line, '#__GR2PROTO__' ) == -1:
 
140
            raise "Bad response: \r\n" + response
 
141
 
 
142
        resDict = {}
 
143
 
 
144
        for myS in myStr:
 
145
            myS = myS.strip()
 
146
            strList = string.split(myS, '=', 2)
 
147
 
 
148
            try:
 
149
                resDict[strList[0]] = strList[1]
 
150
            except:
 
151
                resDict[strList[0]] = ''
 
152
 
 
153
        return resDict
 
154
 
 
155
    def _get(self, response, kwd):
 
156
        """
 
157
        """
 
158
        try:
 
159
            retval = response[kwd]
 
160
        except:
 
161
            retval = ''
 
162
 
 
163
        return retval
 
164
 
 
165
    def login(self, username, password):
 
166
        """
 
167
        Establish an authenticated session to the remote gallery.
 
168
        username - A valid gallery user's username
 
169
        password - That valid user's password
 
170
        """
 
171
        if self.version == 1:
 
172
            request = {
 
173
                'protocol_version': self.protocol_version,
 
174
                'cmd': 'login',
 
175
                'uname': username,
 
176
                'password': password
 
177
            }
 
178
        else:
 
179
            request = {
 
180
                'g2_controller' : 'remote:GalleryRemote',
 
181
                'g2_form[protocol_version]' : self.protocol_version,
 
182
                'g2_form[cmd]' : 'login',
 
183
                'g2_form[uname]': username,
 
184
                'g2_form[password]': password
 
185
            }
 
186
 
 
187
        def gotPage(result):
 
188
            if result is None:
 
189
                print "Unable to login as %s to gallery2  server (%s)" % (username, self.url)
 
190
                return
 
191
            self.logged_in = 1
 
192
 
 
193
 
 
194
        d = self._do_request(request)
 
195
        d.addCallbacks(gotPage)
 
196
 
 
197
        return d
 
198
 
 
199
    def fetch_albums(self):
 
200
        """
 
201
        Obtain a dict of albums contained in the gallery keyed by
 
202
        album name. In Gallery1, the name is alphanumeric. In Gallery2,
 
203
        the name is the unique identifying number for that album.
 
204
        """
 
205
        if self.version == 1:
 
206
            request = {
 
207
                'protocol_version' : self.protocol_version,
 
208
                'cmd' : 'fetch-albums'
 
209
            }
 
210
        else:
 
211
            request = {
 
212
                'g2_controller' : 'remote:GalleryRemote',
 
213
                'g2_form[protocol_version]' : self.protocol_version,
 
214
                'g2_form[cmd]' : 'fetch-albums'
 
215
            }
 
216
 
 
217
        d = self._do_request(request)
 
218
 
 
219
        def gotResponse(response):
 
220
            if response is None:
 
221
                print "Unable to retrieve list of albums!"
 
222
                return None
 
223
 
 
224
            albums = {}
 
225
            for x in range(1, int(response['album_count']) + 1):
 
226
                album = {}
 
227
                album['name']                   = self._get(response,'album.name.' + str(x))
 
228
                album['title']                  = self._get(response,'album.title.' + str(x))
 
229
                album['summary']                = self._get(response,'album.summary.' + str(x))
 
230
                album['parent']                 = self._get(response,'album.parent.' + str(x))
 
231
                album['resize_size']            = self._get(response,'album.resize_size.' + str(x))
 
232
                album['perms.add']              = self._get(response,'album.perms.add.' + str(x))
 
233
                album['perms.write']            = self._get(response,'album.perms.write.' + str(x))
 
234
                album['perms.del_item']         = self._get(response,'album.perms.del_item.' + str(x))
 
235
                album['perms.del_alb']          = self._get(response,'album.perms.del_alb.' + str(x))
 
236
                album['perms.create_sub']       = self._get(response,'album.perms.create_sub.' + str(x))
 
237
                album['perms.info.extrafields'] = self._get(response,'album.info.extrafields' + str(x))
 
238
 
 
239
                albums[album['name']] = album
 
240
            return albums
 
241
 
 
242
        d.addCallback(gotResponse)
 
243
        return d
 
244
 
 
245
    def fetch_albums_prune(self):
 
246
        """
 
247
        Obtain a dict of albums contained in the gallery keyed by
 
248
        album name. In Gallery1, the name is alphanumeric. In Gallery2,
 
249
        the name is the unique identifying number for that album.
 
250
 
 
251
        From the protocol docs:
 
252
        "The fetch_albums_prune command asks the server to return a list
 
253
        of all albums that the user can either write to, or that are
 
254
        visible to the user and contain a sub-album that is writable
 
255
        (including sub-albums several times removed)."
 
256
        """
 
257
        if self.version == 1:
 
258
            request = {
 
259
                'protocol_version' : self.protocol_version,
 
260
                'cmd' : 'fetch-albums-prune'
 
261
            }
 
262
        else:
 
263
            request = {
 
264
                'g2_controller' : 'remote:GalleryRemote',
 
265
                'g2_form[protocol_version]' : self.protocol_version,
 
266
                'g2_form[cmd]' : 'fetch-albums-prune'
 
267
            }
 
268
 
 
269
        response = self._do_request(request)
 
270
 
 
271
        def gotResponse(response):
 
272
            # as long as it comes back here without an exception, we're ok.
 
273
            albums = {}
 
274
 
 
275
            for x in range(1, int(response['album_count']) + 1):
 
276
                album = {}
 
277
                album['name']                   = self._get(response,'album.name.' + str(x))
 
278
                album['title']                  = self._get(response,'album.title.' + str(x))
 
279
                album['summary']                = self._get(response,'album.summary.' + str(x))
 
280
                album['parent']                 = self._get(response,'album.parent.' + str(x))
 
281
                album['resize_size']            = self._get(response,'album.resize_size.' + str(x))
 
282
                album['perms.add']              = self._get(response,'album.perms.add.' + str(x))
 
283
                album['perms.write']            = self._get(response,'album.perms.write.' + str(x))
 
284
                album['perms.del_item']         = self._get(response,'album.perms.del_item.' + str(x))
 
285
                album['perms.del_alb']          = self._get(response,'album.perms.del_alb.' + str(x))
 
286
                album['perms.create_sub']       = self._get(response,'album.perms.create_sub.' + str(x))
 
287
                album['perms.info.extrafields'] = self._get(response,'album.info.extrafields' + str(x))
 
288
 
 
289
                albums[album['name']] = album
 
290
 
 
291
            return albums
 
292
 
 
293
        d.addCallback(gotResponse)
 
294
        return d
 
295
 
 
296
 
 
297
    def add_item(self, album, filename, caption, description):
 
298
        """
 
299
        Add a photo to the specified album.
 
300
        album - album name / identifier
 
301
        filename - image to upload
 
302
        caption - string caption to add to the image
 
303
        description - string description to add to the image
 
304
        """
 
305
        if self.version == 1:
 
306
            request = {
 
307
                'protocol_version' : self.protocol_version,
 
308
                'cmd' : 'add-item',
 
309
                'set_albumName' : album,
 
310
                'userfile' : file,
 
311
                'userfile_name' : filename,
 
312
                'caption' : caption,
 
313
                'extrafield.Description' : description
 
314
            }
 
315
        else:
 
316
            request = {
 
317
                'g2_form[protocol_version]' : self.protocol_version,
 
318
                'g2_form[cmd]' : 'add-item',
 
319
                'g2_form[set_albumName]' : album,
 
320
                'g2_form[userfile]' : file,
 
321
                'g2_form[userfile_name]' : filename,
 
322
                'g2_form[caption]' : caption,
 
323
                'g2_form[extrafield.Description]' : description
 
324
            }
 
325
 
 
326
        file = open(filename)
 
327
        d = self._do_request(request)
 
328
        # if we get here, everything went ok.
 
329
 
 
330
        return d
 
331
 
 
332
    def album_properties(self, album):
 
333
        """
 
334
        Obtain album property information for the specified album.
 
335
        album - the album name / identifier to obtain information for
 
336
        """
 
337
        if self.version == 1:
 
338
            request = {
 
339
                'protocol_version' : self.protocol_version,
 
340
                'cmd' : 'album-properties',
 
341
                'set_albumName' : album
 
342
            }
 
343
        else:
 
344
            request = {
 
345
                'g2_controller' : 'remote:GalleryRemote',
 
346
                'g2_form[protocol_version]' : self.protocol_version,
 
347
                'g2_form[cmd]' : 'album-properties',
 
348
                'g2_form[set_albumName]' : album
 
349
            }
 
350
 
 
351
        d = self._do_request(request)
 
352
 
 
353
        def gotResponse(response):
 
354
            res_dict = {}
 
355
 
 
356
            if response.has_key('auto_resize'):
 
357
                res_dict['auto_resize'] = response['auto_resize']
 
358
            if response.has_key('add_to_beginning'):
 
359
                res_dict['add_to_beginning'] = response['add_to_beginning']
 
360
 
 
361
            return res_dict
 
362
 
 
363
        d.addCallback(gotResponse)
 
364
        return d
 
365
 
 
366
 
 
367
    def new_album(self, parent, name=None, title=None, description=None):
 
368
        """
 
369
        Add an album to the specified parent album.
 
370
        parent - album name / identifier to contain the new album
 
371
        name - unique string name of the new album
 
372
        title - string title of the album
 
373
        description - string description to add to the image
 
374
        """
 
375
        if self.version == 1:
 
376
            request = {
 
377
                'g2_controller' : 'remote:GalleryRemote',
 
378
                'protocol_version' : self.protocol_version,
 
379
                'cmd' : 'new-album',
 
380
                'set_albumName' : parent
 
381
            }
 
382
            if name != None:
 
383
                request['newAlbumName'] = name
 
384
            if title != None:
 
385
                request['newAlbumTitle'] = title
 
386
            if description != None:
 
387
                request['newAlbumDesc'] = description
 
388
        else:
 
389
            request = {
 
390
                'g2_controller' : 'remote:GalleryRemote',
 
391
                'g2_form[protocol_version]' : self.protocol_version,
 
392
                'g2_form[cmd]' : 'new-album',
 
393
                'g2_form[set_albumName]' : parent
 
394
            }
 
395
            if name != None:
 
396
                request['g2_form[newAlbumName]'] = name
 
397
            if title != None:
 
398
                request['g2_form[newAlbumTitle]'] = title
 
399
            if description != None:
 
400
                request['g2_form[newAlbumDesc]'] = description
 
401
 
 
402
        d = self._do_request(request)
 
403
 
 
404
        def gotResponse(response):
 
405
            return response['album_name']
 
406
 
 
407
        d.addCallback(d)
 
408
        return d
 
409
 
 
410
 
 
411
    def fetch_album_images(self, album):
 
412
        """
 
413
        Get the image information for all images in the specified album.
 
414
        album - specifies the album from which to obtain image information
 
415
        """
 
416
        if self.version == 1:
 
417
            request = {
 
418
                'protocol_version' : self.protocol_version,
 
419
                'cmd' : 'fetch-album-images',
 
420
                'set_albumName' : album,
 
421
                'albums_too' : 'no',
 
422
                'extrafields' : 'yes'
 
423
            }
 
424
        else:
 
425
            request = {
 
426
                'g2_controller' : 'remote:GalleryRemote',
 
427
                'g2_form[protocol_version]' : self.protocol_version,
 
428
                'g2_form[cmd]' : 'fetch-album-images',
 
429
                'g2_form[set_albumName]' : album,
 
430
                'g2_form[albums_too]' : 'no',
 
431
                'g2_form[extrafields]' : 'yes'
 
432
            }
 
433
 
 
434
        d = self._do_request(request)
 
435
 
 
436
        def gotResponse (response):
 
437
            if response is None:
 
438
                print "Unable to retrieve list of item for album %s." % album
 
439
                return None
 
440
 
 
441
            images = []
 
442
 
 
443
            for x in range(1, int(response['image_count']) + 1):
 
444
                image = {}
 
445
                image['name']                = self._get(response, 'image.name.' + str(x))
 
446
                image['title']               = self._get(response, 'image.title.' + str(x))
 
447
                image['raw_width']           = self._get(response, 'image.raw_width.' + str(x))
 
448
                image['raw_height']          = self._get(response, 'image.raw_height.' + str(x))
 
449
                image['resizedName']         = self._get(response, 'image.resizedName.' + str(x))
 
450
                image['resized_width']       = self._get(response, 'image.resized_width.' + str(x))
 
451
                image['resized_height']      = self._get(response, 'image.resized_height.' + str(x))
 
452
                image['thumbName']           = self._get(response, 'image.thumbName.' + str(x))
 
453
                image['thumb_width']         = self._get(response, 'image.thumb_width.' + str(x))
 
454
                image['thumb_height']        = self._get(response, 'image.thumb_height.' + str(x))
 
455
                image['raw_filesize']        = self._get(response, 'image.raw_filesize.' + str(x))
 
456
                image['caption']             = self._get(response, 'image.caption.' + str(x))
 
457
                image['clicks']              = self._get(response, 'image.clicks.' + str(x))
 
458
                image['capturedate.year']    = self._get(response, 'image.capturedate.year' + str(x))
 
459
                image['capturedate.mon']     = self._get(response, 'image.capturedate.mon' + str(x))
 
460
                image['capturedate.mday']    = self._get(response, 'image.capturedate.mday' + str(x))
 
461
                image['capturedate.hours']   = self._get(response, 'image.capturedate.hours' + str(x))
 
462
                image['capturedate.minutes'] = self._get(response, 'image.capturedate.minutes' + str(x))
 
463
                image['capturedate.seconds'] = self._get(response, 'image.capturedate.seconds' + str(x))
 
464
                image['description']         = self._get(response, 'image.extrafield.Description.' + str(x))
 
465
 
 
466
                images.append(image)
 
467
 
 
468
            return images
 
469
 
 
470
        d.addCallback(gotResponse)
 
471
        return d
 
472
 
 
473
 
 
474
    def get_URL_for_image(self, gallery2_id):
 
475
        url = '%s/main.php?g2_view=core.DownloadItem&g2_itemId=%s' % (self.url, gallery2_id)
 
476
        return url
 
 
b'\\ No newline at end of file'