From d67b2e640133c45a31813ec853e58a866553134a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 10 Nov 2009 19:39:00 -0500 Subject: [PATCH] Set a default usage=None for the CLI.default_option_parser() function. Updated all of the scripts to use the default_option_parser(). --- bin/drag_wkt_along_kml_path | 19 ++----------------- bin/find_avg_population_density | 20 ++------------------ bin/lines2kml | 21 +++------------------ bin/pg2kml | 23 +++-------------------- bin/wkt2kml | 20 ++------------------ bin/wkt2pop | 21 ++------------------- src/CLI.py | 2 +- 7 files changed, 15 insertions(+), 111 deletions(-) diff --git a/bin/drag_wkt_along_kml_path b/bin/drag_wkt_along_kml_path index 072d72a..752a60f 100755 --- a/bin/drag_wkt_along_kml_path +++ b/bin/drag_wkt_along_kml_path @@ -11,13 +11,13 @@ import pgdb import os import site import sys -from optparse import OptionParser # Basically, add '../src' and '../lib/Shapely' to our path. # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../lib/Shapely') +import CLI import Configuration.Defaults import ExitCodes import Geometry @@ -26,26 +26,11 @@ import KML usage = '%prog [options] ' # -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(usage=usage, add_help_option = False) +parser = CLI.default_option_parser(usage) # Use this module's docstring as the description. parser.description = __doc__.strip() -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) - parser.add_option('-o', '--outfile', help='Optional output file path. Defaults to stdout.') diff --git a/bin/find_avg_population_density b/bin/find_avg_population_density index 712db20..ef60d22 100755 --- a/bin/find_avg_population_density +++ b/bin/find_avg_population_density @@ -7,13 +7,13 @@ Find the average population density of a set of GPS coordinates. import sys import os import site -from optparse import OptionParser # Basically, add '../src' to our path. # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') import Census +import CLI import Configuration.Defaults import ExitCodes import GPS @@ -38,27 +38,11 @@ for idx, value in enumerate(sys.argv): usage = '%prog [options] ' -# -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(usage=usage, add_help_option = False) +parser = CLI.default_option_parser(usage) # Use this module's docstring as the description. parser.description = __doc__.strip() -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) - parser.add_option('-s', '--srid', type="int", diff --git a/bin/lines2kml b/bin/lines2kml index bba63b2..3fc3d78 100755 --- a/bin/lines2kml +++ b/bin/lines2kml @@ -6,7 +6,6 @@ Language (KML). The exported roads should have both a human-readable name and a globally-unique identifier (gid). """ -from optparse import OptionParser import os import pgdb import site @@ -16,6 +15,7 @@ import sys # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') +import CLI import Configuration.Defaults import ExitCodes import KML @@ -26,27 +26,12 @@ Parse the command line options. All of these are optional; defaults are provided for the database information and the output is written to stdout. """ -# -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(add_help_option = False) + +parser = CLI.default_option_parser() # Use this module's docstring as the description. parser.description = __doc__.strip() -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) - parser.add_option('-s', '--state', help='The FIPS id of the State whose roads you would like.') diff --git a/bin/pg2kml b/bin/pg2kml index 81afeca..bef23fa 100755 --- a/bin/pg2kml +++ b/bin/pg2kml @@ -7,7 +7,6 @@ upon their blocks' average population densities. Output is written to stdout. """ -from optparse import OptionParser import os import pgdb import site @@ -17,6 +16,7 @@ import sys # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') +import CLI import Configuration.Defaults import ExitCodes import GPS @@ -29,31 +29,14 @@ Parse the command line options. All of these are optional; defaults are provided for the database information and the output is written to stdout. """ -# -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(add_help_option = False) + +parser = CLI.default_option_parser() # Use this module's docstring as the description. parser.description = __doc__.strip() - -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) - (options, args) = parser.parse_args() - def GenerateRedStyles(alpha='7f'): """ Generate 256 styles (0-255), each corresponding to a shade of red. diff --git a/bin/wkt2kml b/bin/wkt2kml index 598fb52..81b45ff 100755 --- a/bin/wkt2kml +++ b/bin/wkt2kml @@ -18,7 +18,6 @@ conversions correctly, sans-database. [2] http://search.cpan.org/dist/Geo-Converter-WKT2KML/ """ -from optparse import OptionParser import os import pgdb import site @@ -28,6 +27,7 @@ import sys # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') +import CLI import Configuration.Defaults import ExitCodes import GPS @@ -45,27 +45,11 @@ default. The name option is available in case the user would like to e.g. see the object name in Google Earth. """ -# -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(add_help_option = False) +parser = CLI.default_option_parser() # Use this module's docstring as the description. parser.description = __doc__.strip() -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) - parser.add_option('-o', '--outfile', help='Optional output file path. Defaults to stdout.') diff --git a/bin/wkt2pop b/bin/wkt2pop index 6b6290e..3fa70b2 100755 --- a/bin/wkt2pop +++ b/bin/wkt2pop @@ -16,38 +16,22 @@ string. import sys import os import site -from optparse import OptionParser # Basically, add '../src' to our path. # Needed for the imports that follow. site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') import Census +import CLI import Configuration.Defaults import ExitCodes usage = '%prog [options] ' - -# -h (help) Conflicts with -h HOSTNAME -parser = OptionParser(usage=usage, add_help_option = False) +parser = CLI.default_option_parser(usage) # Use this module's docstring as the description. parser.description = __doc__.strip() -parser.add_option('-h', - '--host', - help='The hostname/address where the database is located.', - default=Configuration.Defaults.DATABASE_HOST) - -parser.add_option('-d', - '--database', - help='The database in which the population data are stored.', - default=Configuration.Defaults.DATABASE_NAME) - -parser.add_option('-U', - '--username', - help='The username who has access to the database.', - default=Configuration.Defaults.DATABASE_USERNAME) parser.add_option('-s', '--srid', @@ -77,4 +61,3 @@ if (population != None): else: print 'Error: No rows returned.' raise SystemExit(ExitCodes.NO_RESULTS) - diff --git a/src/CLI.py b/src/CLI.py index eb8b219..b7fcaaf 100644 --- a/src/CLI.py +++ b/src/CLI.py @@ -3,7 +3,7 @@ from optparse import OptionParser import Configuration.Defaults -def default_option_parser(usage): +def default_option_parser(usage=None): # -h (help) Conflicts with -h HOSTNAME parser = OptionParser(usage=usage, add_help_option = False) -- 2.43.2