Revision: 80774a7e474e
Author:   Pekka Klärck
Date:     Mon Jan 30 09:26:41 2012
Log: took new command line app to use. interesting to see what breaks... some cleanup still needed anyway. i'll take care of that later today.
http://code.google.com/p/robotframework/source/detail?r=80774a7e474e

Modified:
 /src/robot/__init__.py
 /src/robot/cliapp.py

=======================================
--- /src/robot/__init__.py      Mon Jan 30 07:32:52 2012
+++ /src/robot/__init__.py      Mon Jan 30 09:26:41 2012
@@ -27,54 +27,23 @@
 from robot.variables import init_global_variables
 from robot.version import get_version, get_full_version
 from robot import utils
+from robot.cliapp import CommandLineApplication


 __version__ = get_version()


-def run_from_cli(args, usage):
-    LOGGER.info(get_full_version('Robot Framework'))
-    return _run_or_rebot_from_cli(run, args, usage)
-
-def rebot_from_cli(args, usage):
-    LOGGER.info(get_full_version('Rebot'))
-    return _run_or_rebot_from_cli(rebot, args, usage)
-
-def _run_or_rebot_from_cli(method, cliargs, usage):
-    LOGGER.register_file_logger()
-    try:
-        options, datasources = _parse_arguments(cliargs, usage)
-    except Information, msg:
-        print utils.encode_output(unicode(msg))
-        return INFO_PRINTED
-    except DataError, err:
-        _report_error(unicode(err), help=True)
-        return DATA_ERROR
-    LOGGER.info('Data sources: %s' % utils.seq2str(datasources))
-    return method(*datasources, **options)
-
-def _parse_arguments(cliargs, usage):
-    ap = utils.ArgumentParser(usage, get_full_version())
-    return ap.parse_args(cliargs, check_args=True)
-
-def _execute(method, datasources, options):
-    try:
-        rc = method(datasources, options)
-    except DataError, err:
-        _report_error(unicode(err), help=True)
-        return DATA_ERROR
-    except (KeyboardInterrupt, SystemExit):
-        _report_error('Execution stopped by user.')
-        return STOPPED_BY_USER
-    except:
-        error, details = utils.get_error_details()
-        _report_error('Unexpected error: %s' % error, details)
-        return FRAMEWORK_ERROR
-    else:
-        return rc
-    finally:
-        LOGGER.close()
-
+def run_from_cli(cli_args, usage):
+    app = CommandLineApplication(usage)
+    opts, args = app.parse_arguments(cli_args)
+    rc = app.execute(_run, opts, args)
+    app.exit(rc)
+
+def rebot_from_cli(cli_args, usage):
+    app = CommandLineApplication(usage)
+    opts, args = app.parse_arguments(cli_args)
+    rc = app.execute(_rebot, opts, args)
+    app.exit(rc)

 def run(*datasources, **options):
     """Executes given Robot data sources with given options.
@@ -97,9 +66,11 @@
     pybot path/to/tests.html
     pybot --report r.html --log NONE t1.txt t2.txt > stdout.txt
     """
-    return _execute(_run, datasources, options)
-
-def _run(datasources, options):
+    app = CommandLineApplication('xxx', exit=False)
+    rc = app.execute(_run, options, datasources)
+    return app.exit(rc)
+
+def _run(*datasources, **options):
     STOP_SIGNAL_MONITOR.start()
     settings = RobotSettings(options)
     pyloggingconf.initialize(settings['LogLevel'])
@@ -141,9 +112,11 @@
     rebot path/to/output.xml
     rebot --report r.html --log NONE o1.xml o2.xml > stdout.txt
     """
-    return _execute(_rebot, datasources, options)
-
-def _rebot(datasources, options):
+    app = CommandLineApplication('xxx', exit=False)
+    rc = app.execute(_rebot, options, datasources)
+    return app.exit(rc)
+
+def _rebot(*datasources, **options):
     settings = RebotSettings(options)
     LOGGER.register_console_logger(colors=settings['MonitorColors'],
                                    stdout=settings['StdOut'],
=======================================
--- /src/robot/cliapp.py        Mon Jan 30 08:54:41 2012
+++ /src/robot/cliapp.py        Mon Jan 30 09:26:41 2012
@@ -22,8 +22,9 @@

 class CommandLineApplication(object):

-    def __init__(self, usage, name=None, version=None, arg_limits=None):
+ def __init__(self, usage, name=None, version=None, arg_limits=None, exit=True):
         self._ap = utils.ArgumentParser(usage, name, version, arg_limits)
+        self._exit = exit
         LOGGER.register_file_logger()
         LOGGER.info('%s %s' % (self.name, self.version))

@@ -39,9 +40,9 @@
         try:
             options, arguments = self._ap.parse_args(cli_args, check_args)
         except Information, msg:
-            self._report_info_and_exit(unicode(msg))
+            return self._report_info(unicode(msg))
         except DataError, err:
-            self._report_error_and_exit(unicode(err), help=True)
+            return self._report_error(unicode(err), help=True)
         else:
             LOGGER.info('Arguments: %s' % utils.seq2str(arguments))
             return options, arguments
@@ -50,30 +51,31 @@
         try:
             rc = method(*arguments, **options)
         except DataError, err:
-            self._report_error_and_exit(unicode(err), help=True)
+            return self._report_error(unicode(err), help=True)
         except (KeyboardInterrupt, SystemExit):
-            self._report_error_and_exit('Execution stopped by user.',
-                                        rc=STOPPED_BY_USER)
+            return self._report_error('Execution stopped by user.',
+                                      rc=STOPPED_BY_USER)
         except:
             error, details = utils.get_error_details()
-            self._report_error_and_exit('Unexpected error: %s' % error,
-                                        details, rc=FRAMEWORK_ERROR)
+            return self._report_error('Unexpected error: %s' % error,
+                                      details, rc=FRAMEWORK_ERROR)
         else:
             return rc

-    def _report_info_and_exit(self, msg):
+    def _report_info(self, msg):
         print utils.encode_output(unicode(msg))
-        self.exit(INFO_PRINTED)
-
-    def _report_error_and_exit(self, message, details=None, help=False,
-                               rc=DATA_ERROR):
+        return self.exit(INFO_PRINTED)
+
+ def _report_error(self, message, details=None, help=False, rc=DATA_ERROR):
         if help:
             message += '\n\nTry --help for usage information.'
         if details:
             message += '\n' + details
         LOGGER.error(message)
-        self.exit(rc)
+        return self.exit(rc)

     def exit(self, rc):
         LOGGER.close()
-        sys.exit(rc)
+        if self._exit:
+            sys.exit(rc)
+        return rc

Reply via email to