-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
73 lines (63 loc) · 1.79 KB
/
common.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
"""
Common includes the base event schema, and some functions to deal with storing/loading data.
There is a strict schema, you can't just insert into any field:
>>> try: print event(bad_field="Should cause assertion")
... except KeyError: print "Event Assertion Received"
Event Assertion Received
"""
import sys
import json
import traceback
import os
from random import randrange
import pipeless
event_schema = {
'id': "", # DateHash_Geohash
'name': "",
'date': "",
'address': "",
'venue_name': "",
'type': "",
'location': "",
'keywords': "",
'description': "",
'raw': {},
'sources': [],
'scores': {},
'score': 1,
'site_info': {},
'url': "",
'html': "",
}
event = pipeless.namedtuple_optional(event_schema, 'event')
def export_exception(event, exception):
try:
if 'name' in event.site_info:
event = event._replace(site_info=event.site_info['name'])
event = event._replace(html='')
msg = {
'error': exception.message,
'stack': traceback.format_exc().split("\n"),
'event': dict(event._asdict()),
'args': sys.argv
}
if not os.path.isdir('err'):
os.mkdir('err')
with open("err/e_" + str(randrange(0, 10000000)), "a") as f:
json.dump(msg, f)
sys.stderr.write('!')
sys.stderr.flush()
except:
pass
def unicode_to_whitespace(string):
"""
>>> unicode_to_whitespace(u'January\u00a011')
'January 11'
"""
def whitespace(string):
for char in string:
if ord(char) > 128:
yield ' '
else:
yield char
return "".join(whitespace(string)).encode('ascii')