master
1import logging
2import os
3from random import SystemRandom
4import sys
5
6logger = logging.getLogger('stripe')
7logger.addHandler(logging.StreamHandler(sys.stdout))
8logger.setLevel(logging.INFO)
9
10__all__ = ['StringIO', 'json', 'utf8', 'random_letters', 'mkdir_p']
11
12if sys.version_info < (3,0):
13 # Used to interface with pycurl, which we only make available for
14 # those Python versions
15 try:
16 import cStringIO as StringIO
17 except ImportError:
18 import StringIO
19
20try:
21 import json
22except ImportError:
23 json = None
24
25if not (json and hasattr(json, 'loads')):
26 try:
27 import simplejson as json
28 except ImportError:
29 if not json:
30 raise ImportError(
31 "Stripe requires a JSON library, such as simplejson. "
32 "HINT: Try installing the "
33 "python simplejson library via 'pip install simplejson' or "
34 "'easy_install simplejson'.")
35 else:
36 raise ImportError(
37 "Stripe requires a JSON library with the same interface as "
38 "the Python 2.6 'json' library. You appear to have a 'json' "
39 "library with a different interface. Please install "
40 "the simplejson library. HINT: Try installing the "
41 "python simplejson library via 'pip install simplejson' "
42 "or 'easy_install simplejson'.")
43
44
45def utf8(value):
46 if sys.version_info < (3, 0) and isinstance(value, unicode):
47 return value.encode('utf-8')
48 else:
49 return value
50
51def random_letters(count=4):
52 LETTERS = "abcdefghijklmnopqrstuvwxyz"
53 output = []
54 for i in range(0, count):
55 output.append(SystemRandom().choice(LETTERS))
56 return "".join(output)
57
58# TODO: Python >2.5 ?
59def mkdir_p(path):
60 try:
61 os.makedirs(path)
62 except OSError:
63 if os.path.isdir(path): pass
64 else: raise
65
66def exception_as():
67 _, err, _ = sys.exc_info()
68 return err