Skip to content
This repository
Browse code

Only require simplejson if a compatible json library isn't found

  • Loading branch information...
commit 174d438dd0b867c728081bdb049b6868bc088c60 1 parent d225d7c
Greg Brockman gdb authored

Showing 3 changed files with 39 additions and 19 deletions. Show diff stats Hide diff stats

  1. +15 1 setup.py
  2. +3 18 stripe/__init__.py
  3. +21 0 stripe/importer.py
16 setup.py
@@ -4,11 +4,25 @@
4 4
5 5 # Don't import stripe module here, since deps may not be installed
6 6 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'stripe'))
  7 +import importer
7 8 import version
8 9
9 10 path, script = os.path.split(sys.argv[0])
10 11 os.chdir(os.path.abspath(path))
11 12
  13 +# Get simplejson if we don't already have json
  14 +install_requires = 'requests'
  15 +try:
  16 + importer.import_json()
  17 +except ImportError:
  18 + install_requires.append('simplejson')
  19 +
  20 +try:
  21 + import json
  22 + _json_loaded = hasattr(json, 'loads')
  23 +except ImportError:
  24 + pass
  25 +
12 26 setup(name='stripe',
13 27 version=version.VERSION,
14 28 description='Stripe python bindings',
@@ -17,5 +31,5 @@
17 31 url='https://stripe.com/',
18 32 packages=['stripe'],
19 33 package_data={'stripe' : ['data/ca-certificates.crt', '../VERSION']},
20   - install_requires=['simplejson', 'requests']
  34 + install_requires=install_requires
21 35 )
21 stripe/__init__.py
@@ -20,24 +20,6 @@
20 20 except ImportError:
21 21 import StringIO
22 22
23   -# Python 2.5 and below do not ship with json
24   -_json_loaded = None
25   -try:
26   - import json
27   - _json_loaded = hasattr(json, 'loads')
28   -except ImportError:
29   - pass
30   -
31   -
32   -if not _json_loaded:
33   - try:
34   - import simplejson as json
35   - except ImportError:
36   - if _json_loaded is None:
37   - raise ImportError("Stripe requires a JSON library, which you do not appear to have. Please install the simplejson library. HINT: Try installing the python simplejson library via 'easy_install simplejson', or contact support@stripe.com with questions.")
38   - else:
39   - raise ImportError("Stripe requires a JSON library with the same interface as the Python 2.6 'json' library. You appear to have a 'json' library with a different interface. Please install the simplejson library. HINT: Try installing the python simplejson library via 'easy_install simplejson', or contact support@stripe.com with questions.")
40   -
41 23 # - Requests is the preferred HTTP library
42 24 # - Google App Engine has urlfetch
43 25 # - Use Pycurl if it's there (at least it verifies SSL certs)
@@ -76,6 +58,9 @@
76 58 raise ImportError("Stripe requires one of pycurl, Google App Engine's urlfetch, or urllib2. If you are on a platform where none of these libraries are available, please let us know at support@stripe.com.")
77 59
78 60 from version import VERSION
  61 +import importer
  62 +json = importer.import_json()
  63 +
79 64 logger = logging.getLogger('stripe')
80 65
81 66 ## Configuration variables
21 stripe/importer.py
... ... @@ -0,0 +1,21 @@
  1 +# Imports needed in setup.py and __init__.py
  2 +
  3 +def import_json():
  4 + # Python 2.5 and below do not ship with json
  5 + _json_loaded = None
  6 + try:
  7 + import json
  8 + if hasattr(json, 'loads'):
  9 + return json
  10 + _json_loaded = False
  11 + except ImportError:
  12 + pass
  13 +
  14 + try:
  15 + import simplejson
  16 + return simplejson
  17 + except ImportError:
  18 + if _json_loaded is None:
  19 + raise ImportError("Stripe requires a JSON library, which you do not appear to have. Please install the simplejson library. HINT: Try installing the python simplejson library via 'pip install simplejson' or 'easy_install simplejson', or contact support@stripe.com with questions.")
  20 + else:
  21 + raise ImportError("Stripe requires a JSON library with the same interface as the Python 2.6 'json' library. You appear to have a 'json' library with a different interface. Please install the simplejson library. HINT: Try installing the python simplejson library via 'pip install simplejson' or 'easy_install simplejson', or contact support@stripe.com with questions.")

0 comments on commit 174d438

Please sign in to comment.
Something went wrong with that request. Please try again.