2016-03-12 20:09:28 +01:00
|
|
|
from __future__ import division
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from builtins import range
|
|
|
|
from past.utils import old_div
|
2015-12-11 08:02:43 +01:00
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import tempfile
|
2016-01-28 19:30:13 +01:00
|
|
|
import re
|
|
|
|
import time
|
2016-03-23 08:09:25 +01:00
|
|
|
import urllib
|
2016-01-28 19:30:13 +01:00
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timedelta
|
2015-12-11 08:02:43 +01:00
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
from elodie import constants
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
def checksum(file_path, blocksize=65536):
|
|
|
|
hasher = hashlib.sha256()
|
2016-03-12 20:09:28 +01:00
|
|
|
with open(file_path, 'rb') as f:
|
2015-12-11 08:02:43 +01:00
|
|
|
buf = f.read(blocksize)
|
|
|
|
|
|
|
|
while len(buf) > 0:
|
|
|
|
hasher.update(buf)
|
|
|
|
buf = f.read(blocksize)
|
|
|
|
return hasher.hexdigest()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def create_working_folder():
|
|
|
|
temporary_folder = tempfile.gettempdir()
|
2016-01-26 20:01:05 +01:00
|
|
|
folder = os.path.join(temporary_folder, random_string(10), random_string(10))
|
2015-12-11 08:02:43 +01:00
|
|
|
os.makedirs(folder)
|
|
|
|
|
|
|
|
return (temporary_folder, folder)
|
|
|
|
|
2016-03-23 08:09:25 +01:00
|
|
|
def download_file(name, destination):
|
|
|
|
try:
|
|
|
|
final_name = '{}/{}{}'.format(destination, random_string(10), os.path.splitext(name)[1])
|
|
|
|
urllib.urlretrieve(
|
|
|
|
'https://s3.amazonaws.com/jmathai/github/elodie/{}'.format(name),
|
|
|
|
final_name
|
|
|
|
)
|
|
|
|
return final_name
|
|
|
|
except Exception as e:
|
|
|
|
return False
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
def get_file(name):
|
2016-03-23 08:09:25 +01:00
|
|
|
file_path = get_file_path(name)
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return file_path
|
|
|
|
|
|
|
|
def get_file_path(name):
|
2015-12-11 08:02:43 +01:00
|
|
|
current_folder = os.path.dirname(os.path.realpath(__file__))
|
2016-01-26 20:01:05 +01:00
|
|
|
return os.path.join(current_folder, 'files', name)
|
2015-12-11 08:02:43 +01:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def get_test_location():
|
|
|
|
return (61.013710, 99.196656, 'Siberia')
|
|
|
|
|
2016-12-20 06:12:36 +01:00
|
|
|
def populate_folder(number_of_files, include_invalid=False):
|
2015-12-11 08:02:43 +01:00
|
|
|
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10))
|
|
|
|
os.makedirs(folder)
|
|
|
|
|
|
|
|
for x in range(0, number_of_files):
|
|
|
|
ext = 'jpg' if x % 2 == 0 else 'txt'
|
|
|
|
fname = '%s/%s.%s' % (folder, x, ext)
|
|
|
|
with open(fname, 'a'):
|
|
|
|
os.utime(fname, None)
|
|
|
|
|
2016-12-20 06:12:36 +01:00
|
|
|
if include_invalid:
|
|
|
|
fname = '%s/%s' % (folder, 'invalid.invalid')
|
|
|
|
with open(fname, 'a'):
|
|
|
|
os.utime(fname, None)
|
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
return folder
|
|
|
|
|
|
|
|
def random_string(length):
|
|
|
|
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length))
|
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def random_decimal():
|
|
|
|
return random.random()
|
|
|
|
|
|
|
|
def random_coordinate(coordinate, precision):
|
|
|
|
# Here we add to the decimal section of the coordinate by a given precision
|
2016-03-12 20:09:28 +01:00
|
|
|
return coordinate + ((old_div(10.0, (10.0**precision))) * random_decimal())
|
2015-12-28 09:42:18 +01:00
|
|
|
|
2015-12-11 08:02:43 +01:00
|
|
|
def temp_dir():
|
|
|
|
return tempfile.gettempdir()
|
2016-01-28 19:30:13 +01:00
|
|
|
|
|
|
|
def is_windows():
|
|
|
|
return os.name == 'nt'
|
|
|
|
|
|
|
|
# path_tz_fix(file_name)
|
|
|
|
# Change timestamp in file_name by the offset
|
|
|
|
# between UTC and local time, i.e.
|
|
|
|
# 2015-12-05_00-59-26-with-title-some-title.jpg ->
|
|
|
|
# 2015-12-04_20-59-26-with-title-some-title.jpg
|
|
|
|
# (Windows only)
|
|
|
|
|
|
|
|
def path_tz_fix(file_name):
|
|
|
|
if is_windows():
|
|
|
|
# Calculate the offset between UTC and local time
|
2016-03-12 20:09:28 +01:00
|
|
|
tz_shift = old_div((datetime.fromtimestamp(0) -
|
|
|
|
datetime.utcfromtimestamp(0)).seconds,3600)
|
2016-01-28 19:30:13 +01:00
|
|
|
# replace timestamp in file_name
|
|
|
|
m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',file_name)
|
|
|
|
t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S')))
|
|
|
|
s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S')
|
|
|
|
return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,file_name)
|
|
|
|
else:
|
|
|
|
return file_name
|
|
|
|
|
|
|
|
# time_convert(s_time)
|
|
|
|
# Change s_time (struct_time) by the offset
|
|
|
|
# between UTC and local time
|
|
|
|
# (Windows only)
|
|
|
|
|
|
|
|
def time_convert(s_time):
|
|
|
|
if is_windows():
|
|
|
|
return time.gmtime((time.mktime(s_time)))
|
|
|
|
else:
|
|
|
|
return s_time
|
2016-02-12 20:22:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
# isclose(a,b,rel_tol)
|
|
|
|
# To compare float coordinates a and b
|
|
|
|
# with relative tolerance c
|
|
|
|
|
|
|
|
def isclose(a, b, rel_tol = 1e-8):
|
2016-03-23 17:34:35 +01:00
|
|
|
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
|
|
|
return False
|
|
|
|
|
2016-02-12 20:22:26 +01:00
|
|
|
diff = abs(a - b)
|
|
|
|
return (diff <= abs(rel_tol * a) and
|
|
|
|
diff <= abs(rel_tol * b))
|
2017-01-03 05:58:52 +01:00
|
|
|
|
|
|
|
def reset_dbs():
|
|
|
|
hash_db = constants.hash_db
|
|
|
|
if os.path.isfile(hash_db):
|
|
|
|
os.rename(hash_db, '{}-test'.format(hash_db))
|
|
|
|
|
|
|
|
location_db = constants.location_db
|
|
|
|
if os.path.isfile(location_db):
|
|
|
|
os.rename(location_db, '{}-test'.format(location_db))
|
|
|
|
|
|
|
|
def restore_dbs():
|
|
|
|
hash_db = '{}-test'.format(constants.hash_db)
|
|
|
|
if os.path.isfile(hash_db):
|
|
|
|
os.rename(hash_db, hash_db.replace('-test', ''))
|
|
|
|
|
|
|
|
location_db = '{}-test'.format(constants.location_db)
|
|
|
|
if os.path.isfile(location_db):
|
|
|
|
os.rename(location_db, location_db.replace('-test', ''))
|