Robot Framework
application.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import sys
17 
18 from robot.errors import (INFO_PRINTED, DATA_ERROR, STOPPED_BY_USER,
19  FRAMEWORK_ERROR, Information, DataError)
20 
21 from .argumentparser import ArgumentParser
22 from .encoding import console_encode
23 from .error import get_error_details
24 
25 
27 
28  def __init__(self, usage, name=None, version=None, arg_limits=None,
29  env_options=None, logger=None, **auto_options):
30  self._ap_ap = ArgumentParser(usage, name, version, arg_limits,
31  self.validatevalidate, env_options, **auto_options)
32  self._logger_logger = logger or DefaultLogger()
33 
34  def main(self, arguments, **options):
35  raise NotImplementedError
36 
37  def validate(self, options, arguments):
38  return options, arguments
39 
40  def execute_cli(self, cli_arguments, exit=True):
41  with self._logger_logger:
42  self._logger_logger.info('%s %s' % (self._ap_ap.name, self._ap_ap.version))
43  options, arguments = self._parse_arguments_parse_arguments(cli_arguments)
44  rc = self._execute_execute(arguments, options)
45  if exit:
46  self._exit_exit(rc)
47  return rc
48 
49  def console(self, msg):
50  if msg:
51  print(console_encode(msg))
52 
53  def _parse_arguments(self, cli_args):
54  try:
55  options, arguments = self.parse_argumentsparse_arguments(cli_args)
56  except Information as msg:
57  self._report_info_report_info(msg.message)
58  except DataError as err:
59  self._report_error_report_error(err.message, help=True, exit=True)
60  else:
61  self._logger_logger.info('Arguments: %s' % ','.join(arguments))
62  return options, arguments
63 
64 
71  def parse_arguments(self, cli_args):
72  return self._ap_ap.parse_args(cli_args)
73 
74  def execute(self, *arguments, **options):
75  with self._logger_logger:
76  self._logger_logger.info('%s %s' % (self._ap_ap.name, self._ap_ap.version))
77  return self._execute_execute(list(arguments), options)
78 
79  def _execute(self, arguments, options):
80  try:
81  rc = self.mainmain(arguments, **options)
82  except DataError as err:
83  return self._report_error_report_error(err.message, help=True)
84  except (KeyboardInterrupt, SystemExit):
85  return self._report_error_report_error('Execution stopped by user.',
86  rc=STOPPED_BY_USER)
87  except:
88  error, details = get_error_details(exclude_robot_traces=False)
89  return self._report_error_report_error('Unexpected error: %s' % error,
90  details, rc=FRAMEWORK_ERROR)
91  else:
92  return rc or 0
93 
94  def _report_info(self, message):
95  self.consoleconsole(message)
96  self._exit_exit(INFO_PRINTED)
97 
98  def _report_error(self, message, details=None, help=False, rc=DATA_ERROR,
99  exit=False):
100  if help:
101  message += '\n\nTry --help for usage information.'
102  if details:
103  message += '\n' + details
104  self._logger_logger.error(message)
105  if exit:
106  self._exit_exit(rc)
107  return rc
108 
109  def _exit(self, rc):
110  sys.exit(rc)
111 
112 
114 
115  def info(self, message):
116  pass
117 
118  def error(self, message):
119  print(console_encode(message))
120 
121  def close(self):
122  pass
123 
124  def __enter__(self):
125  pass
126 
127  def __exit__(self, *exc_info):
128  pass
def execute_cli(self, cli_arguments, exit=True)
Definition: application.py:40
def _report_error(self, message, details=None, help=False, rc=DATA_ERROR, exit=False)
Definition: application.py:99
def _parse_arguments(self, cli_args)
Definition: application.py:53
def main(self, arguments, **options)
Definition: application.py:34
def __init__(self, usage, name=None, version=None, arg_limits=None, env_options=None, logger=None, **auto_options)
Definition: application.py:29
def validate(self, options, arguments)
Definition: application.py:37
def execute(self, *arguments, **options)
Definition: application.py:74
def parse_arguments(self, cli_args)
Public interface for parsing command line arguments.
Definition: application.py:71
def _execute(self, arguments, options)
Definition: application.py:79
def info(msg, html=False, also_console=False)
Writes the message to the log file using the INFO level.
Definition: logger.py:113
def error(msg, html=False)
Writes the message to the log file using the ERROR level.
Definition: logger.py:126
def console_encode(string, encoding=None, errors='replace', stream=sys.__stdout__, force=False)
Encodes the given string so that it can be used in the console.
Definition: encoding.py:61
def get_error_details(full_traceback=True, exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Returns error message and details of the last occurred exception.
Definition: error.py:39