Fix bug where code makes assumption of 2 levels of folders in the update command. #206 (#207)

This commit is contained in:
Jaisen Mathai 2017-04-01 16:26:29 +05:30 committed by GitHub
parent 74d8675b20
commit 8082b739af
4 changed files with 52 additions and 18 deletions

View File

@ -207,7 +207,7 @@ Not too bad, eh? Wait a second, what's *Unknown Location*? If I'm not able to fi
OK, so what if you don't like the folders being named `2015-07-Jul/Mountain View`? No problem!
You can add a custom folder structure by editing your `config.ini` file. This is what I include in the sample config file.
You can add a custom folder structure by editing your `config.ini` file.
#### Custom folder examples
@ -217,28 +217,23 @@ Sometimes examples are easier to understand than explainations so I'll start the
location=%city, %state
year=%Y
full_path=%year/%location
# 2015/Sunnyvale, California
# -> 2015/Sunnyvale, California
location=%city, %state
month=%B
year=%Y
full_path=%year/%month/%location
# 2015/December/Sunnyvale, California
# -> 2015/December/Sunnyvale, California
location=%city, %state
month=%m
year=%Y
date=%year-%month
full_path=%date/%location
# 2015-12/Sunnyvale, California
# -> 2015-12/Sunnyvale, California
full_path=%country/%state/%city
# US/California/Sunnyvale
# -> US/California/Sunnyvale
```
#### How folder customization works

View File

@ -1,8 +1,2 @@
[MapQuest]
key=your-api-key-goes-here
[Directory]
date=%Y-%m-%b
location=%city
full_path=%date/%location

View File

@ -236,8 +236,18 @@ def _update(album, location, time, title, paths):
continue
current_file = os.path.expanduser(current_file)
destination = os.path.expanduser(os.path.dirname(os.path.dirname(
os.path.dirname(current_file))))
# The destination folder structure could contain any number of levels
# So we calculate that and traverse up the tree.
# '/path/to/file/photo.jpg' -> '/path/to/file' ->
# ['path','to','file'] -> ['path','to'] -> '/path/to'
current_directory = os.path.dirname(current_file)
destination_depth = -1 * len(FILESYSTEM.get_folder_path_definition())
destination = os.sep.join(
os.path.normpath(
current_directory
).split(os.sep)[:destination_depth]
)
media = Media.get_class_by_file(current_file, get_all_subclasses())
if not media:

View File

@ -1,5 +1,6 @@
# Project imports
from imp import load_source
import mock
import os
import sys
import shutil
@ -8,6 +9,7 @@ from click.testing import CliRunner
from nose.plugins.skip import SkipTest
from nose.tools import assert_raises
from six import text_type, unichr as six_unichr
from tempfile import gettempdir
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))))
@ -15,6 +17,7 @@ sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirna
import helper
elodie = load_source('elodie', os.path.abspath('{}/../../elodie.py'.format(os.path.dirname(os.path.realpath(__file__)))))
from elodie.config import load_config
from elodie.localstorage import Db
from elodie.media.audio import Audio
from elodie.media.photo import Photo
@ -427,6 +430,38 @@ def test_update_time_on_video():
assert metadata['date_taken'] != metadata_processed['date_taken']
assert metadata_processed['date_taken'] == helper.time_convert((2000, 1, 1, 12, 0, 0, 5, 1, 0)), metadata_processed['date_taken']
@mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir())
def test_update_with_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
""")
temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder()
origin = '%s/plain.jpg' % folder
shutil.copyfile(helper.get_file('plain.jpg'), origin)
if hasattr(load_config, 'config'):
del load_config.config
cfg = load_config()
helper.reset_dbs()
runner = CliRunner()
result = runner.invoke(elodie._import, ['--destination', folder_destination, folder])
runner2 = CliRunner()
result = runner2.invoke(elodie._update, ['--title', 'test title', folder_destination])
helper.restore_dbs()
if hasattr(load_config, 'config'):
del load_config.config
updated_file_path = '{}/2015/12/05/2015-12-05_00-59-26-plain-test-title.jpg'.format(folder_destination)
assert os.path.isfile(updated_file_path), updated_file_path
def test_update_with_directory_passed_in():
temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder()