Skip to content

Commit

Permalink
Merge 2b18f3f into 5b4c742
Browse files Browse the repository at this point in the history
  • Loading branch information
edent committed Dec 21, 2016
2 parents 5b4c742 + 2b18f3f commit 27413d1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ cp config.ini-sample ~/.elodie/config.ini
# now you're ready to add your MapQuest key
```

## Custom folder structured

OK, so what if you don't like the folders being named "2016-01-Jan"? No problem!

You can add a custom, date based folder structure by editing `~/.elodie/config.ini`

By default, you'll see:

```
[Directory]
dir=%Y-%m-%b
```

### Examples

* To have just `201601`, use `dir=%Y%m`
* For `Sunday, 01 January 2016`, use `dir=%A, %d %B %Y`
* Python also has some pre-built formats. So you can get `Sun Jan 01 12:34:56 2016`, by using `%c`

You can use any of [the standard Python time directives](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) to create your ideal structure.

## Questions, comments or concerns?

The best ways to provide feedback is by reaching out on Twitter at [@getelodie](https://twitter.com/getelodie), opening a [GitHub issue](https://github.com/jmathai/elodie/issues) or emailing me at [jaisen@jmathai.com](mailto:jaisen@jmathai.com).
2 changes: 2 additions & 0 deletions config.ini-sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[MapQuest]
key=your-api-key-goes-here
[Directory]
dir=%Y-%m-%b
19 changes: 19 additions & 0 deletions elodie/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Load config file as a singleton."""
from configparser import RawConfigParser
from os import path

from elodie import constants

config_file = '%s/config.ini' % constants.application_directory


def load_config():
if hasattr(load_config, "config"):
return load_config.config

if not path.exists(config_file):
return None

load_config.config = RawConfigParser()
load_config.config.read(config_file)
return load_config.config
28 changes: 26 additions & 2 deletions elodie/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,34 @@
import re
import shutil
import time
from os import path
from configparser import RawConfigParser

from elodie import geolocation
from elodie import constants
from elodie import log
from elodie.localstorage import Db
from elodie.media.base import Base, get_all_subclasses

__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()):
return default_dir

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

class FileSystem(object):

Expand Down Expand Up @@ -140,17 +162,19 @@ def get_folder_name_by_date(self, time_obj):
:param time time_obj: Time object to be used to determine folder name.
:returns: str
"""
return time.strftime('%Y-%m-%b', time_obj)
dir = get_dir()
return time.strftime(dir, time_obj)

def get_folder_path(self, metadata):
"""Get folder path by various parameters.
:param time time_obj: Time object to be used to determine folder name.
:returns: str
"""
dir = get_dir()
path = []
if(metadata['date_taken'] is not None):
path.append(time.strftime('%Y-%m-%b', metadata['date_taken']))
path.append(time.strftime(dir, metadata['date_taken']))

if(metadata['album'] is not None):
path.append(metadata['album'])
Expand Down
9 changes: 4 additions & 5 deletions elodie/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
standard_library.install_aliases() # noqa

from os import path
from configparser import ConfigParser

import requests
import urllib.request
import urllib.parse
import urllib.error

from elodie.config import load_config
from elodie import constants
from elodie import log
from elodie.localstorage import Db
Expand Down Expand Up @@ -106,12 +106,11 @@ def get_key():
if not path.exists(config_file):
return None

config = ConfigParser()
config.read(config_file)
if('MapQuest' not in config.sections()):
config = load_config()
if('MapQuest' not in config):
return None

__KEY__ = config.get('MapQuest', 'key')
__KEY__ = config['MapQuest']['key']
return __KEY__


Expand Down
26 changes: 26 additions & 0 deletions elodie/tests/config_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import absolute_import
# Project imports

import os
import sys
import unittest

from mock import patch

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

from elodie import constants
from elodie.config import load_config

BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

@patch('elodie.config.config_file', '%s/config.ini-sample' % BASE_PATH)
def test_load_config_singleton_success():
config = load_config()
assert config['MapQuest']['key'] == 'your-api-key-goes-here', config.get('MapQuest', 'key')
config.set('MapQuest', 'key', 'new-value')

config = load_config()
assert config['MapQuest']['key'] == 'new-value', config.get('MapQuest', 'key')

del load_config.config

0 comments on commit 27413d1

Please sign in to comment.