#!python

from database import Database
from importer import Importer
import os
import argparse
import glob


# Find directory from command arguments
if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description='Importer for VicMap datasets to PostGIS')
    parser.add_argument(
        'folder', help='Name of subfolder or VicMap dataset, eg VMADMIN')
    parser.add_argument(
        'dataset', nargs='?', help='Optional specific dataset from subfolder, eg lga_polygon. If omitted whole folder will be imported.')
    parser.add_argument('--recreate', action='store_true', default=False,
                        help='Forces dropping any existing tables and recreating. Be careful!')
    parser.add_argument('--skipshpimport', action='store_true', default=False,
                        help='Skips the initial shp file import to a temporary table in the public schema. For debugging only.')
    args = parser.parse_args()

    folder = args.folder  # .lower()
    if args.dataset:
        dataset = args.dataset.lower()
    else:
        dataset = None
    recreate = args.recreate
    skip_shape_import = args.skipshpimport
    print "Importing from {}".format(folder.upper())
    if dataset:
        print "  Dataset: {}".format(dataset.upper())
    if recreate:
        print "  Existing table definitions will be removed!"

    datasets = []
    if dataset:
        shape_file = os.path.join(folder, 'layer', '{}.shp'.format(dataset))
        if os.path.isfile(shape_file):
            datasets.append(shape_file)
        else:
            dbf_file = os.path.join(folder, 'table', '{}.dbf'.format(dataset))
            if os.path.isfile(dbf_file):
                datasets.append(dbf_file)
    else:
        for s in glob.glob(os.path.join(folder, 'layer', '*.shp')):
            datasets.append(s)
        for s in glob.glob(os.path.join(folder, 'table', '*.dbf')):
            datasets.append(s)

    print "\nDatasets to be processed are:"
    for d in datasets:
        print d

    i = Importer(Database())
    i.recreate = recreate
    i.skip_shape_import = skip_shape_import
    i.setupDatabase()

    for idx, d in enumerate(datasets):
        print "\n\nImporting {}/{}: {}\n-------------".format(idx + 1, len(datasets), d)
        path, file = os.path.split(d)
        layer = file[:-4]

        i.importLayer(d, os.path.basename(
            os.path.normpath(folder)).lower(), layer)

@@ -1390,23 +1390,21 @@
 %0D%0A%0D%0A    
-dataset
+layer
 s = %5B%5D%0D%0A
@@ -1412,32 +1412,102 @@
    if dataset:%0D%0A
+        dataset = os.path.basename(os.path.normpath(folder)).lower()%0D%0A
         shape_fi
@@ -1612,39 +1612,37 @@
 ):%0D%0A            
-dataset
+layer
 s.append(shape_f
@@ -1634,16 +1634,46 @@
 .append(
+%7B'dataset': dataset, 'layer': 
 shape_fi
@@ -1670,24 +1670,25 @@
 : shape_file
+%7D
 )%0D%0A        e
@@ -1823,39 +1823,37 @@
                 
-dataset
+layer
 s.append(dbf_fil
@@ -1845,24 +1845,54 @@
 .append(
+%7B'dataset': dataset, 'layer': 
 dbf_file
 )%0D%0A    e
@@ -1883,16 +1883,17 @@
 dbf_file
+%7D
 )%0D%0A    e
@@ -1890,32 +1890,102 @@
 e%7D)%0D%0A    else:%0D%0A
+        dataset = os.path.basename(os.path.normpath(folder)).lower()%0D%0A
         for s in
@@ -2041,39 +2041,37 @@
 ):%0D%0A            
-dataset
+layer
 s.append(s)%0D%0A   
@@ -2063,17 +2063,48 @@
 .append(
-s
+%7B'dataset': dataset, 'layer': s%7D
 )%0D%0A     
@@ -2183,25 +2183,489 @@
     
-datasets.append(s
+layers.append(%7B'dataset': dataset, 'layer': s%7D)%0D%0A%0D%0A    if len(layers) == 0:%0D%0A        for d in %5Bname for name in os.listdir(folder)%0D%0A                  if os.path.isdir(os.path.join(folder, name))%5D:%0D%0A            for s in glob.glob(os.path.join(folder, d, 'layer', '*.shp')):%0D%0A                layers.append(%7B'dataset': d.lower(), 'layer': s%7D)%0D%0A            for s in glob.glob(os.path.join(folder, d, 'table', '*.dbf')):%0D%0A                layers.append(%7B'dataset': d.lower(), 'layer': s%7D
 )%0D%0A%0D
@@ -2678,23 +2678,21 @@
 rint %22%5Cn
-Dataset
+Layer
 s to be 
@@ -2720,20 +2720,18 @@
 for 
-d
+l
  in 
-dataset
+layer
 s:%0D%0A
@@ -2744,17 +2744,57 @@
   print 
-d
+'%7B%7D: %7B%7D'.format(l%5B'dataset'%5D, l%5B'layer'%5D)
 %0D%0A%0D%0A    
@@ -2929,17 +2929,17 @@
 or idx, 
-d
+l
  in enum
@@ -2944,23 +2944,21 @@
 umerate(
-dataset
+layer
 s):%0D%0A   
@@ -3033,20 +3033,27 @@
 len(
-datasets), d
+layers), l%5B'layer'%5D
 )%0D%0A 
@@ -3086,17 +3086,26 @@
 h.split(
-d
+l%5B'layer'%5D
 )%0D%0A     
@@ -3154,75 +3154,32 @@
 yer(
-d, os.path.basename(%0D%0A            os.path.normpath(folder)).lower()
+l%5B'layer'%5D, l%5B'dataset'%5D
 , la

Support importing multiple datasets at once