-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
162 lines (125 loc) · 4.92 KB
/
util.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import inspect
import json
import datetime
import logging
import rozengine
import re
from jinja2 import nodes
from jinja2.ext import Extension
from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS
from google.appengine.ext.db import Property
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import datastore_types
def safe_getattr(obj, attr, default=None):
if hasattr(obj, attr):
return getattr(obj, attr)
else:
return default
def safe_setattr(obj, attr, default=None):
if hasattr(obj, attr):
setattr(obj, attr, default)
def import_from_string(name, safe=False):
try:
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
except ImportError as error:
if safe:
return None
else:
raise error
def is_serializable(p, v):
if inspect.isclass(v) and issubclass(v, db.Model):
return True
return not p.startswith("_") and not inspect.isclass(v) and \
v.__class__.__name__ != "property" and v.__class__.__name__ != "function" and \
not v.__class__.__name__.startswith('_')
def available_attrs(fn):
"""
Return the list of functools-wrappable attributes on a callable.
This is required as a workaround for http://bugs.python.org/issue3445.
"""
return tuple(a for a in WRAPPER_ASSIGNMENTS if hasattr(fn, a))
def humanize(val):
new_parts = []
parts = val.split('_')
parts = " ".join(parts).split(' ')
for word in parts:
word = "".join([word[0].upper()] + list(word[1:]))
new_parts.append(word)
return " ".join(new_parts)
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime) or \
isinstance(obj, datetime.date) or \
isinstance(obj, datetime.time):
return obj.isoformat()
elif isinstance(obj, db.Model):
vals = dict((p, getattr(obj, p))
for p in obj.properties())
if hasattr(obj, 'key_or_id'):
vals['key_or_id'] = getattr(obj, 'key_or_id')
else:
vals['key_or_id'] = obj.key().id_or_name()
vals['__str__'] = str(obj)
return vals
elif inspect.isclass(obj) and issubclass(obj, db.Model):
vals = {}
vals['key_or_id'] = {'dbtype': 'Key', 'name': 'key_or_id', 'verbose_name': 'Key', 'creation_counter':-1}
#vals['__str__'] = {'dbtype': '__internal__', 'name': '__str__', 'creation_counter':-1}
for p, v in vars(obj).iteritems():
if is_serializable(p, v):
dbtype = v.__class__.__name__
vals[p] = {'dbtype': dbtype}
for pp, vv in vars(v).iteritems():
if not inspect.isclass(vv) or (inspect.isclass(vv) and not issubclass(vv, db.Model)):
vals[p][pp] = getattr(v, pp)
elif pp == "reference_class":
vals[p][pp] = v.reference_class.__module__ + "." + v.reference_class.__name__
else:
vals[p][pp] = pp
return vals
elif inspect.isclass(obj):
return obj.__module__ + "." + obj.__name__
elif inspect.ismodule(obj):
return None
elif isinstance(obj, rozengine.RozModel) or isinstance(obj, rozengine.RozApp):
return dict((p, getattr(obj, p))
for p, v in vars(obj).iteritems() if is_serializable(p, v))
elif isinstance(obj, Property):
return dict((p, getattr(obj, p))
for p, v in vars(obj).iteritems())
elif isinstance(obj, users.User):
value = {}
for key in ['email', 'nickname', 'user_id', 'federated_identity', 'federated_provider']:
value[key] = getattr(obj, key)()
value['__str__'] = value['nickname']
value['__type__'] = 'User'
return value
elif isinstance(obj, db.Key):
value = {}
for key in ['id', 'id_or_name', 'kind', 'name', 'namespace', 'app', 'parent']:
value[key] = getattr(obj, key)()
value['__str__'] = value['id_or_name']
value['__type__'] = 'Key'
return value
else:
return super(JSONEncoder, self).default(obj)
class RozJSEscapeExtension(Extension):
# a set of names that trigger the extension.
tags = set(['rozjsescape'])
def parse(self, parser):
lineno = parser.stream.next().lineno
body = parser.parse_statements(['name:endrozjsescape'], drop_needle=True)
return nodes.CallBlock(self.call_method('_escape_it', []),
[], [], body).set_lineno(lineno)
def _escape_it(self, caller):
"""Helper callback."""
content = caller()
content = content.replace("\t", " ")
content = content.replace("\n", " ")
content = re.sub(r"\s\s+", " ", str(content))
return content