comparison rhodecode/lib/compat.py @ 2151:12ceeda33339 beta

#404 API extensions for showing permission for users - added permissions to get_user function - added last_login to get_user and get_users function - modified custom JSON encoder to handle non standard python types, like set, OrderedDict, datetime, date
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 21 Mar 2012 21:00:55 +0200
parents f664d3b57fa4
children e14ae8437548
comparison
equal deleted inserted replaced
2150:a8c9c0094ddf 2151:12ceeda33339
23 # 23 #
24 # You should have received a copy of the GNU General Public License 24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>. 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
27 import os 27 import os
28 import datetime
29 import functools
28 from rhodecode import __platform__, PLATFORM_WIN 30 from rhodecode import __platform__, PLATFORM_WIN
29 31
30 #============================================================================== 32 #==============================================================================
31 # json 33 # json
32 #============================================================================== 34 #==============================================================================
35
36
37 def __obj_dump(obj):
38 DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
39 DATE_FORMAT = "%Y-%m-%d"
40 if isinstance(obj, complex):
41 return [obj.real, obj.imag]
42 elif isinstance(obj, datetime.datetime):
43 return obj.strftime(DATETIME_FORMAT)
44 elif isinstance(obj, datetime.date):
45 return obj.strftime(DATE_FORMAT)
46 elif isinstance(obj, set):
47 return list(obj)
48 elif isinstance(obj, OrderedDict):
49 return obj.as_dict()
50 else:
51 raise NotImplementedError
52
33 try: 53 try:
34 import json 54 import json
55
56 # extended JSON encoder for json
57 class ExtendedEncoder(json.JSONEncoder):
58 def default(self, obj):
59 try:
60 return __obj_dump(obj)
61 except NotImplementedError:
62 pass
63 return json.JSONEncoder.default(self, obj)
64 # monkey-patch JSON encoder to use extended version
65 json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder)
35 except ImportError: 66 except ImportError:
36 import simplejson as json 67 import simplejson as json
68
69 def extended_encode(obj):
70 try:
71 return __obj_dump(obj)
72 except NotImplementedError:
73 pass
74 raise TypeError("%r is not JSON serializable" % (obj,))
75 json.dumps = functools.partial(json.dumps, default=extended_encode)
37 76
38 77
39 #============================================================================== 78 #==============================================================================
40 # izip_longest 79 # izip_longest
41 #============================================================================== 80 #==============================================================================
42 try: 81 try:
43 from itertools import izip_longest 82 from itertools import izip_longest
44 except ImportError: 83 except ImportError:
45 import itertools 84 import itertools
46 85
47 def izip_longest(*args, **kwds): # noqa 86 def izip_longest(*args, **kwds):
48 fillvalue = kwds.get("fillvalue") 87 fillvalue = kwds.get("fillvalue")
49 88
50 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop): 89 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop):
51 yield counter() # yields the fillvalue, or raises IndexError 90 yield counter() # yields the fillvalue, or raises IndexError
52 91
53 fillers = itertools.repeat(fillvalue) 92 fillers = itertools.repeat(fillvalue)
54 iters = [itertools.chain(it, sentinel(), fillers) 93 iters = [itertools.chain(it, sentinel(), fillers)
55 for it in args] 94 for it in args]
56 try: 95 try: