-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
util.py
191 lines (154 loc) · 5.57 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2012, Dongsheng Cai
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Dongsheng Cai nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL DONGSHENG CAI BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
try:
from html.parser import HTMLParser
except:
from html.parser import HTMLParser
import calendar
import datetime
import logging
import tornado
try:
from html.entities import name2codepoint
except:
from html.entities import name2codepoint
import re
import sys
import unicodedata
import os
import base64
from tornado.options import options
from hashlib import sha1, sha512, md5
from bson.dbref import DBRef
from bson.max_key import MaxKey
from bson.min_key import MinKey
from bson.objectid import ObjectId
from bson.son import RE_TYPE
from bson.timestamp import Timestamp
try:
import uuid
_use_uuid = True
except ImportError:
_use_uuid = False
class HTMLTextExtractor(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.result = []
def handle_data(self, d):
self.result.append(d)
def handle_charref(self, number):
codepoint = int(number[1:], 16) if number[0] in ("x", "X") else int(number)
self.result.append(chr(codepoint))
def handle_entityref(self, name):
codepoint = name2codepoint[name]
self.result.append(chr(codepoint))
def get_text(self):
return "".join(self.result)
def strip_tags(html):
s = HTMLTextExtractor()
s.feed(html)
return s.get_text()
def json_default(obj):
""" adapted from bson.json_util.default """
if isinstance(obj, ObjectId):
""" _id field should be just string """
return str(obj)
if isinstance(obj, DBRef):
return obj.as_doc()
if isinstance(obj, datetime.datetime):
# TODO share this code w/ bson.py?
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(calendar.timegm(obj.timetuple()) * 1000 + obj.microsecond / 1000)
return {"$date": millis}
if isinstance(obj, RE_TYPE):
flags = ""
if obj.flags & re.IGNORECASE:
flags += "i"
if obj.flags & re.MULTILINE:
flags += "m"
return {"$regex": obj.pattern, "$options": flags}
if isinstance(obj, MinKey):
return {"$minKey": 1}
if isinstance(obj, MaxKey):
return {"$maxKey": 1}
if isinstance(obj, Timestamp):
return {"t": obj.time, "i": obj.inc}
if _use_uuid and isinstance(obj, uuid.UUID):
return {"$uuid": obj.hex}
raise TypeError("%r is not JSON serializable" % obj)
def filter_alphabetanum(string):
# absolutely alphabeta and number only
string = (
unicodedata.normalize("NFKD", string).encode("ascii", "ignore").decode("ascii")
)
string = re.sub(r"[^\w]+", " ", string)
string = "".join(string.lower().strip().split())
return string
def get_filepath(filename):
return os.path.join(os.path.abspath(options.pemdir), filename)
def encode_file(file):
content = file["body"]
filename = file["filename"]
content_type = file["content_type"]
return base64.b64encode(content).decode("utf-8")
def save_file(req):
filename = sha1(req["body"]).hexdigest()
filepath = get_filepath(filename)
thefile = open(filepath, "w")
thefile.write(req["body"])
thefile.close()
return filename
def file_exists(filename):
if not filename:
return False
if os.path.exists(filename):
return True
fullpath = get_filepath(filename)
if fullpath and os.path.exists(fullpath):
return True
return False
def rm_file(filename):
if not filename:
return
fullpath = get_filepath(filename)
if os.path.isfile(filename):
os.remove(filename)
elif os.path.isfile(fullpath):
os.remove(fullpath)
def get_password(password, salt):
hash = sha512()
hash.update(("%s%s" % (salt, password)).encode("utf-8"))
return hash.hexdigest()
def create_access_key():
return md5(str(uuid.uuid4()).encode("utf-8")).hexdigest()
def json_decode(text):
""" Takes a JSON encoded string and converts it into a ptyhon variable """
return tornado.escape.json_decode(text)
def json_encode(obj):
""" serialize python value to str """
return tornado.escape.json_encode(obj)