Skip to content

Commit

Permalink
Move parse_command_line_arguments to util.py
Browse files Browse the repository at this point in the history
rmg.py is not installed as a module in the conda binary, so we cannot import this function
  • Loading branch information
amarkpayne committed Sep 5, 2019
1 parent 3846fcc commit a12dc4e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 85 deletions.
85 changes: 1 addition & 84 deletions rmg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions rmgpy/rmg/rmgTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion rmgpy/tools/generate_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
84 changes: 84 additions & 0 deletions rmgpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# #
###############################################################################

import argparse
import os.path
import shutil
from functools import wraps
Expand Down Expand Up @@ -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

0 comments on commit a12dc4e

Please sign in to comment.