2016-07-10 14:16:17 +02:00
|
|
|
"""EventMan(ager) utils
|
2015-03-30 21:31:09 +02:00
|
|
|
|
|
|
|
Miscellaneous utilities.
|
|
|
|
|
2016-07-10 14:16:17 +02:00
|
|
|
Copyright 2015-2016 Davide Alberani <da@erlug.linux.it>
|
|
|
|
RaspiBO <info@raspibo.org>
|
2015-03-30 21:31:09 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import csv
|
2015-03-30 21:39:12 +02:00
|
|
|
import json
|
2015-05-02 19:26:23 +02:00
|
|
|
import string
|
|
|
|
import random
|
|
|
|
import hashlib
|
2015-03-30 21:39:12 +02:00
|
|
|
import datetime
|
2017-03-27 21:57:40 +02:00
|
|
|
import io
|
2015-03-31 23:35:56 +02:00
|
|
|
from bson.objectid import ObjectId
|
2015-03-30 21:31:09 +02:00
|
|
|
|
2015-03-30 21:39:12 +02:00
|
|
|
|
2015-03-30 21:31:09 +02:00
|
|
|
def csvParse(csvStr, remap=None, merge=None):
|
|
|
|
"""Parse a CSV file, optionally renaming the columns and merging other information.
|
|
|
|
|
|
|
|
:param csvStr: the CSV to parse, as a string
|
|
|
|
:type csvStr: str
|
|
|
|
:param remap: a dictionary used to rename the columns
|
|
|
|
:type remap: dict
|
|
|
|
:param merge: merge these information into each line
|
|
|
|
:type merge: dict
|
|
|
|
|
2016-06-11 16:14:28 +02:00
|
|
|
:returns: tuple with a dict of total and valid lines and the data
|
2015-03-30 21:31:09 +02:00
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2017-03-27 21:57:40 +02:00
|
|
|
if isinstance(csvStr, bytes):
|
|
|
|
csvStr = csvStr.decode('utf-8')
|
|
|
|
fd = io.StringIO(csvStr)
|
2015-03-30 21:31:09 +02:00
|
|
|
reader = csv.reader(fd)
|
|
|
|
remap = remap or {}
|
|
|
|
merge = merge or {}
|
|
|
|
fields = 0
|
|
|
|
reply = dict(total=0, valid=0)
|
|
|
|
results = []
|
|
|
|
try:
|
2017-03-27 21:57:40 +02:00
|
|
|
headers = next(reader)
|
2015-03-30 21:31:09 +02:00
|
|
|
fields = len(headers)
|
|
|
|
except (StopIteration, csv.Error):
|
|
|
|
return reply, {}
|
|
|
|
|
|
|
|
for idx, header in enumerate(headers):
|
|
|
|
if header in remap:
|
|
|
|
headers[idx] = remap[header]
|
2015-04-25 10:04:47 +02:00
|
|
|
else:
|
|
|
|
headers[idx] = header.lower().replace(' ', '_')
|
2015-03-30 21:31:09 +02:00
|
|
|
try:
|
|
|
|
for row in reader:
|
|
|
|
try:
|
|
|
|
reply['total'] += 1
|
|
|
|
if len(row) != fields:
|
|
|
|
continue
|
2017-03-27 21:57:40 +02:00
|
|
|
values = dict(zip(headers, row))
|
2015-03-30 21:31:09 +02:00
|
|
|
values.update(merge)
|
|
|
|
results.append(values)
|
|
|
|
reply['valid'] += 1
|
|
|
|
except csv.Error:
|
|
|
|
continue
|
|
|
|
except csv.Error:
|
|
|
|
pass
|
|
|
|
fd.close()
|
|
|
|
return reply, results
|
|
|
|
|
2015-03-30 21:39:12 +02:00
|
|
|
|
2015-05-02 19:26:23 +02:00
|
|
|
def hash_password(password, salt=None):
|
2016-08-01 14:40:29 +02:00
|
|
|
"""Hash a password.
|
|
|
|
|
|
|
|
:param password: the cleartext password
|
|
|
|
:type password: str
|
|
|
|
:param salt: the optional salt (randomly generated, if None)
|
|
|
|
:type salt: str
|
|
|
|
|
|
|
|
:returns: the hashed password
|
|
|
|
:rtype: str"""
|
2015-05-02 19:26:23 +02:00
|
|
|
if salt is None:
|
|
|
|
salt_pool = string.ascii_letters + string.digits
|
2017-03-27 21:57:40 +02:00
|
|
|
salt = ''.join(random.choice(salt_pool) for x in range(32))
|
|
|
|
pwd = '%s%s' % (salt, password)
|
|
|
|
hash_ = hashlib.sha512(pwd.encode('utf-8'))
|
2015-05-02 19:26:23 +02:00
|
|
|
return '$%s$%s' % (salt, hash_.hexdigest())
|
|
|
|
|
|
|
|
|
2015-03-30 21:39:12 +02:00
|
|
|
class ImprovedEncoder(json.JSONEncoder):
|
2015-04-04 17:26:00 +02:00
|
|
|
"""Enhance the default JSON encoder to serialize datetime and ObjectId instances."""
|
2015-03-30 21:39:12 +02:00
|
|
|
def default(self, o):
|
2017-03-27 21:57:40 +02:00
|
|
|
if isinstance(o, bytes):
|
|
|
|
try:
|
|
|
|
return o.decode('utf-8')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
elif isinstance(o, (datetime.datetime, datetime.date,
|
2015-03-31 23:35:56 +02:00
|
|
|
datetime.time, datetime.timedelta, ObjectId)):
|
2015-04-04 17:26:00 +02:00
|
|
|
try:
|
|
|
|
return str(o)
|
2017-03-27 21:57:40 +02:00
|
|
|
except Exception as e:
|
2015-04-04 17:26:00 +02:00
|
|
|
pass
|
2016-06-13 22:44:57 +02:00
|
|
|
elif isinstance(o, set):
|
|
|
|
return list(o)
|
2015-03-30 21:39:12 +02:00
|
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
|
2015-04-04 17:26:00 +02:00
|
|
|
|
2015-03-30 21:39:12 +02:00
|
|
|
# Inject our class as the default encoder.
|
|
|
|
json._default_encoder = ImprovedEncoder()
|
|
|
|
|