~cairo-dock-team/cairo-dock-plug-ins-extras/3.2.x

« back to all changes in this revision

Viewing changes to Transmission/Transmission

  • Committer: Fabrice Rey
  • Date: 2013-02-28 00:04:13 UTC
  • Revision ID: fabounet03@gmail.com-20130228000413-w06f2q0sisckq7e6
changed a few import to lower the number of changes between python 2 & 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from gobject import timeout_add
32
32
from CDApplet import CDApplet, _
33
33
 
34
 
try:
 
34
try: # python3
35
35
        import http.client as httplib, urllib.request, urllib.error
36
 
        def urllib2Request():
37
 
                return urllib.request
38
 
        def urllib2Error():
39
 
                return urllib.error
40
 
except:
 
36
        urllib_request = urllib.request
 
37
        urllib_error = urllib.error
 
38
except: # python2
41
39
        import httplib, urllib2
42
 
        def urllib2Request():
43
 
                return urllib2
44
 
        def urllib2Error():
45
 
                return urllib2
 
40
        urllib_request = urllib2
 
41
        urllib_error = urllib2
46
42
 
47
43
TR_STATUS_CHECK_WAIT   = (1<<0)
48
44
TR_STATUS_CHECK        = (1<<1)
106
102
        # The headers will contain our session ID.
107
103
        def get_session_id(self):
108
104
                query = json.dumps({'method': 'session-get', 'arguments': {}})
109
 
                request = urllib2Request().Request(self.url, query, {})
 
105
                request = urllib_request.Request(self.url, query, {})
110
106
                try:
111
 
                        response = urllib2Request().urlopen(request, timeout=30)
112
 
                except urllib2Error().HTTPError as error:
 
107
                        response = urllib_request.urlopen(request, timeout=30)
 
108
                except urllib_error.HTTPError as error:
113
109
                        #~ aerror_data = error.read()
114
110
                        if error.code == 409:
115
111
                                self.iSessionID = error.headers['X-Transmission-Session-Id']
214
210
        def _http_query(self, query):
215
211
                headers = {'x-transmission-session-id': self.iSessionID}
216
212
                try:
217
 
                        request = urllib2Request().Request(self.url, query, headers)
218
 
                        response = urllib2Request().urlopen(request, timeout=20)
 
213
                        request = urllib_request.Request(self.url, query, headers)
 
214
                        response = urllib_request.urlopen(request, timeout=20)
219
215
                        return response.read()
220
216
                except urllib2Error.HTTPError as error:
221
217
                        print("HTTP error:",error.filename, error.code, error.msg)
415
411
                
416
412
                # set authentication
417
413
                if self.config['user'] and self.config['password']:
418
 
                        password_manager = urllib2Request().HTTPPasswordMgrWithDefaultRealm()
 
414
                        password_manager = urllib_request.HTTPPasswordMgrWithDefaultRealm()
419
415
                        password_manager.add_password(realm=None, uri=self.url, user=self.config['user'], passwd=self.config['password'])
420
 
                        opener = urllib2Request().build_opener(
421
 
                                urllib2Request().HTTPBasicAuthHandler(password_manager),
422
 
                                urllib2Request().HTTPDigestAuthHandler(password_manager) )
423
 
                        urllib2Request().install_opener(opener)
 
416
                        opener = urllib_request.build_opener(
 
417
                                urllib_request.HTTPBasicAuthHandler(password_manager),
 
418
                                urllib_request.HTTPDigestAuthHandler(password_manager) )
 
419
                        urllib_request.install_opener(opener)
424
420
                
425
421
                # start the connection timer.
426
422
                if self.iSidTryConnect == 0: