diff --git a/main.py b/main.py index 83ae85e..27b372b 100644 --- a/main.py +++ b/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 +# 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 diff --git a/mtoolkit/console.py b/mtoolkit/console.py new file mode 100644 index 0000000..6be9bdf --- /dev/null +++ b/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 +# 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