Skip to content

Commit 3660231

Browse files
committed
Updates to support Python 2.4 and 2.5
1 parent f86909a commit 3660231

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

ox3apiclient/__init__.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22

33
import ConfigParser
44
import cookielib
5-
import json
5+
6+
# json module is not supported in versions of Python < 2.6 so try to load the
7+
# simplejson module instead. Note that as of simplejson v2.1.1, Python 2.4
8+
# support was dropped. You will need to look for v2.1.0 specifically for
9+
# Python 2.4 support.
10+
try:
11+
import json
12+
except ImportError:
13+
import simplejson as json
14+
615
import oauth2 as oauth
716
import urllib
817
import urllib2
18+
19+
# parse_qs is in the urlparse module as of 2.6, but in cgi in earlier versions.
20+
try:
21+
from urlparse import parse_qs
22+
except ImportError:
23+
from cgi import parse_qs
24+
925
import urlparse
1026

1127
__version__ = '0.1.0'
@@ -107,8 +123,8 @@ def request(self, url, method='GET', headers={}, data=None, sign=False):
107123
# Since we are using a urllib2.Request object we need to assign a value
108124
# other than None to "data" in order to make the request a POST request,
109125
# even if there is no data to post.
110-
if method == 'POST':
111-
data = data if data else ''
126+
if method == 'POST' and not data:
127+
data = ''
112128

113129
req = urllib2.Request(url, headers=headers, data=data)
114130

@@ -140,8 +156,11 @@ def authorize_token(self, email=None, password=None):
140156
# Give precedence to credentials passed in methods calls over those set
141157
# in the instance. This allows you to override user creds that may have
142158
# been loaded from a file.
143-
email = email if email else self._email
144-
password = password if password else self._password
159+
if not email:
160+
email = self._email
161+
162+
if not password:
163+
password = self._password
145164

146165
if not email or not password:
147166
self._email = self._password = None
@@ -161,7 +180,7 @@ def authorize_token(self, email=None, password=None):
161180
# Clear user credentials.
162181
self._email = self._password = None
163182

164-
verifier = urlparse.parse_qs(res.read())['oauth_verifier'][0]
183+
verifier = parse_qs(res.read())['oauth_verifier'][0]
165184
self._token.set_verifier(verifier)
166185

167186
def fetch_access_token(self):
@@ -229,10 +248,17 @@ def logoff(self):
229248
def _resolve_url(self, url):
230249
""""""
231250
parse_res = urlparse.urlparse(url)
232-
if not parse_res.scheme:
251+
252+
# 2.4 returns a tuple instead of ParseResult. Since ParseResult is a
253+
# subclass or tuple we can access URL components similarly across
254+
# 2.4 - 2.7. Yay!
255+
256+
# If there is no scheme specified we create a fully qualified URL.
257+
if not parse_res[0]:
233258
url ='%s://%s%s%s' % (self.scheme, self.domain, self.api_path,
234-
parse_res.path)
235-
url = url + '?' + parse_res.query if parse_res.query else url
259+
parse_res[2])
260+
if parse_res[4]:
261+
url = url + '?' + parse_res[4]
236262

237263
return url
238264

@@ -265,8 +291,8 @@ def client_from_file(file_path='.ox3rc', env=None):
265291

266292
# Load default env if no env is specified. The default env is just the first
267293
# env listed.
268-
env_ids = [e for e in cp.get('ox3apiclient', 'envs').split('\n') if e]
269-
env = env if env else env_ids[0]
294+
if not env:
295+
env = [e for e in cp.get('ox3apiclient', 'envs').split('\n') if e][0]
270296

271297
# Required parameters for a ox3apiclient.Client instance.
272298
required_params = [

0 commit comments

Comments
 (0)