import os.path
import sys

import click

from .utils import default_state_options, load_module

@click.command(name='load.run', help="Load cached data files into the database")
@default_state_options
@click.argument('filenames', nargs=-1)
def run(state, datefilter='', filenames=[]):
    """
    Load cached data files into MongoDB.

    State is required. Optionally provide 'datefilter' to limit files that are loaded.
    """
    state_mod = load_module(state, ['datasource', 'load'])
    datasrc = state_mod.datasource.Datasource()
    loader = state_mod.load.LoadResults()

    if datefilter and len(filenames):
        sys.stderr.write("You must specify a datefilter or filename but not both")
        sys.exit(1)

    if len(filenames):
        # A filename was specified.  Load only this file.
        mappings = [datasrc.mapping_for_file(os.path.basename(fn))
                    for fn in filenames]
    else:
        # Load all files for the specified date filter
        mappings = datasrc.mappings(datefilter)

    #TODO: Notify user if there's a mismatch between expected files and
    # cache.diff
    for mapping in mappings:
        loader.run(mapping)

@@ -32,16 +32,33 @@
 rt click
+%0Aimport importlib
 %0A%0Afrom .
@@ -249,16 +249,77 @@
 rgs=-1)%0A
+@click.option('--place', help=%22the name of a place to load%22)%0A
 def run(
@@ -324,16 +324,23 @@
 n(state,
+ place,
  datefil
@@ -453,16 +453,58 @@
  provide
+:%0A    - 'place' to load place files %0A    -
  'datefi
@@ -553,115 +553,301 @@
 %22%22%22%0A
+%0A
     
-state_mod = load_module(state, %5B'datasource', 'load'%5D)%0A    datasrc = state_mod.datasource.Datasource(
+if datefilter and len(filenames):%0A        sys.stderr.write(%22You must specify a datefilter or filename but not both%22)%0A        sys.exit(1)%0A%0A    # load a place within a state%0A    if place:%0A        place_load_mod = importlib.import_module('openelex.us.%25s.places.%25s.load' %25(state, place)
 )%0A
+    
     
@@ -859,21 +859,21 @@
 r = 
-state_mod.loa
+place_load_mo
 d.Lo
@@ -888,155 +888,229 @@
 s()%0A
-%0A    if datefilter and len(filenames):%0A        sys.stderr.write(%22You must specify a datefilter or filename but not both%22)%0A        sys.exit(1)%0A%0A
+        loader.run()%0A%0A    # load a state%0A    else:%0A        state_mod = load_module(state, %5B'datasource', 'load'%5D)%0A        datasrc = state_mod.datasource.Datasource()%0A        loader = state_mod.load.LoadResults()%0A%0A   
     
+ 
 if l
@@ -1116,32 +1116,36 @@
 len(filenames):%0A
+    
         # A file
@@ -1186,32 +1186,36 @@
 s file.%0A        
+    
 mappings = %5Bdata
@@ -1273,24 +1273,28 @@
             
+    
 for fn in fi
@@ -1302,16 +1302,20 @@
 enames%5D%0A
+    
     else
@@ -1308,32 +1308,36 @@
 %5D%0A        else:%0A
+    
         # Load a
@@ -1375,32 +1375,36 @@
  filter%0A        
+    
 mappings = datas
@@ -1428,16 +1428,20 @@
 ilter)%0A%0A
+    
     #TOD
@@ -1508,16 +1508,20 @@
 and%0A    
+    
 # cache.
@@ -1525,16 +1525,20 @@
 he.diff%0A
+    
     for 
@@ -1558,16 +1558,20 @@
 ppings:%0A
+    
         

add place to load command