#NEWLINE# def main( ) : #NEWLINE# #INDENT# 'Main function\n    \n    Returns:\n        None\n    ' #NEWLINE# global args , output_folder , source_feature_class , staging_feature_class , temp_workspace , logger #NEWLINE# parser = argparse . ArgumentParser ( fromfile_prefix_chars = '@' ) #NEWLINE# parser . add_argument ( '-o' , '--output-folder' , action = 'store' , dest = 'output_folder' , help = 'The root output folder in which to create the published files.  Sub-folders will automatically be created for each dataset (ex. \\\\myserver\\OpenDataCatalog).' ) #NEWLINE# parser . add_argument ( '-w' , '--temp-workspace' , action = 'store' , dest = 'temp_workspace' , help = 'The root workspace folder in which to create temporary output files. A local workspace will increase performance. (ex. C:\\temp).' ) #NEWLINE# parser . add_argument ( '-d' , '--download-url' , action = 'store' , dest = 'download_url' , help = 'The root path to the data download repository. (ex. http://data.denvergov.org/).' ) #NEWLINE# parser . add_argument ( '-s' , '--source-workspace' , action = 'store' , dest = 'source_workspace' , required = True , help = 'The source workspace to publish the feature class from (ex. Database Connections\\\\SDE Connection.sde).  Backslashes must be escaped as in the example.' ) #NEWLINE# parser . add_argument ( '-e' , '--exclude-fields' , action = 'store' , dest = 'exclude_fields' , help = 'Specifies a comma-delimited list of fields (columns) to remove from the dataset before publishing. (ex. TEMP_FIELD1,TEMP_FIELD2)' ) #NEWLINE# parser . add_argument ( '-f' , '--formats' , action = 'store' , dest = 'formats' , default = 'shp,dwg,kml,csv,metadata,gdb' , help = 'Specific formats to publish (shp=Shapefile, dwg=CAD drawing file, kml=Keyhole Markup Language, metadata=Metadata, gdb=File Geodatabase).  If not specified all formats will be published.' ) #NEWLINE# parser . add_argument ( '-a' , '--ckan-api' , action = 'store' , dest = 'ckan_api' , default = 'http://colorado.ckan.net/api/2/' , help = 'The root path to the CKAN repository (ex. http://colorado.ckan.net/api/2/)' ) #NEWLINE# parser . add_argument ( '-k' , '--ckan-api-key' , action = 'store' , dest = 'ckan_api_key' , required = True , help = 'The CKAN API key (get from http://colorado.ckan.net/user/me when logged in)' ) #NEWLINE# parser . add_argument ( '-p' , '--ckan-dataset-name-prefix' , action = 'store' , dest = 'ckan_dataset_name_prefix' , default = '' , help = 'A prefix used in conjunction with the dataset-name argument to create the complete dataset name on OpenColorado.' ) #NEWLINE# parser . add_argument ( '-t' , '--ckan-dataset-title-prefix' , action = 'store' , dest = 'ckan_dataset_title_prefix' , default = '' , help = 'A prefix used in conjunction with the dataset-title argument to create the complete dataset title on OpenColorado.' ) #NEWLINE# parser . add_argument ( '-g' , '--ckan-group-name' , action = 'store' , dest = 'ckan_group_name' , default = '' , help = 'The group name in the OpenColorado group register that the dataset will be added to.' ) #NEWLINE# parser . add_argument ( '-l' , '--ckan-license' , action = 'store' , dest = 'ckan_license' , default = 'cc-by' , help = 'The default data license type for the dataset.' ) #NEWLINE# parser . add_argument ( '-i' , '--ckan-increment-version' , action = 'store' , dest = 'increment' , choices = [ 'major' , 'minor' , 'revision' , 'none' ] , default = 'revision' , help = 'Update the version number on OpenColorado \n(default: %(default)s)' ) #NEWLINE# parser . add_argument ( '-m' , '--update-from-metadata' , action = 'store' , dest = 'update_from_metadata' , choices = [ 'description' , 'tags' , 'all' ] , help = 'Update dataset information using the source metadata' ) #NEWLINE# parser . add_argument ( '-x' , '--metadata-xslt' , action = 'store' , dest = 'metadata_xslt' , default = '..\\StyleSheets\\Format_FGDC.xslt' , help = 'The XSLT stylesheet to pass the FGDC CSDGM metadata through before publishing.' ) #NEWLINE# parser . add_argument ( '-r' , '--exe-result' , action = 'store' , dest = 'exe_result' , choices = [ 'export' , 'publish' , 'all' ] , default = 'all' , help = 'Result of executing this script; export data files only, publish to CKAN, or both.' ) #NEWLINE# parser . add_argument ( '-v' , '--log-level' , action = 'store' , dest = 'log_level' , choices = [ 'DEBUG' , 'INFO' , 'WARNING' , 'ERROR' , 'CRITICAL' , 'NOTSET' ] , default = 'INFO' , help = 'The level of detail to output to log files.' ) #NEWLINE# parser . add_argument ( '-b' , '--build-target' , action = 'store' , dest = 'build_target' , choices = [ 'TEST' , 'PROD' ] , default = 'TEST' , help = 'The server tier the script is running on. Only PROD supports email alerts.' ) #NEWLINE# parser . add_argument ( '-n' , '--gdb-version' , action = 'store' , dest = 'gdb_version' , choices = [ '9.2' , '9.3' , '10.0' , 'CURRENT' ] , default = '9.3' , help = 'The oldest version of Esri ArcGIS file geodatabases need to work with.' ) #NEWLINE# parser . add_argument ( 'feature_class' , action = 'store' , help = 'The fully qualified path to the feature class (ex. Database Connections\\\\SDE Connection.sde\\\\schema.parcels).  If a source workspace is specified (ex. -s Database Connections\\\\SDE Connection.sde) just the feature class name needs to be provided here (ex. schema.parcels)' ) #NEWLINE# parser . add_argument ( 'dataset_name' , action = 'store' , help = 'The name of the dataset on OpenColorado.  If a prefix is provided (-p) dont include it here.' ) #NEWLINE# parser . add_argument ( 'dataset_title' , action = 'store' , help = 'The title of the dataset on OpenColorado.  If a prefix is provided (-t) dont include it here.' ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# if ( args . output_folder != None ) : #NEWLINE# #INDENT# output_folder = args . output_folder . strip ( ) #NEWLINE# #UNINDENT# if ( args . temp_workspace != None ) : #NEWLINE# #INDENT# temp_workspace = args . temp_workspace . strip ( ) #NEWLINE# #UNINDENT# if ( args . source_workspace == None ) : #NEWLINE# #INDENT# source_feature_class = args . feature_class #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# source_feature_class = os . path . join ( args . source_workspace , args . feature_class ) #NEWLINE# #UNINDENT# if ( args . formats == None ) : #NEWLINE# #INDENT# args . formats = available_formats #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# args . formats = args . formats . split ( ',' ) #NEWLINE# for arg in args . formats : #NEWLINE# #INDENT# if ( not ( arg in available_formats ) ) : #NEWLINE# #INDENT# raise Exception ( str . format ( "Format type: '{0}' not supported" , arg ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# init_logger ( ) #NEWLINE# try : #NEWLINE# #INDENT# logger . info ( '============================================================' ) #NEWLINE# logger . info ( 'Starting PublishOpenDataset' ) #NEWLINE# logger . info ( 'Executing in directory: {0}' . format ( os . getcwd ( ) ) ) #NEWLINE# logger . info ( 'Featureclass: {0}' . format ( source_feature_class ) ) #NEWLINE# logger . info ( 'CKAN dataset name: {0}' . format ( args . dataset_name ) ) #NEWLINE# logger . info ( 'Download folder: {0}' . format ( output_folder ) ) #NEWLINE# logger . info ( 'Execution type: {0}' . format ( source_feature_class . exe_result ) ) #NEWLINE# logger . info ( 'Export formats: {0}' . format ( str ( args . formats ) ) ) #NEWLINE# delete_dataset_temp_folder ( ) #NEWLINE# output_folder = create_dataset_folder ( ) #NEWLINE# temp_workspace = create_dataset_temp_folder ( ) #NEWLINE# if ( args . exe_result != 'publish' ) : #NEWLINE# #INDENT# arcpy . env . outputCoordinateSystem = outCoordSystem #NEWLINE# arcpy . env . geographicTransformations = geographicTransformation #NEWLINE# if ( len ( args . formats ) > 0 ) : #NEWLINE# #INDENT# logger . info ( 'Exporting to file geodatabase' ) #NEWLINE# staging_feature_class = export_file_geodatabase ( ) #NEWLINE# drop_exclude_fields ( ) #NEWLINE# export_metadata ( ) #NEWLINE# #UNINDENT# if ( 'shp' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Exporting to shapefile' ) #NEWLINE# export_shapefile ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing shapefile for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'metadata' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Exporting metadata XML file' ) #NEWLINE# publish_metadata ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing metadata for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'gdb' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Publishing file geodatabase' ) #NEWLINE# publish_file_geodatabase ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing file geodatabase for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'dwg' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Exporting to CAD drawing file' ) #NEWLINE# export_cad ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing CAD for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'kml' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Exporting to KML file' ) #NEWLINE# export_kml ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing KML for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( 'csv' in args . formats ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# logger . info ( 'Exporting to CSV file' ) #NEWLINE# export_csv ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing CSV for dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( args . exe_result != 'export' ) : #NEWLINE# #INDENT# remove_missing_formats_from_publication ( output_folder ) #NEWLINE# if ( len ( args . formats ) > 0 ) : #NEWLINE# #INDENT# publish_to_ckan ( ) #NEWLINE# #UNINDENT# #UNINDENT# logger . info ( ( 'Done - PublishOpenDataset ' + args . dataset_name ) ) #NEWLINE# logger . info ( '============================================================' ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if logger : #NEWLINE# #INDENT# logger . exception ( 'Error publishing dataset {0}. {1} {2}' . format ( args . dataset_name , sys . exc_info ( ) [ 1 ] , sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# sys . exit ( 1 )