forked from jazzband/sorl-thumbnail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
59 lines (44 loc) · 1.41 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import hashlib
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import smart_str
from django.utils.importlib import import_module
from django.utils import simplejson
class ThumbnailError(Exception):
pass
class SortedJSONEncoder(simplejson.JSONEncoder):
"""
A json encoder that sorts the dict keys
"""
def __init__(self, **kwargs):
kwargs['sort_keys'] = True
super(SortedJSONEncoder, self).__init__(**kwargs)
def toint(number):
"""
Helper to return rounded int for a float or just the int it self.
"""
if isinstance(number, float):
number = round(number, 0)
return int(number)
def tokey(*args):
"""
Computes a (hopefully) unique key from arguments given.
"""
salt = '||'.join([smart_str(arg) for arg in args])
hash_ = hashlib.md5(salt)
return hash_.hexdigest()
def serialize(obj):
return simplejson.dumps(obj, cls=SortedJSONEncoder)
def deserialize(s):
return simplejson.loads(s)
def get_module_class(class_path):
"""
imports and returns module class from ``path.to.module.Class``
argument
"""
try:
mod_name, cls_name = class_path.rsplit('.', 1)
mod = import_module(mod_name)
except ImportError, e:
raise ImproperlyConfigured(('Error importing module %s: "%s"' %
(mod_name, e)))
return getattr(mod, cls_name)