Skip to content

Commit

Permalink
Add method/tests to get a customizable folder path
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Dec 23, 2016
1 parent 2b18f3f commit be25516
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
2 changes: 1 addition & 1 deletion elodie/config.py
Expand Up @@ -12,7 +12,7 @@ def load_config():
return load_config.config

if not path.exists(config_file):
return None
return {}

load_config.config = RawConfigParser()
load_config.config.read(config_file)
Expand Down
50 changes: 37 additions & 13 deletions elodie/filesystem.py
Expand Up @@ -11,37 +11,36 @@
import shutil
import time
from os import path
from configparser import RawConfigParser

from elodie import geolocation
from elodie import constants
from elodie import geolocation
from elodie import log
from elodie.config import load_config
from elodie.localstorage import Db

__DIR__ = None

def get_dir():
global __DIR__
default_dir = "%Y-%m-%b"
if __DIR__ is not None:
return __DIR__

config_file = '%s/config.ini' % constants.application_directory
if not path.exists(config_file):
return default_dir

config = RawConfigParser()
config.read(config_file)
if('Directory' not in config.sections()):
config = load_config()
if('Directory' not in config or 'date' not in config['Directory']):
return default_dir

__DIR__ = config.get('Directory', 'dir')
return __DIR__
return config.get['Directory']['dir']

class FileSystem(object):

class FileSystem(object):
"""A class for interacting with the file system."""

def __init__(self):
# The default folder path is along the lines of 2015-01-Jan/Chicago
self.default_folder_path_definition = [
('date', '%Y-%m-%b'), ('location', '%city')
]

def create_directory(self, directory_path):
"""Create a directory if it does not already exist.
Expand Down Expand Up @@ -152,6 +151,7 @@ def get_file_name(self, media):
metadata['extension'])
return file_name.lower()

# gh-160 remove?
def get_folder_name_by_date(self, time_obj):
"""Get date based folder name.
Expand All @@ -161,6 +161,30 @@ def get_folder_name_by_date(self, time_obj):
dir = get_dir()
return time.strftime(dir, time_obj)

def get_folder_path_definition(self):
config = load_config()

# If Directory is in the config we assume full_path and its
# corresponding values (date, location) are als present
if('Directory' not in config):
return self.default_folder_path_definition

config_directory = config['Directory']

path_parts = re.search(
'\%([^/]+)\/\%([^/]+)',
config_directory['full_path']
)

if not path_parts or len(path_parts.groups()) != 2:
return self.default_folder_path_definition

path_part_groups = path_parts.groups()
return [
(path_part_groups[0], config_directory[path_part_groups[0]]),
(path_part_groups[1], config_directory[path_part_groups[1]]),
]

def get_folder_path(self, metadata):
"""Get folder path by various parameters.
Expand Down
5 changes: 5 additions & 0 deletions elodie/tests/config_test.py
Expand Up @@ -24,3 +24,8 @@ def test_load_config_singleton_success():
assert config['MapQuest']['key'] == 'new-value', config.get('MapQuest', 'key')

del load_config.config

@patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % BASE_PATH)
def test_load_config_singleton_no_file():
config = load_config()
assert config == {}, config
62 changes: 59 additions & 3 deletions elodie/tests/filesystem_test.py
@@ -1,18 +1,19 @@
from __future__ import absolute_import
# Project imports
import mock
import os
import re
import shutil
import time
import sys
import time
from datetime import datetime
from datetime import timedelta

import mock
from tempfile import gettempdir

sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))

from . import helper
from elodie.config import load_config
from elodie.filesystem import FileSystem
from elodie.media.media import Media
from elodie.media.photo import Photo
Expand Down Expand Up @@ -422,3 +423,58 @@ def test_set_utime_without_exif_date():
assert initial_time == final_stat.st_mtime
assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken']))
assert initial_checksum == final_checksum

@mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir())
def test_get_folder_path_definition_default():
if hasattr(load_config, 'config'):
del load_config.config
filesystem = FileSystem()
path_definition = filesystem.get_folder_path_definition()
if hasattr(load_config, 'config'):
del load_config.config

assert path_definition == filesystem.default_folder_path_definition, path_definition

@mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir())
def test_get_folder_path_definition_date_location():
with open('%s/config.ini-date-location' % gettempdir(), 'w') as f:
f.write("""
[Directory]
date=%Y-%m-%d
location=%country
full_path=%date/%location
""")

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

assert path_definition == expected, path_definition

@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
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

0 comments on commit be25516

Please sign in to comment.