-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.py
245 lines (206 loc) · 8.24 KB
/
tools.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
""" Welborn Productions - Stats - Tools
Tools for gathering info about other models and their counts.
(downloads, views, etc.)
"""
import logging
from functools import partial
from wp_main.utilities.utilities import get_attrs
log = logging.getLogger('wp.stats.tools')
def get_models_info(modelinfo):
""" Retrieve several model's info.
Returns a list of StatsGroup on success, or [] on failure.
Arguments:
modelinfo : A dict with Models as keys, and for values it has a
dict of options.
Example:
get_models_info({wp_blog: {'orderby': '-posted'}})
options for modelinfo:
orderby : Attribute name to use with .orderby().
displayattr : Attribute/s to display (title, name, id)
displayformat : Format string for display.
Like: '{displayattr1} {displayattr2}'
Returns a list of StatsGroups, or empty list on failure.
"""
allstats = []
for model, modelopts in modelinfo.items():
modelgrp = get_model_info(
model,
orderby=modelopts.get('orderby', None),
displayattr=modelopts.get('displayattr', None),
displayformat=modelopts.get('displayformat', None))
if modelgrp:
allstats.append(modelgrp)
return sorted(allstats, key=lambda sgrp: str(sgrp.name))
def get_model_info(model, orderby=None, displayattr=None, displayformat=None):
""" Retrieves info about a model's objects.
Returns a StatsGroup on success, or None on failure.
"""
if not hasattr(model, 'objects'):
log.error('Model with no objects attribute!: {}'.format(model))
return None
# Try getting Model._meta.verbose_name_plural. Use None on failure.
name = getattr(getattr(model, '_meta', None), 'verbose_name_plural', None)
# Build a new StatsGroup to use.
stats = StatsGroup(name=name)
if orderby:
if not validate_orderby(model, orderby):
log.error('Invalid orderby for {}: {}'.format(name, orderby))
return None
get_objects = partial(model.objects.order_by, orderby)
else:
get_objects = model.objects.all
try:
for obj in get_objects():
statitem = get_object_info(
obj,
displayattr=displayattr,
displayformat=displayformat)
if statitem:
stats.items.append(statitem)
except Exception as ex:
log.error('Error getting objects from: {}\n{}'.format(name, ex))
return stats if stats else None
def get_object_info(obj, displayattr=None, displayformat=None):
""" Retrieves a single objects info.
Returns a StatsItem (with name, download_count, view_count).
"""
dlcount = getattr(obj, 'download_count', None)
viewcount = getattr(obj, 'view_count', None)
name = ''
if displayattr:
if not isinstance(displayattr, (list, tuple)):
displayattr = (displayattr,)
if not displayformat:
# Default format is using spaces as a separator.
displayformat = ' '.join((
'{{{}}}'.format(a) for a in displayattr
))
formatargs = {
a.replace('.', '-'): get_attrs(obj, a, '') for a in displayattr
}
try:
name = displayformat.format(**formatargs)
except KeyError as ex:
# KeyError might be hard to debug for this, especially in prod.
# So, just add a more helpful error msg.
errmsg = '\n'.join((
'Error formatting stats object: {obj}',
' Error message: {msg}',
' displayattr: {attrs}',
' format str: {fmtstr}',
' format args: {fmtargs}',
)).format(
obj=obj,
msg=ex,
attrs=displayattr,
fmtstr=displayformat,
fmtargs=formatargs)
log.error(errmsg)
name = ''
if not name:
# Fall back to a known attribute if display formatting is missing
# The order of these attributes matters. (shortname before name)
trynames = ('image.name', 'shortname', 'slug', 'name', 'title')
for obj_id_attr in trynames:
name = get_attrs(obj, obj_id_attr, None)
if name:
break
else:
log.error('Object without a name!: {}'.format(obj))
return StatsItem(name=name, download_count=dlcount, view_count=viewcount)
def validate_orderby(modelobj, orderby):
""" Make sure this orderby is valid for this modelobj.
It knows about the '-orderby' style.
Returns True if the orderby is good, else False.
"""
try:
tempobj = modelobj.objects.create()
except Exception as ex:
if hasattr(modelobj, '__name__'):
mname = modelobj.__name__
else:
mname = 'unknown model'
errmsg = '\nUnable to create temp object for: {}\n{}'
log.error(errmsg.format(mname, ex))
return None
if orderby.startswith('-'):
orderby = orderby.strip('-')
goodorderby = hasattr(tempobj, orderby)
# Delete the object that was created to test the orderby attribute.
tempobj.delete()
return goodorderby
class _NoValue(object):
""" Something other than None to mean 'No value set'.
It can mean 'missing this attribute originally'.
"""
def __bool__(self):
""" NoValue is like None, bool(NoValue) is always False. """
return False
def __repr__(self):
return 'NoValue'
def __str__(self):
return self.__repr__()
NoValue = _NoValue()
class StatsGroup(object):
""" Holds a collection of stats with a name (Projects, Posts, etc.)
Each item in .items will be a StatsItem().
"""
def __init__(self, name=None, items=None):
self.name = name or 'Unknown'
self.items = items or []
self.id = None
self.update_id()
def __bool__(self):
""" Returns True if any(self.items). """
return any(self.items)
def __repr__(self):
""" A short and simple str representation for this group. """
self.update_id()
return '{}: ({} items)'.format(self.name, len(self.items))
def __str__(self):
""" A formatted str for this stats group. """
self.update_id()
return '{}:\n {}'.format(
self.name,
'\n '.join(
(str(i).replace('\n', '\n ') for i in self.items)))
def update_id(self):
""" Update the id for this stats group. """
if self.id:
return None
if self.name is NoValue:
return None
self.id = self.name.lower().replace(' ', '-').replace('.', '')
class StatsItem(object):
""" A single item with a name, download_count, and view_count. """
def __init__(self, name=None, download_count=None, view_count=None):
self.name = name or NoValue
if download_count is None:
self.download_count = NoValue
else:
self.download_count = download_count
if view_count is None:
self.view_count = NoValue
else:
self.view_count = view_count
def __bool__(self):
""" Returns False if all attributes are set to NoValue. """
return not (
(self.name is NoValue) and
(self.download_count is NoValue) and
(self.view_count is NoValue))
def __repr__(self):
""" Basic str representation. """
name = 'No Name' if self.name is NoValue else self.name
return '{}: {}, {}'.format(name, self.download_count, self.view_count)
def __str__(self):
name = 'No Name' if self.name is NoValue else self.name
infolines = []
if self.download_count is not NoValue:
infolines.append(' downloads: {}'.format(self.download_count))
if self.view_count is not NoValue:
infolines.append(' views: {}'.format(self.view_count))
if infolines:
return '{}\n{}'.format(name, '\n'.join(infolines))
# A stats item with no info!
return '{} (No Info!)'.format(name)