diff --git a/rmg.py b/rmg.py index cc4b3edb41..a6aa077c00 100755 --- a/rmg.py +++ b/rmg.py @@ -33,98 +33,15 @@ """ import os.path -import argparse import logging import rmgpy from rmgpy.rmg.main import RMG, initializeLog, processProfileStats, makeProfileGraph +from rmgpy.util import parse_command_line_arguments ################################################################################ -def parse_command_line_arguments(command_line_args=None): - """ - Parse the command-line arguments being passed to RMG Py. This uses the - :mod:`argparse` module, which ensures that the command-line arguments are - sensible, parses them, and returns them. - """ - - parser = argparse.ArgumentParser(description= - """ - Reaction Mechanism Generator (RMG) is an automatic chemical reaction - mechanism generator that constructs kinetic models composed of - elementary chemical reaction steps using a general understanding of - how molecules react. - """) - parser.add_argument('file', metavar='FILE', type=str, nargs=1, - help='a file describing the job to execute') - - # Options for controlling the amount of information printed to the console - # By default a moderate level of information is printed; you can either - # ask for less (quiet), more (verbose), or much more (debug) - group = parser.add_mutually_exclusive_group() - group.add_argument('-q', '--quiet', action='store_true', help='only print warnings and errors') - group.add_argument('-v', '--verbose', action='store_true', help='print more verbose output') - group.add_argument('-d', '--debug', action='store_true', help='print debug information') - - # Add options for controlling what directories files are written to - parser.add_argument('-o', '--output-directory', type=str, nargs=1, default='', - metavar='DIR', help='use DIR as output directory') - - # Add restart option - parser.add_argument('-r', '--restart', type=str, nargs=1, metavar='path/to/seed/', help='restart RMG from a seed', - default='') - - parser.add_argument('-p', '--profile', action='store_true', - help='run under cProfile to gather profiling statistics, and postprocess them if job completes') - parser.add_argument('-P', '--postprocess', action='store_true', - help='postprocess profiling statistics from previous [failed] run; does not run the simulation') - - parser.add_argument('-t', '--walltime', type=str, nargs=1, default='00:00:00:00', - metavar='DD:HH:MM:SS', help='set the maximum execution time') - - # Add option to select max number of processes for reaction generation - parser.add_argument('-n', '--maxproc', type=int, nargs=1, default=1, - help='max number of processes used during reaction generation') - - # Add option to output a folder that stores the details of each kinetic database entry source - parser.add_argument('-k', '--kineticsdatastore', action='store_true', - help='output a folder, kinetics_database, that contains a .txt file for each reaction family ' - 'listing the source(s) for each entry') - - args = parser.parse_args(command_line_args) - - # Process args to set correct default values and format - - # For output and scratch directories, if they are empty strings, set them - # to match the input file location - args.file = args.file[0] - - # If walltime was specified, retrieve this string from the element 1 list - if args.walltime != '00:00:00:00': - args.walltime = args.walltime[0] - - if args.restart: - args.restart = args.restart[0] - - if args.maxproc != 1: - args.maxproc = args.maxproc[0] - - # Set directories - input_directory = os.path.abspath(os.path.dirname(args.file)) - - if args.output_directory == '': - args.output_directory = input_directory - # If output directory was specified, retrieve this string from the element 1 list - else: - args.output_directory = args.output_directory[0] - - if args.postprocess: - args.profile = True - - return args - - def main(): # Parse the command-line arguments (requires the argparse module) args = parse_command_line_arguments() diff --git a/rmgpy/rmg/rmgTest.py b/rmgpy/rmg/rmgTest.py index 628b8139ab..50542bf38a 100644 --- a/rmgpy/rmg/rmgTest.py +++ b/rmgpy/rmg/rmgTest.py @@ -42,6 +42,7 @@ from rmgpy.data.base import ForbiddenStructures from rmg import * +from rmgpy.util import parse_command_line_arguments ################################################### class TestRMGWorkFlow(unittest.TestCase): diff --git a/rmgpy/tools/generate_reactions.py b/rmgpy/tools/generate_reactions.py index 1c0ea40e46..11f3c1e662 100644 --- a/rmgpy/tools/generate_reactions.py +++ b/rmgpy/tools/generate_reactions.py @@ -42,7 +42,7 @@ from rmgpy.rmg.main import initializeLog, RMG from rmgpy.chemkin import ChemkinWriter from rmgpy.rmg.output import OutputHTMLWriter -from rmg import parse_command_line_arguments +from rmgpy.util import parse_command_line_arguments def main(): diff --git a/rmgpy/util.py b/rmgpy/util.py index e055a17176..20ad7d3bf0 100644 --- a/rmgpy/util.py +++ b/rmgpy/util.py @@ -28,6 +28,7 @@ # # ############################################################################### +import argparse import os.path import shutil from functools import wraps @@ -129,3 +130,86 @@ def measure_time(*args, **kwargs): logging.info ("@timefn: {} took {:.2f} seconds".format(fn.func_name, t2 - t1)) return result return measure_time + + +def parse_command_line_arguments(command_line_args=None): + """ + Parse the command-line arguments being passed to RMG Py. This uses the + :mod:`argparse` module, which ensures that the command-line arguments are + sensible, parses them, and returns them. + """ + + parser = argparse.ArgumentParser(description= + """ + Reaction Mechanism Generator (RMG) is an automatic chemical reaction + mechanism generator that constructs kinetic models composed of + elementary chemical reaction steps using a general understanding of + how molecules react. + """) + parser.add_argument('file', metavar='FILE', type=str, nargs=1, + help='a file describing the job to execute') + + # Options for controlling the amount of information printed to the console + # By default a moderate level of information is printed; you can either + # ask for less (quiet), more (verbose), or much more (debug) + group = parser.add_mutually_exclusive_group() + group.add_argument('-q', '--quiet', action='store_true', help='only print warnings and errors') + group.add_argument('-v', '--verbose', action='store_true', help='print more verbose output') + group.add_argument('-d', '--debug', action='store_true', help='print debug information') + + # Add options for controlling what directories files are written to + parser.add_argument('-o', '--output-directory', type=str, nargs=1, default='', + metavar='DIR', help='use DIR as output directory') + + # Add restart option + parser.add_argument('-r', '--restart', type=str, nargs=1, metavar='path/to/seed/', help='restart RMG from a seed', + default='') + + parser.add_argument('-p', '--profile', action='store_true', + help='run under cProfile to gather profiling statistics, and postprocess them if job completes') + parser.add_argument('-P', '--postprocess', action='store_true', + help='postprocess profiling statistics from previous [failed] run; does not run the simulation') + + parser.add_argument('-t', '--walltime', type=str, nargs=1, default='00:00:00:00', + metavar='DD:HH:MM:SS', help='set the maximum execution time') + + # Add option to select max number of processes for reaction generation + parser.add_argument('-n', '--maxproc', type=int, nargs=1, default=1, + help='max number of processes used during reaction generation') + + # Add option to output a folder that stores the details of each kinetic database entry source + parser.add_argument('-k', '--kineticsdatastore', action='store_true', + help='output a folder, kinetics_database, that contains a .txt file for each reaction family ' + 'listing the source(s) for each entry') + + args = parser.parse_args(command_line_args) + + # Process args to set correct default values and format + + # For output and scratch directories, if they are empty strings, set them + # to match the input file location + args.file = args.file[0] + + # If walltime was specified, retrieve this string from the element 1 list + if args.walltime != '00:00:00:00': + args.walltime = args.walltime[0] + + if args.restart: + args.restart = args.restart[0] + + if args.maxproc != 1: + args.maxproc = args.maxproc[0] + + # Set directories + input_directory = os.path.abspath(os.path.dirname(args.file)) + + if args.output_directory == '': + args.output_directory = input_directory + # If output directory was specified, retrieve this string from the element 1 list + else: + args.output_directory = args.output_directory[0] + + if args.postprocess: + args.profile = True + + return args