-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
231 lines (188 loc) · 6.85 KB
/
utils.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- encoding: utf-8 -*-
import os
import six
import json
import functools
__all__ = ('ustr',
'AttrDict',
'DirMixIn',
'UConverter',
'wpartial',
'makedirs',
'json_read',
'json_write',
'xinput',
)
# Python 2/3 workaround in raw_input
try:
xinput = raw_input
except NameError:
xinput = input
# Check if anyfield is installed
# and import function which converts SField instances to functions
try:
from anyfield import toFn as normalizeSField
except ImportError:
normalizeSField = lambda x: x
def makedirs(path):
""" os.makedirs wrapper. No errors raised if directory already exists
:param str path: directory path to create
"""
try:
os.makedirs(path)
except os.error:
pass
def json_read(file_path):
""" Read specified json file
"""
with open(file_path, 'rt') as json_data:
data = json.load(json_data)
return data
def json_write(file_path, *args, **kwargs):
""" Write data to specified json file
Note, this function uses dumps function to convert data to json first,
and write only if conversion is successfule. This allows to avoid
loss of data when rewriting file.
"""
# note, using dumps instead of dump, because we need to check if data will
# be dumped correctly. using dump on incorect data, causes file to be half
# written, and thus broken
json_data = json.dumps(*args, **kwargs)
with open(file_path, 'wt') as json_file:
json_file.write(json_data)
def wpartial(func, *args, **kwargs):
"""Wrapped partial, same as functools.partial decorator,
but also calls functools.wrap on its result thus shwing correct
function name and representation.
"""
partial = functools.partial(func, *args, **kwargs)
return functools.wraps(func)(partial)
def preprocess_args(*args, **kwargs):
""" Skip all args, and kwargs that set to None
Mostly for internal usage.
Used to workaround xmlrpc None restrictions
"""
kwargs = {key: val for key, val in kwargs.items() if val is not None}
xargs = list(args[:])
while xargs and xargs[-1] is None:
xargs.pop()
return xargs, kwargs
def stdcall(fn):
""" Simple decorator for server methods, that supports standard call
If method supports call like
``method(ids, <args>, context=context, <kwargs>)``,
then it may be decrated by this decorator to appear in
dir(record) and dir(recordlist) calls, thus making it available
for autocompletition in ipython or other python shells
"""
fn.__x_stdcall__ = True
return fn
class UConverter(object):
""" Simple converter to unicode
Create instance with specified list of encodings to be used to
try to convert value to unicode
Example::
ustr = UConverter(['utf-8', 'cp-1251'])
my_unicode_str = ustr(b'hello - привет')
"""
default_encodings = ['utf-8', 'ascii']
def __init__(self, hint_encodings=None):
if hint_encodings:
self.encodings = hint_encodings
else:
self.encodings = self.default_encodings[:]
def __call__(self, value):
""" Convert value to unicode
:param value: the value to convert
:raise: UnicodeError if value cannot be coerced to unicode
:return: unicode string representing the given value
"""
# it is unicode
if isinstance(value, six.text_type):
return value
# it is not binary type (str for python2 and bytes for python3)
if not isinstance(value, six.binary_type):
try:
return six.text_type(value)
except Exception:
# Cannot directly convert to unicode. So let's try to convert
# to binary, and that try diferent encoding to it
try:
value = six.binary_type(value)
except:
raise UnicodeError('unable to convert to unicode %r'
'' % (value,))
# value is binary type (str for python2 and bytes for python3)
for ln in self.encodings:
try:
return six.text_type(value, ln)
except Exception:
pass
raise UnicodeError('unable to convert to unicode %r' % (value,))
# default converter instance
ustr = UConverter()
# DirMixIn class implementation. To be able to use super calls to __dir__ in
# subclasses (Py 2/3 support)
# code is based on
# http://www.quora.com/How-dir-is-implemented-Is-there-any-PEP-related-to-that
try:
object.__dir__
except AttributeError:
class DirMixIn(object):
""" Mix-in to make implementing __dir__ method in subclasses simpler
"""
def __dir__(self):
def get_attrs(obj):
import types
if not hasattr(obj, '__dict__'):
return [] # slots only
if not isinstance(obj.__dict__, (dict, types.DictProxyType)):
raise TypeError("%s.__dict__ is not a dictionary"
"" % obj.__name__)
return obj.__dict__.keys()
def dir2(obj):
attrs = set()
if not hasattr(obj, '__bases__'):
# obj is an instance
if not hasattr(obj, '__class__'):
# slots
return sorted(get_attrs(obj))
klass = obj.__class__
attrs.update(get_attrs(klass))
else:
# obj is a class
klass = obj
for cls in klass.__bases__:
attrs.update(get_attrs(cls))
attrs.update(dir2(cls))
attrs.update(get_attrs(obj))
return list(attrs)
return dir2(self)
else:
# There are no need to implement any aditional logic for Python 3.3+,
# because there base class 'object' already have implemented
# '__dir__' method, which could be accessed via super() by subclasses
class DirMixIn:
pass
class AttrDict(dict, DirMixIn):
""" Simple class to make dictionary able to use attribute get operation
to get elements it contains using syntax like:
>>> d = AttrDict(arg1=1, arg2='hello')
>>> print(d.arg1)
1
>>> print(d.arg2)
hello
>>> print(d['arg2'])
hello
>>> print(d['arg1'])
1
"""
def __getattr__(self, name):
res = None
try:
res = super(AttrDict, self).__getitem__(name)
except KeyError as e:
raise AttributeError(str(e))
return res
def __dir__(self):
return super(AttrDict, self).__dir__() + self.keys()