Robot Framework Integrated Development Environment (RIDE)
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 from __future__ import print_function
17 
18 import sys
19 
20 from robotide.lib.robot.errors import (INFO_PRINTED, DATA_ERROR, STOPPED_BY_USER,
21  FRAMEWORK_ERROR, Information, DataError)
22 
23 from .argumentparser import ArgumentParser
24 from .encoding import console_encode
25 from .error import get_error_details
26 
27 
28 class Application():
29 
30  def __init__(self, usage, name=None, version=None, arg_limits=None,
31  env_options=None, logger=None, **auto_options):
32  self._ap_ap = ArgumentParser(usage, name, version, arg_limits,
33  self.validatevalidate, env_options, **auto_options)
34  self._logger_logger = logger or DefaultLogger()
35 
36  def main(self, arguments, **options):
37  raise NotImplementedError
38 
39  def validate(self, options, arguments):
40  return options, arguments
41 
42  def execute_cli(self, cli_arguments, exit=True):
43  with self._logger_logger:
44  self._logger_logger.info('%s %s' % (self._ap_ap.name, self._ap_ap.version))
45  options, arguments = self._parse_arguments_parse_arguments(cli_arguments)
46  rc = self._execute_execute(arguments, options)
47  if exit:
48  self._exit_exit(rc)
49  return rc
50 
51  def console(self, msg):
52  if msg:
53  print(console_encode(msg))
54 
55  def _parse_arguments(self, cli_args):
56  try:
57  options, arguments = self.parse_argumentsparse_arguments(cli_args)
58  except Information as msg:
59  self._report_info_report_info(msg.message)
60  except DataError as err:
61  self._report_error_report_error(err.message, help=True, exit=True)
62  else:
63  self._logger_logger.info('Arguments: %s' % ','.join(arguments))
64  return options, arguments
65 
66 
73  def parse_arguments(self, cli_args):
74  return self._ap_ap.parse_args(cli_args)
75 
76  def execute(self, *arguments, **options):
77  with self._logger_logger:
78  self._logger_logger.info('%s %s' % (self._ap_ap.name, self._ap_ap.version))
79  return self._execute_execute(list(arguments), options)
80 
81  def _execute(self, arguments, options):
82  try:
83  rc = self.mainmain(arguments, **options)
84  except DataError as err:
85  return self._report_error_report_error(err.message, help=True)
86  except (KeyboardInterrupt, SystemExit):
87  return self._report_error_report_error('Execution stopped by user.',
88  rc=STOPPED_BY_USER)
89  except:
90  error, details = get_error_details(exclude_robot_traces=False)
91  return self._report_error_report_error('Unexpected error: %s' % error,
92  details, rc=FRAMEWORK_ERROR)
93  else:
94  return rc or 0
95 
96  def _report_info(self, message):
97  self.consoleconsole(message)
98  self._exit_exit(INFO_PRINTED)
99 
100  def _report_error(self, message, details=None, help=False, rc=DATA_ERROR,
101  exit=False):
102  if help:
103  message += '\n\nTry --help for usage information.'
104  if details:
105  message += '\n' + details
106  self._logger_logger.error(message)
107  if exit:
108  self._exit_exit(rc)
109  return rc
110 
111  def _exit(self, rc):
112  sys.exit(rc)
113 
114 
116 
117  def info(self, message):
118  pass
119 
120  def error(self, message):
121  print(console_encode(message))
122 
123  def close(self):
124  pass
125 
126  def __enter__(self):
127  pass
128 
129  def __exit__(self, *exc_info):
130  pass
def execute(self, *arguments, **options)
Definition: application.py:76
def execute_cli(self, cli_arguments, exit=True)
Definition: application.py:42
def parse_arguments(self, cli_args)
Public interface for parsing command line arguments.
Definition: application.py:73
def validate(self, options, arguments)
Definition: application.py:39
def _execute(self, arguments, options)
Definition: application.py:81
def __init__(self, usage, name=None, version=None, arg_limits=None, env_options=None, logger=None, **auto_options)
Definition: application.py:31
def main(self, arguments, **options)
Definition: application.py:36
def _report_error(self, message, details=None, help=False, rc=DATA_ERROR, exit=False)
Definition: application.py:101
def info(msg, html=False, also_console=False)
Writes the message to the log file using the INFO level.
Definition: logger.py:115
def error(msg, html=False)
Writes the message to the log file using the ERROR level.
Definition: logger.py:130
def console_encode(string, errors='replace', stream=sys.__stdout__)
Encodes Unicode to bytes in console or system encoding.
Definition: encoding.py:62
def get_error_details(exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Returns error message and details of the last occurred exception.
Definition: error.py:46