예제 #1
0
def test_get_folder_path_with_custom_path():
    with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f:
        f.write("""
[MapQuest]
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx

[Directory]
date=%Y-%m-%d
location=%country-%state-%city
full_path=%date/%location
        """)
    if hasattr(load_config, 'config'):
        del load_config.config
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-location.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
예제 #2
0
def test_process_existing_file_without_changes():
    # gh-210
    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'plain.jpg')
    shutil.copyfile(helper.get_file('plain.jpg'), origin)

    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    assert helper.path_tz_fix(os.path.join('2015-12-Dec', 'Unknown Location', '2015-12-05_00-59-26-plain.jpg')) in destination, destination

    media_second = Photo(destination)
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)

    assert destination_second is None, destination_second

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
예제 #3
0
def test_get_folder_path_with_fallback_folder():
    with open('%s/config.ini-fallback' % gettempdir(), 'w') as f:
        f.write("""
[Directory]
year=%Y
month=%m
full_path=%year/%month/%album|%"No Album Fool"/%month
        """)


#full_path=%year/%album|"No Album"
    if hasattr(load_config, 'config'):
        del load_config.config
    filesystem = FileSystem()
    media = Photo(helper.get_file('plain.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path == os.path.join('2015', '12', 'No Album Fool', '12'), path
예제 #4
0
def test_delete_directory_if_empty_when_not_empty():
    filesystem = FileSystem()
    folder = os.path.join(helper.temp_dir(), helper.random_string(10),
                          helper.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)
예제 #5
0
def test_get_folder_path_definition_location_date():
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
        f.write("""
[Directory]
date=%Y-%m-%d
location=%country
full_path=%location/%date
        """)

    if hasattr(load_config, 'config'):
        del load_config.config
    filesystem = FileSystem()
    path_definition = filesystem.get_folder_path_definition()
    expected = [
        [('location', '%country')], [('date', '%Y-%m-%d')]
    ]
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path_definition == expected, path_definition
예제 #6
0
def test_get_file_name_custom_with_title():
    with open('%s/config.ini-filename-custom-with-title' % gettempdir(),
              'w') as f:
        f.write("""
[File]
date=%Y-%m-%d
name=%date-%original_name-%title.%extension
        """)
    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    media = Photo(helper.get_file('with-title.jpg'))
    file_name = filesystem.get_file_name(media)

    if hasattr(load_config, 'config'):
        del load_config.config

    assert file_name == helper.path_tz_fix(
        '2015-12-05-with-title-some-title.jpg'), file_name
예제 #7
0
def test_get_file_name_custom_with_upper_capitalization():
    with open('%s/config.ini-filename-custom-with-uppercase' % gettempdir(),
              'w') as f:
        f.write("""
[File]
date=%Y-%m-%d
name=%date-%original_name-%title.%extension
capitalization=upper
        """)
    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    media = Photo(helper.get_file('plain.jpg'))
    file_name = filesystem.get_file_name(media.get_metadata())

    if hasattr(load_config, 'config'):
        del load_config.config

    assert file_name == helper.path_tz_fix('2015-12-05-PLAIN.JPG'), file_name
예제 #8
0
def test_get_folder_path_definition_multi_level_custom():
    with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f:
        f.write("""
[Directory]
year=%Y
month=%M
full_path=%year/%album|%month|%"foo"/%month
        """)

    if hasattr(load_config, 'config'):
        del load_config.config
    filesystem = FileSystem()
    path_definition = filesystem.get_folder_path_definition()

    expected = [[('year', '%Y')],
                [('album', ''), ('month', '%M'), ('"foo"', '')],
                [('month', '%M')]]
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path_definition == expected, path_definition
예제 #9
0
def test_process_file_no_exif_date_is_correct_gh_330():
    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'photo.jpg')
    shutil.copyfile(helper.get_file('no-exif.jpg'), origin)

    atime = 1330712100
    utime = 1330712900
    os.utime(origin, (atime, utime))

    media = Photo(origin)
    metadata = media.get_metadata()

    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

    assert '/2012-03-Mar/' in destination, destination
    assert '/2012-03-02_18-28-20' in destination, destination
예제 #10
0
def test_process_file_validate_original_checksum():
    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'photo.jpg')
    shutil.copyfile(helper.get_file('plain.jpg'), origin)

    origin_checksum_preprocess = helper.checksum(origin)
    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    origin_checksum = helper.checksum(origin)
    destination_checksum = helper.checksum(destination)

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

    assert origin_checksum_preprocess is not None, origin_checksum_preprocess
    assert origin_checksum is not None, origin_checksum
    assert destination_checksum is not None, destination_checksum
    assert origin_checksum_preprocess == origin_checksum, (origin_checksum_preprocess, origin_checksum)
예제 #11
0
def test_get_folder_path_definition_with_more_than_two_levels():
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
        f.write("""
[Directory]
year=%Y
month=%m
day=%d
full_path=%year/%month/%day
        """)

    if hasattr(load_config, 'config'):
        del load_config.config
    filesystem = FileSystem()
    path_definition = filesystem.get_folder_path_definition()
    expected = [
        [('year', '%Y')], [('month', '%m')], [('day', '%d')]
    ]
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path_definition == expected, path_definition
예제 #12
0
def test_get_all_files_by_extension():
    filesystem = FileSystem()
    folder = helper.populate_folder(5)

    files = filesystem.get_all_files(folder)
    length = len(files)
    assert length == 5, length

    files = filesystem.get_all_files(folder, 'jpg')
    length = len(files)
    assert length == 3, length

    files = filesystem.get_all_files(folder, 'txt')
    length = len(files)
    assert length == 2, length

    files = filesystem.get_all_files(folder, 'gif')
    length = len(files)
    assert length == 0, length

    shutil.rmtree(folder)
예제 #13
0
def test_process_twice_more_than_two_levels_of_directories():
    with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f:
        f.write("""
[Directory]
year=%Y
month=%m
day=%d
full_path=%year/%month/%day
        """)

    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'plain.jpg')
    shutil.copyfile(helper.get_file('plain.jpg'), origin)

    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
    if hasattr(load_config, 'config'):
        del load_config.config

    assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination

    if hasattr(load_config, 'config'):
        del load_config.config

    media_second = Photo(destination)
    media_second.set_title('foo')
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)

    if hasattr(load_config, 'config'):
        del load_config.config

    assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
예제 #14
0
def test_process_file_with_plugin_runtime_error():
    with open('%s/config.ini-plugin-runtime-error' % gettempdir(), 'w') as f:
        f.write("""
[Plugins]
plugins=RuntimeError
        """)
    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'plain.jpg')
    shutil.copyfile(helper.get_file('plain.jpg'), origin)

    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    if hasattr(load_config, 'config'):
        del load_config.config

    assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination, destination
예제 #15
0
def test_process_file_with_album_and_title_and_location():
    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'photo.jpg')
    shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)

    origin_checksum_preprocess = helper.checksum(origin)
    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    origin_checksum = helper.checksum(origin)
    destination_checksum = helper.checksum(destination)

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

    assert origin_checksum_preprocess is not None
    assert origin_checksum is not None
    assert destination_checksum is not None
    assert origin_checksum_preprocess == origin_checksum
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
예제 #16
0
def test_process_video_with_album_then_title():
    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'movie.mov')
    shutil.copyfile(helper.get_file('video.mov'), origin)

    origin_checksum = helper.checksum(origin)

    media = Video(origin)
    media.set_album('test_album')
    media.set_title('test_title')
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    destination_checksum = helper.checksum(destination)

    shutil.rmtree(folder)
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

    assert origin_checksum is not None, origin_checksum
    assert origin_checksum != destination_checksum, destination_checksum
    assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination
예제 #17
0
def test_process_file_with_plugin_throw_error():
    with open('%s/config.ini-plugin-throw-error' % gettempdir(), 'w') as f:
        f.write("""
[Plugins]
plugins=ThrowError
        """)

    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    temporary_folder, folder = helper.create_working_folder()

    origin = os.path.join(folder,'plain.jpg')
    shutil.copyfile(helper.get_file('plain.jpg'), origin)

    media = Photo(origin)
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)

    if hasattr(load_config, 'config'):
        del load_config.config

    assert destination is None, destination
예제 #18
0
def test_get_folder_path_with_with_more_than_two_levels():
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
        f.write("""
[MapQuest]
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx

[Directory]
year=%Y
month=%m
location=%city, %state
full_path=%year/%month/%location
        """)

    if hasattr(load_config, 'config'):
        del load_config.config

    filesystem = FileSystem()
    media = Photo(helper.get_file('with-location.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())
    if hasattr(load_config, 'config'):
        del load_config.config

    assert path == os.path.join('2015','12','Sunnyvale, California'), path
예제 #19
0
from elodie import log
from elodie.compatability import _decode
from elodie.config import load_config
from elodie.filesystem import FileSystem
from elodie.localstorage import Db
from elodie.media.base import Base, get_all_subclasses
from elodie.media.media import Media
from elodie.media.text import Text
from elodie.media.audio import Audio
from elodie.media.photo import Photo
from elodie.media.video import Video
from elodie.plugins.plugins import Plugins
from elodie.result import Result


FILESYSTEM = FileSystem()


def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
    
    _file = _decode(_file)
    destination = _decode(destination)

    """Set file metadata and move it to destination.
    """
    if not os.path.exists(_file):
        log.warn('Could not find %s' % _file)
        log.all('{"source":"%s", "error_msg":"Could not find %s"}' %
                  (_file, _file))
        return
    # Check if the source, _file, is a child folder within destination
예제 #20
0
def test_get_folder_path_with_location_and_title():
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-location-and-title.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())

    assert path == os.path.join('2015-12-Dec', 'Sunnyvale'), path
예제 #21
0
def test_get_folder_path_with_title():
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-title.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())

    assert path == os.path.join('2015-12-Dec', 'Unknown Location'), path
예제 #22
0
def test_get_current_directory():
    filesystem = FileSystem()
    assert os.getcwd() == filesystem.get_current_directory()
예제 #23
0
def test_get_file_name_definition_default():
    filesystem = FileSystem()
    name_template, definition = filesystem.get_file_name_definition()

    assert name_template == '%date-%original_name-%title.%extension', name_template
    assert definition == [[('date', '%Y-%m-%d_%H-%M-%S')], [('original_name', '')], [('title', '')], [('extension', '')]], definition #noqa
예제 #24
0
def test_get_file_name_with_uppercase_and_spaces():
    filesystem = FileSystem()
    media = Photo(helper.get_file('Plain With Spaces And Uppercase 123.jpg'))
    file_name = filesystem.get_file_name(media.get_metadata())

    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain-with-spaces-and-uppercase-123.jpg'), file_name
예제 #25
0
def test_get_folder_path_plain():
    filesystem = FileSystem()
    media = Photo(helper.get_file('plain.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())

    assert path == '2015-12-Dec/Unknown Location', path
예제 #26
0
def test_get_file_name_with_original_name_title_exif():
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-filename-and-title-in-exif.jpg'))
    file_name = filesystem.get_file_name(media)

    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar-foobar-title.jpg'), file_name
예제 #27
0
def test_get_file_name_with_title():
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-title.jpg'))
    file_name = filesystem.get_file_name(media)

    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
예제 #28
0
def test_get_file_name_plain():
    filesystem = FileSystem()
    media = Photo(helper.get_file('plain.jpg'))
    file_name = filesystem.get_file_name(media)

    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
예제 #29
0
def test_create_directory_invalid_permissions():
    filesystem = FileSystem()
    status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')

    assert status == False
예제 #30
0
def test_get_folder_path_with_location():
    filesystem = FileSystem()
    media = Photo(helper.get_file('with-location.jpg'))
    path = filesystem.get_folder_path(media.get_metadata())

    assert path == '2015-12-Dec/Sunnyvale', path