diff --git a/.travis.yml b/.travis.yml index 4c657b5..38e80eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,12 @@ virtualenv: system_site_packages: true before_install: - "sudo apt-get update -qq" - - "sudo apt-get install python-dev python-pip python-pyexiv2 -y" + - "sudo apt-get install python-dev python-pip python-pyexiv2 libimage-exiftool-perl -y" install: - "sudo pip install docopt LatLon nose requests" # command to run tests +# test mapquest key +before_script: + - "mkdir ~/.elodie" + - "sed 's/your-api-key-goes-here/x8wQLqGhW7qK3sFpjYtVTogVtoMK0S8s/g' config.ini-sample > ~/.elodie/config.ini" script: "nosetests elodie/tests/*.py" diff --git a/elodie/constants.py b/elodie/constants.py index c324876..ebb7c49 100644 --- a/elodie/constants.py +++ b/elodie/constants.py @@ -1,6 +1,6 @@ from os import path -debug = False +debug = True application_directory = '{}/.elodie'.format(path.expanduser('~')) hash_db = '{}/hash.json'.format(application_directory) script_directory = path.dirname(path.dirname(path.abspath(__file__))) diff --git a/elodie/filesystem.py b/elodie/filesystem.py index 436edfd..a200a30 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -43,9 +43,12 @@ class FileSystem: def delete_directory_if_empty(self, directory_path): try: os.rmdir(directory_path) + return True except OSError: pass + return False + """ Recursively get all files which match a path and extension. diff --git a/elodie/tests/files/plain.jpg b/elodie/tests/files/plain.jpg new file mode 100644 index 0000000..6461606 Binary files /dev/null and b/elodie/tests/files/plain.jpg differ diff --git a/elodie/tests/files/with-location-and-title.jpg b/elodie/tests/files/with-location-and-title.jpg new file mode 100644 index 0000000..ad15505 Binary files /dev/null and b/elodie/tests/files/with-location-and-title.jpg differ diff --git a/elodie/tests/files/with-location.jpg b/elodie/tests/files/with-location.jpg new file mode 100644 index 0000000..f4bc4d1 Binary files /dev/null and b/elodie/tests/files/with-location.jpg differ diff --git a/elodie/tests/files/with-title.jpg b/elodie/tests/files/with-title.jpg new file mode 100644 index 0000000..4e74f6d Binary files /dev/null and b/elodie/tests/files/with-title.jpg differ diff --git a/elodie/tests/filesystem.py b/elodie/tests/filesystem.py index afde9da..a6a98de 100644 --- a/elodie/tests/filesystem.py +++ b/elodie/tests/filesystem.py @@ -3,13 +3,20 @@ import os import sys import random +import re import shutil import string import tempfile +import time sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) from elodie import filesystem +from elodie.media.media import Media +from elodie.media.photo import Photo +from elodie.media.video import Video + +os.environ['TZ'] = 'GMT' filesystem = filesystem.FileSystem() @@ -46,6 +53,191 @@ def test_create_directory_invalid_permissions(): assert status == False +def test_delete_directory_if_empty(): + folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) + os.makedirs(folder) + + assert os.path.isdir(folder) == True + assert os.path.exists(folder) == True + + filesystem.delete_directory_if_empty(folder) + + assert os.path.isdir(folder) == False + assert os.path.exists(folder) == False + +def test_delete_directory_if_empty_when_not_empty(): + folder = '%s/%s/%s' % (tempfile.gettempdir(), random_string(10), random_string(10)) + os.makedirs(folder) + parent_folder = os.path.dirname(folder) + + assert os.path.isdir(folder) == True + assert os.path.exists(folder) == True + assert os.path.isdir(parent_folder) == True + assert os.path.exists(parent_folder) == True + + filesystem.delete_directory_if_empty(parent_folder) + + assert os.path.isdir(folder) == True + assert os.path.exists(folder) == True + assert os.path.isdir(parent_folder) == True + assert os.path.exists(parent_folder) == True + + shutil.rmtree(parent_folder) + +def test_get_all_files_success(): + folder = populate_folder(5) + files = filesystem.get_all_files(folder) + shutil.rmtree(folder) + + assert len(files) == 5 + + +def test_get_all_files_by_extension(): + folder = populate_folder(5) + + files = filesystem.get_all_files(folder) + assert len(files) == 5 + + files = filesystem.get_all_files(folder, 'jpg') + assert len(files) == 3 + + files = filesystem.get_all_files(folder, 'txt') + assert len(files) == 2 + + files = filesystem.get_all_files(folder, 'gif') + assert len(files) == 0 + + shutil.rmtree(folder) + +def test_get_current_directory(): + assert os.getcwd() == filesystem.get_current_directory() + +def test_get_file_name_plain(): + media = Photo(get_file('plain.jpg')) + file_name = filesystem.get_file_name(media) + + assert file_name == '2015-12-05_00-59-26-plain.jpg' + +def test_get_file_name_with_title(): + media = Photo(get_file('with-title.jpg')) + file_name = filesystem.get_file_name(media) + + assert file_name == '2015-12-05_00-59-26-with-title-some-title.jpg' + +def test_get_folder_name_by_date(): + time_tuple = (2010, 4, 15, 1, 2, 3, 0, 0, 0) + folder_name = filesystem.get_folder_name_by_date(time_tuple) + + assert folder_name == '2010-04-Apr' + + time_tuple = (2010, 9, 15, 1, 2, 3, 0, 0, 0) + folder_name = filesystem.get_folder_name_by_date(time_tuple) + + assert folder_name == '2010-09-Sep' + +def test_get_folder_path_plain(): + media = Photo(get_file('plain.jpg')) + path = filesystem.get_folder_path(media.get_metadata()) + + assert path == '2015-12-Dec/Unknown Location' + +def test_get_folder_path_with_title(): + media = Photo(get_file('with-title.jpg')) + path = filesystem.get_folder_path(media.get_metadata()) + + assert path == '2015-12-Dec/Unknown Location' + +def test_get_folder_path_with_location(): + media = Photo(get_file('with-location.jpg')) + path = filesystem.get_folder_path(media.get_metadata()) + + assert path == '2015-12-Dec/Sunnyvale' + +def test_get_folder_path_with_location_and_title(): + media = Photo(get_file('with-location-and-title.jpg')) + path = filesystem.get_folder_path(media.get_metadata()) + + assert path == '2015-12-Dec/Sunnyvale' + +def test_process_file_plain(): + temporary_folder = tempfile.gettempdir() + folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) + os.makedirs(folder) + + origin = '%s/plain.jpg' % folder + shutil.copyfile(get_file('plain.jpg'), origin) + + media = Photo(origin) + destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) + + shutil.rmtree(folder) + shutil.rmtree(os.path.dirname(os.path.dirname(destination))) + + assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination + +def test_process_file_with_title(): + temporary_folder = tempfile.gettempdir() + folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) + os.makedirs(folder) + + origin = '%s/plain.jpg' % folder + shutil.copyfile(get_file('with-title.jpg'), origin) + + media = Photo(origin) + destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) + + shutil.rmtree(folder) + shutil.rmtree(os.path.dirname(os.path.dirname(destination))) + + assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain-some-title.jpg' in destination + +def test_process_file_with_location(): + temporary_folder = tempfile.gettempdir() + folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) + os.makedirs(folder) + + origin = '%s/plain.jpg' % folder + shutil.copyfile(get_file('with-location.jpg'), origin) + + media = Photo(origin) + destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) + + shutil.rmtree(folder) + shutil.rmtree(os.path.dirname(os.path.dirname(destination))) + + assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain.jpg' in destination + +def test_process_file_with_location_and_title(): + temporary_folder = tempfile.gettempdir() + folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) + os.makedirs(folder) + + origin = '%s/plain.jpg' % folder + shutil.copyfile(get_file('with-location-and-title.jpg'), origin) + + media = Photo(origin) + destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) + + shutil.rmtree(folder) + shutil.rmtree(os.path.dirname(os.path.dirname(destination))) + + assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain-some-title.jpg' in destination + +def populate_folder(number_of_files): + 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) + + return folder + +def get_file(name): + current_folder = os.path.dirname(os.path.realpath(__file__)) + return '%s/files/%s' % (current_folder, name) def random_string(length): return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length))