Skip to content

Commit

Permalink
Merge pull request #18 from gvallarelli/bug-847818_command_line_runne…
Browse files Browse the repository at this point in the history
…r_mt

bug-847818_command_line_runner_mt
  • Loading branch information
gvallarelli committed Nov 8, 2011
2 parents e43134e + 808228f commit 959f03c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 36 deletions.
65 changes: 29 additions & 36 deletions main.py
@@ -1,41 +1,34 @@
import sys
import os
import argparse
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010-2011, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenQuake. If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.txt> for a copy of the LGPLv3 License.

from mtoolkit.workflow import Context, PipeLineBuilder


def build_cmd_parser():
"""Create a simple parser for cmdline"""

parser = argparse.ArgumentParser(prog='MToolkit')
parser.add_argument('-i', '--input-file',
dest='input_file',
nargs=1,
metavar='input file',
help="""Specify the configuration
file (i.e. config.yml)""")
parser.add_argument('-v', '--version',
action='version',
version="%(prog)s 0.0.1")
return parser
from mtoolkit.console import cmd_line
from mtoolkit.workflow import Context, PipeLineBuilder


if __name__ == '__main__':
PARSER = build_cmd_parser()
if len(sys.argv) == 1:
PARSER.print_help()
else:
ARGS = PARSER.parse_args()
if ARGS.input_file != None:
if os.path.exists(ARGS.input_file[0]):
CONTEXT = Context("config.yml")
PIPELINE = PipeLineBuilder("test pipeline").build(
CONTEXT.config)
PIPELINE.run(CONTEXT)
print CONTEXT.vcl
print CONTEXT.vmain_shock
print CONTEXT.flag_vector
else:
print 'Error: non existent input file\n'
PARSER.print_help()
INPUT_CONFIG_FILENAME = cmd_line()
if INPUT_CONFIG_FILENAME != None:
CONTEXT = Context(INPUT_CONFIG_FILENAME)
PIPELINE = PipeLineBuilder("test pipeline").build(
CONTEXT.config)
PIPELINE.run(CONTEXT)

print CONTEXT.vcl
print CONTEXT.vmain_shock
print CONTEXT.flag_vector
65 changes: 65 additions & 0 deletions mtoolkit/console.py
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010-2011, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenQuake. If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.txt> for a copy of the LGPLv3 License.

"""
A set of utility functions for cmdline
"""

import os
import sys
import argparse


def build_cmd_parser():
"""
Create a simple parser for cmdline
"""

parser = argparse.ArgumentParser(prog='MToolkit')
parser.add_argument('-i', '--input-file',
dest='input_file',
nargs=1,
metavar='input file',
help="""Specify the configuration
file (i.e. config.yml)""")
parser.add_argument('-v', '--version',
action='version',
version="%(prog)s 0.0.1")
return parser


def cmd_line():
"""
Return cmdline input argument
after checking the proper input
has been given.
"""

parser = build_cmd_parser()
input_filename = None
if len(sys.argv) == 1:
parser.print_help()
else:
args = parser.parse_args()
if os.path.exists(args.input_file[0]):
input_filename = args.input_file[0]
else:
print 'Error: non existent input file\n'
parser.print_help()

return input_filename

0 comments on commit 959f03c

Please sign in to comment.