Skip to content

Commit 0bef99b

Browse files
committed
Move parse_command_line_arguments to util.py
rmg.py is not installed as a module in the conda binary, so we cannot import this function
1 parent 11146ef commit 0bef99b

4 files changed

Lines changed: 85 additions & 85 deletions

File tree

rmg.py

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from __future__ import print_function
3535

3636
import os.path
37-
import argparse
3837
import logging
3938

4039
# Before importing any RMG modules, check Python version
@@ -43,92 +42,11 @@
4342

4443
import rmgpy
4544
from rmgpy.rmg.main import RMG, initialize_log, process_profile_stats, make_profile_graph
46-
45+
from rmgpy.util import parse_command_line_arguments
4746

4847
################################################################################
4948

5049

51-
def parse_command_line_arguments(command_line_args=None):
52-
"""
53-
Parse the command-line arguments being passed to RMG Py. This uses the
54-
:mod:`argparse` module, which ensures that the command-line arguments are
55-
sensible, parses them, and returns them.
56-
"""
57-
58-
parser = argparse.ArgumentParser(description='Reaction Mechanism Generator (RMG) is an automatic chemical reaction '
59-
'mechanism generator that constructs kinetic models composed of '
60-
'elementary chemical reaction steps using a general understanding of '
61-
'how molecules react.')
62-
63-
parser.add_argument('file', metavar='FILE', type=str, nargs=1,
64-
help='a file describing the job to execute')
65-
66-
# Options for controlling the amount of information printed to the console
67-
# By default a moderate level of information is printed; you can either
68-
# ask for less (quiet), more (verbose), or much more (debug)
69-
group = parser.add_mutually_exclusive_group()
70-
group.add_argument('-q', '--quiet', action='store_true', help='only print warnings and errors')
71-
group.add_argument('-v', '--verbose', action='store_true', help='print more verbose output')
72-
group.add_argument('-d', '--debug', action='store_true', help='print debug information')
73-
74-
# Add options for controlling what directories files are written to
75-
parser.add_argument('-o', '--output-directory', type=str, nargs=1, default='',
76-
metavar='DIR', help='use DIR as output directory')
77-
78-
# Add restart option
79-
parser.add_argument('-r', '--restart', type=str, nargs=1, metavar='path/to/seed/', help='restart RMG from a seed',
80-
default='')
81-
82-
parser.add_argument('-p', '--profile', action='store_true',
83-
help='run under cProfile to gather profiling statistics, and postprocess them if job completes')
84-
parser.add_argument('-P', '--postprocess', action='store_true',
85-
help='postprocess profiling statistics from previous [failed] run; does not run the simulation')
86-
87-
parser.add_argument('-t', '--walltime', type=str, nargs=1, default='00:00:00:00',
88-
metavar='DD:HH:MM:SS', help='set the maximum execution time')
89-
90-
# Add option to select max number of processes for reaction generation
91-
parser.add_argument('-n', '--maxproc', type=int, nargs=1, default=1,
92-
help='max number of processes used during reaction generation')
93-
94-
# Add option to output a folder that stores the details of each kinetic database entry source
95-
parser.add_argument('-k', '--kineticsdatastore', action='store_true',
96-
help='output a folder, kinetics_database, that contains a .txt file for each reaction family '
97-
'listing the source(s) for each entry')
98-
99-
args = parser.parse_args(command_line_args)
100-
101-
# Process args to set correct default values and format
102-
103-
# For output and scratch directories, if they are empty strings, set them
104-
# to match the input file location
105-
args.file = args.file[0]
106-
107-
# If walltime was specified, retrieve this string from the element 1 list
108-
if args.walltime != '00:00:00:00':
109-
args.walltime = args.walltime[0]
110-
111-
if args.restart:
112-
args.restart = args.restart[0]
113-
114-
if args.maxproc != 1:
115-
args.maxproc = args.maxproc[0]
116-
117-
# Set directories
118-
input_directory = os.path.abspath(os.path.dirname(args.file))
119-
120-
if args.output_directory == '':
121-
args.output_directory = input_directory
122-
# If output directory was specified, retrieve this string from the element 1 list
123-
else:
124-
args.output_directory = args.output_directory[0]
125-
126-
if args.postprocess:
127-
args.profile = True
128-
129-
return args
130-
131-
13250
def main():
13351
# Parse the command-line arguments (requires the argparse module)
13452
args = parse_command_line_arguments()

rmgpy/rmg/rmgTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import unittest
3333

3434
from external.wip import work_in_progress
35-
from rmg import parse_command_line_arguments
3635
from rmgpy import settings
3736
from rmgpy.data.base import ForbiddenStructures
3837
from rmgpy.data.rmg import RMGDatabase
@@ -41,6 +40,7 @@
4140
from rmgpy.rmg.main import RMG
4241
from rmgpy.rmg.model import CoreEdgeReactionModel
4342
from rmgpy.species import Species
43+
from rmgpy.util import parse_command_line_arguments
4444

4545

4646
###################################################

rmgpy/tools/generate_reactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
import logging
4040
import os.path
4141

42-
from rmg import parse_command_line_arguments
4342
from rmgpy.chemkin import ChemkinWriter
4443
from rmgpy.rmg.main import initialize_log, RMG
4544
from rmgpy.rmg.output import OutputHTMLWriter
45+
from rmgpy.util import parse_command_line_arguments
4646

4747

4848
def main():

rmgpy/util.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# #
2929
###############################################################################
3030

31+
import argparse
3132
import logging
3233
import os.path
3334
import shutil
@@ -137,6 +138,87 @@ def measure_time(*args, **kwargs):
137138
return measure_time
138139

139140

141+
def parse_command_line_arguments(command_line_args=None):
142+
"""
143+
Parse the command-line arguments being passed to RMG Py. This uses the
144+
:mod:`argparse` module, which ensures that the command-line arguments are
145+
sensible, parses them, and returns them.
146+
"""
147+
148+
parser = argparse.ArgumentParser(description='Reaction Mechanism Generator (RMG) is an automatic chemical reaction '
149+
'mechanism generator that constructs kinetic models composed of '
150+
'elementary chemical reaction steps using a general understanding of '
151+
'how molecules react.')
152+
153+
parser.add_argument('file', metavar='FILE', type=str, nargs=1,
154+
help='a file describing the job to execute')
155+
156+
# Options for controlling the amount of information printed to the console
157+
# By default a moderate level of information is printed; you can either
158+
# ask for less (quiet), more (verbose), or much more (debug)
159+
group = parser.add_mutually_exclusive_group()
160+
group.add_argument('-q', '--quiet', action='store_true', help='only print warnings and errors')
161+
group.add_argument('-v', '--verbose', action='store_true', help='print more verbose output')
162+
group.add_argument('-d', '--debug', action='store_true', help='print debug information')
163+
164+
# Add options for controlling what directories files are written to
165+
parser.add_argument('-o', '--output-directory', type=str, nargs=1, default='',
166+
metavar='DIR', help='use DIR as output directory')
167+
168+
# Add restart option
169+
parser.add_argument('-r', '--restart', type=str, nargs=1, metavar='path/to/seed/', help='restart RMG from a seed',
170+
default='')
171+
172+
parser.add_argument('-p', '--profile', action='store_true',
173+
help='run under cProfile to gather profiling statistics, and postprocess them if job completes')
174+
parser.add_argument('-P', '--postprocess', action='store_true',
175+
help='postprocess profiling statistics from previous [failed] run; does not run the simulation')
176+
177+
parser.add_argument('-t', '--walltime', type=str, nargs=1, default='00:00:00:00',
178+
metavar='DD:HH:MM:SS', help='set the maximum execution time')
179+
180+
# Add option to select max number of processes for reaction generation
181+
parser.add_argument('-n', '--maxproc', type=int, nargs=1, default=1,
182+
help='max number of processes used during reaction generation')
183+
184+
# Add option to output a folder that stores the details of each kinetic database entry source
185+
parser.add_argument('-k', '--kineticsdatastore', action='store_true',
186+
help='output a folder, kinetics_database, that contains a .txt file for each reaction family '
187+
'listing the source(s) for each entry')
188+
189+
args = parser.parse_args(command_line_args)
190+
191+
# Process args to set correct default values and format
192+
193+
# For output and scratch directories, if they are empty strings, set them
194+
# to match the input file location
195+
args.file = args.file[0]
196+
197+
# If walltime was specified, retrieve this string from the element 1 list
198+
if args.walltime != '00:00:00:00':
199+
args.walltime = args.walltime[0]
200+
201+
if args.restart:
202+
args.restart = args.restart[0]
203+
204+
if args.maxproc != 1:
205+
args.maxproc = args.maxproc[0]
206+
207+
# Set directories
208+
input_directory = os.path.abspath(os.path.dirname(args.file))
209+
210+
if args.output_directory == '':
211+
args.output_directory = input_directory
212+
# If output directory was specified, retrieve this string from the element 1 list
213+
else:
214+
args.output_directory = args.output_directory[0]
215+
216+
if args.postprocess:
217+
args.profile = True
218+
219+
return args
220+
221+
140222
def as_list(item, default=None):
141223
"""
142224
Wrap the given item in a list if it is not None and not already a list.

0 commit comments

Comments
 (0)