#!/usr/bin/env python

import os
import shutil
import sys

from elodie import arguments
from elodie.media.photo import Photo
from elodie.media.video import Video
from elodie.filesystem import FileSystem
from elodie.localstorage import Db

def help():
    return """
    usage: ./import.py --type=photo --source=/path/to/photos --destination=/path/to/destination

    --type          Valid values are 'photo' or 'video'. Only files of *type* are imported.
    --file          Full path to a photo or video to be imported. The --type argument should match the file type of the file.
                    @TODO: Automatically determine *type* from *file*
    --source        Full path to a directory which will be recursively crawled for files of *type*.
    --destination   Full path to a directory where organized photos will be placed.
    """

def parse_arguments(args):
    config = {
        'type': 'photo',
        'file': None,
        'source': None,
        'destination': None
    }

    if('destination' not in args):
        help()
        sys.exit(2)
    
    config.update(args)
    return config

def main(argv):

    destination = config['destination']
    if(config['type'] == 'photo'):
        media_type = Photo
    else:
        media_type = Video

    if(config['source'] is not None):
        source = config['source']

        write_counter = 0
        for current_file in filesystem.get_all_files(source, media_type.get_valid_extensions()):
            media = media_type(current_file)

            if(media_type.__name__ == 'Video'):
                filesystem.set_date_from_path_video(media)

            dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False)
            print '%s -> %s' % (current_file, dest_path)
            # Write to the hash database every 10 iterations
            write_counter += 1
            if(write_counter % 10 == 0):
                db.update_hash_db()

        # If there's anything we haven't written to the hash database then write it now
        if(write_counter % 10 != 10):
            db.update_hash_db()
    elif(config['file'] is not None):
        media = media_type(config['file'])
        if(media_type.__name__ == 'Video'):
            filesystem.set_date_from_path_video(media)

        dest_path = filesystem.process_file(config['file'], destination, media, allowDuplicate=False, move=False)
        print '%s -> %s' % (current_file, dest_path)
        db.update_hash_db()
    else:
        help()

db = Db()
filesystem = FileSystem()
args = arguments.parse(sys.argv[1:], None, ['file=','type=','source=','destination='], help())
config = parse_arguments(args)

if __name__ == '__main__':
    main(config)
    sys.exit(0)

@@ -1337,34 +1337,8 @@
 '%5D%0A%0A
-        write_counter = 0%0A
     
@@ -1430,16 +1430,16 @@
 ons()):%0A
+
         
@@ -1692,32 +1692,75 @@
 se, move=False)%0A
+            if(dest_path is not None):%0A    
             prin
@@ -1804,336 +1804,8 @@
 th)%0A
-            # Write to the hash database every 10 iterations%0A            write_counter += 1%0A            if(write_counter %25 10 == 0):%0A                db.update_hash_db()%0A%0A        # If there's anything we haven't written to the hash database then write it now%0A        if(write_counter %25 10 != 10):%0A            db.update_hash_db()%0A
     
@@ -2087,32 +2087,71 @@
 se, move=False)%0A
+        if(dest_path is not None):%0A    
         print '%25
@@ -2191,36 +2191,8 @@
 th)%0A
-        db.update_hash_db()%0A
     

Update import script to not update hash db and let the filesystem module do it