|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +import textwrap |
| 7 | + |
| 8 | +import commentjson |
| 9 | + |
| 10 | + |
| 11 | +def get_input_paths(config_file="config/config.js", verbose=True): |
| 12 | + ''' |
| 13 | + ''' |
| 14 | + with open(config_file) as config_fh: |
| 15 | + config = commentjson.load(config_fh) |
| 16 | + |
| 17 | + #Should this also handle parameter_dir and output_spec file location? |
| 18 | + #At least for now, no. The matching 'set_input_paths' method should likely |
| 19 | + # only apply to this set of files. |
| 20 | + input_filenames = ['hist_climate_file', 'proj_climate_file', |
| 21 | + 'veg_class_file', 'drainage_file', 'soil_texture_file', |
| 22 | + 'co2_file', 'proj_co2_file', 'runmask_file', 'topo_file', |
| 23 | + 'fri_fire_file', 'hist_exp_fire_file', |
| 24 | + 'proj_exp_fire_file'] |
| 25 | + if verbose: |
| 26 | + print("Fetching input paths for filenames:", *input_filenames, sep=', ') |
| 27 | + |
| 28 | + paths = [] |
| 29 | + for filename in input_filenames: |
| 30 | +# path = os.path.dirname(config['IO'][filename]) |
| 31 | + path = config['IO'][filename] |
| 32 | + if path not in paths: |
| 33 | + paths.append(path) |
| 34 | + |
| 35 | + return paths |
| 36 | + |
| 37 | + |
| 38 | +def cmdline_define(): |
| 39 | + '''Define the command line interface and return the parser object.''' |
| 40 | + |
| 41 | + parser = argparse.ArgumentParser( |
| 42 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 43 | + description=textwrap.dedent(''' |
| 44 | + Helper script for a dvm-dos-tem config file. |
| 45 | + ''') |
| 46 | + ) |
| 47 | + parser.add_argument('file', nargs='?', metavar=('FILE'), |
| 48 | + help=textwrap.dedent('''The config file to operate on.''')) |
| 49 | + |
| 50 | + parser.add_argument('--verbose', action='store_true', |
| 51 | + help=textwrap.dedent('''Print info to stdout when script runs.''')) |
| 52 | + |
| 53 | + parser.add_argument('--get-input-paths', action='store_true', |
| 54 | + help=textwrap.dedent('''Returns all input file paths''')) |
| 55 | + |
| 56 | + return parser |
| 57 | + |
| 58 | +def cmdline_parse(argv=None): |
| 59 | + ''' |
| 60 | + The command line interface specification and parser for util config.py. |
| 61 | +
|
| 62 | + If parse_args(...) is called with argv=None, then parse_args(...) will |
| 63 | + use sys.argv[1:]. Otherwise argv is parsed according to the specification. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + argv : None or list of strings |
| 68 | + arguments that argparse library will parse; if None, then sys.argv[1:] are |
| 69 | + parsed. |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + args : Namespace generated by argparse |
| 74 | +
|
| 75 | + ''' |
| 76 | + parser = cmdline_define() |
| 77 | + |
| 78 | + args = parser.parse_args(argv) |
| 79 | + |
| 80 | + # print(argv) |
| 81 | + # print(args) |
| 82 | + |
| 83 | + # Force vebosity on if user requests showing data |
| 84 | + #if args.show: |
| 85 | + #args.verbose = True # ?? Not sure about this... |
| 86 | + |
| 87 | + if (args.file is None) or (not os.path.isfile(args.file)): |
| 88 | + parser.error("'{}' is an invalid path to a config file!".format(args.file)) |
| 89 | + |
| 90 | + return args |
| 91 | + |
| 92 | + |
| 93 | +def cmdline_run(args): |
| 94 | + ''' |
| 95 | + Executes based on the command line arguments. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + args : Namespace |
| 100 | + Should be a Namespace with all the appropriate arguments. |
| 101 | +
|
| 102 | + Returns |
| 103 | + ------- |
| 104 | + exit_code : int |
| 105 | + Non-zero if the program cannot complete successfully. |
| 106 | +
|
| 107 | + ''' |
| 108 | + if args.get_input_paths: |
| 109 | + get_input_paths(args.file, args.verbose) |
| 110 | + |
| 111 | + return 0 |
| 112 | + |
| 113 | + |
| 114 | +def cmdline_entry(argv=None): |
| 115 | + ''' |
| 116 | + Wrapper allowing for easier testing of the cmdline run and parse functions. |
| 117 | + ''' |
| 118 | + args = cmdline_parse(argv) |
| 119 | + return cmdline_run(args) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + sys.exit(cmdline_entry()) # this makes sure appropriate exit code is passed on. |
| 124 | + |
0 commit comments