2015-03-30 21:31:09 +02:00
|
|
|
"""Event Man(ager) utils
|
|
|
|
|
|
|
|
Miscellaneous utilities.
|
|
|
|
|
|
|
|
Copyright 2015 Davide Alberani <da@erlug.linux.it>
|
|
|
|
RaspiBO <info@raspibo.org>
|
|
|
|
|
|
|
|
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
|
2015-03-30 21:31:09 +02:00
|
|
|
import StringIO
|
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
|
|
|
|
"""
|
|
|
|
fd = StringIO.StringIO(csvStr)
|
|
|
|
reader = csv.reader(fd)
|
|
|
|
remap = remap or {}
|
|
|
|
merge = merge or {}
|
|
|
|
fields = 0
|
|
|
|
reply = dict(total=0, valid=0)
|
|
|
|
results = []
|
|
|
|
try:
|
|
|
|
headers = reader.next()
|
|
|
|
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
|
|
|
|
row = [unicode(cell, 'utf-8', 'replace') for cell in row]
|
|
|
|
values = dict(map(None, headers, row))
|
|
|
|
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):
|
|
|
|
if salt is None:
|
|
|
|
salt_pool = string.ascii_letters + string.digits
|
|
|
|
salt = ''.join(random.choice(salt_pool) for x in xrange(32))
|
|
|
|
hash_ = hashlib.sha512('%s%s' % (salt, password))
|
|
|
|
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):
|
|
|
|
if 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)
|
|
|
|
except Exception, e:
|
|
|
|
pass
|
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()
|
|
|
|
|