"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "relax.py" between
relax-4.1.3.src.tar.bz2 and relax-5.0.0.src.tar.bz2

About: relax allows the study of molecular dynamics through the analysis of experimental NMR data. Organic molecules, proteins, RNA, DNA, sugars, and other biomolecules are all supported.

relax.py  (relax-4.1.3.src.tar.bz2):relax.py  (relax-5.0.0.src.tar.bz2)
############################################################################### ###############################################################################
# # # #
# Copyright (C) 2001-2014 Edward d'Auvergne # # Copyright (C) 2001-2014,2019 Edward d'Auvergne #
# Copyright (C) 2006 Chris MacRaild # # Copyright (C) 2006 Chris MacRaild #
# Copyright (C) 2007 Gary Thompson # # Copyright (C) 2007 Gary Thompson #
# Copyright (C) 2008 Sebastien Morin # # Copyright (C) 2008 Sebastien Morin #
# Copyright (C) 2019 Troels Schwarz-Linnet # # Copyright (C) 2019 Troels Schwarz-Linnet #
# # # #
# This program is free software; you can redistribute it and/or modify # # This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by # # it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or # # the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. # # (at your option) any later version. #
# # # #
skipping to change at line 39 skipping to change at line 39
# Eliminate the ^[[?1034h escape code being produced on Linux systems by the imp ort of the readline module. # Eliminate the ^[[?1034h escape code being produced on Linux systems by the imp ort of the readline module.
import os import os
if 'TERM' in os.environ and os.environ['TERM'] == 'xterm': if 'TERM' in os.environ and os.environ['TERM'] == 'xterm':
os.environ['TERM'] = 'linux' os.environ['TERM'] = 'linux'
# Set up the Python 2 and 3 work-arounds. # Set up the Python 2 and 3 work-arounds.
import lib.compat import lib.compat
# Python modules. # Python modules.
from argparse import ArgumentParser
import numpy import numpy
from optparse import Option, OptionGroup, OptionParser
from os import F_OK, access, getpid, putenv from os import F_OK, access, getpid, putenv
if dep_check.cprofile_module: if dep_check.cprofile_module:
import cProfile as profile import cProfile as profile
elif dep_check.profile_module: elif dep_check.profile_module:
import profile import profile
import pstats import pstats
from pydoc import pager from pydoc import pager
import sys import sys
# relax modules. # relax modules.
skipping to change at line 218 skipping to change at line 218
# Execute the relax test suite # Execute the relax test suite
elif self.mode == 'test suite': elif self.mode == 'test suite':
# Only import the module in the test modes (to improve program start up speeds). # Only import the module in the test modes (to improve program start up speeds).
from test_suite.test_suite_runner import Test_suite_runner from test_suite.test_suite_runner import Test_suite_runner
# Load the interpreter and turn intros on. # Load the interpreter and turn intros on.
self.interpreter = interpreter.Interpreter(show_script=False, raise_ relax_error=True) self.interpreter = interpreter.Interpreter(show_script=False, raise_ relax_error=True)
self.interpreter.on() self.interpreter.on()
# Run the tests. # Run the tests.
runner = Test_suite_runner(self.tests, timing=self.test_timings) runner = Test_suite_runner(self.tests, timing=self.test_timings, io_ capture=self.io_capture, list_tests=self.list_tests)
self.exit_code = int(not runner.run_all_tests()) self.exit_code = int(not runner.run_all_tests())
# Execute the relax system tests. # Execute the relax system tests.
elif self.mode == 'system tests': elif self.mode == 'system tests':
# Only import the module in the test modes (to improve program start up speeds). # Only import the module in the test modes (to improve program start up speeds).
from test_suite.test_suite_runner import Test_suite_runner from test_suite.test_suite_runner import Test_suite_runner
# Load the interpreter and turn intros on. # Load the interpreter and turn intros on.
self.interpreter = interpreter.Interpreter(show_script=False, raise_ relax_error=True) self.interpreter = interpreter.Interpreter(show_script=False, raise_ relax_error=True)
self.interpreter.on() self.interpreter.on()
# Run the tests. # Run the tests.
runner = Test_suite_runner(self.tests, timing=self.test_timings) runner = Test_suite_runner(self.tests, timing=self.test_timings, io_ capture=self.io_capture, list_tests=self.list_tests)
self.exit_code = int(not runner.run_system_tests()) self.exit_code = int(not runner.run_system_tests())
# Execute the relax unit tests. # Execute the relax unit tests.
elif self.mode == 'unit tests': elif self.mode == 'unit tests':
# Only import the module in the test modes (to improve program start up speeds). # Only import the module in the test modes (to improve program start up speeds).
from test_suite.test_suite_runner import Test_suite_runner from test_suite.test_suite_runner import Test_suite_runner
# Run the tests. # Run the tests.
runner = Test_suite_runner(self.tests, timing=self.test_timings) runner = Test_suite_runner(self.tests, timing=self.test_timings, io_ capture=self.io_capture, list_tests=self.list_tests)
self.exit_code = int(not runner.run_unit_tests()) self.exit_code = int(not runner.run_unit_tests())
# Execute the relax GUI tests. # Execute the relax GUI tests.
elif self.mode == 'GUI tests': elif self.mode == 'GUI tests':
# Only import the module in the test modes (to improve program start up speeds). # Only import the module in the test modes (to improve program start up speeds).
from test_suite.test_suite_runner import Test_suite_runner from test_suite.test_suite_runner import Test_suite_runner
# Run the tests. # Run the tests.
runner = Test_suite_runner(self.tests, timing=self.test_timings) runner = Test_suite_runner(self.tests, timing=self.test_timings, io_ capture=self.io_capture, list_tests=self.list_tests)
self.exit_code = int(not runner.run_gui_tests()) self.exit_code = int(not runner.run_gui_tests())
# Execute the relax verification tests. # Execute the relax verification tests.
elif self.mode == 'verification tests': elif self.mode == 'verification tests':
# Only import the module in the test modes (to improve program start up speeds). # Only import the module in the test modes (to improve program start up speeds).
from test_suite.test_suite_runner import Test_suite_runner from test_suite.test_suite_runner import Test_suite_runner
# Run the tests. # Run the tests.
runner = Test_suite_runner(self.tests, timing=self.test_timings) runner = Test_suite_runner(self.tests, timing=self.test_timings, io_ capture=self.io_capture, list_tests=self.list_tests)
self.exit_code = int(not runner.run_verification_tests()) self.exit_code = int(not runner.run_verification_tests())
# Test mode. # Test mode.
elif self.mode == 'test': elif self.mode == 'test':
self.test_mode() self.test_mode()
# Licence mode. # Licence mode.
elif self.mode == 'licence': elif self.mode == 'licence':
self.licence() self.licence()
# Unknown mode. # Unknown mode.
else: else:
raise lib.errors.RelaxError("The '%s' mode is unknown." % self.mode) raise lib.errors.RelaxError("The '%s' mode is unknown." % self.mode)
def arguments(self): def arguments(self):
"""Process the command line arguments.""" """Process the command line arguments."""
# Parser object. # Parser object.
parser = RelaxParser(self, usage="usage: %prog [options] [script_file]") parser = RelaxParser(description="Molecular dynamics by NMR data analysi s.")
# Recognised command line options for the UI. # Recognised command line arguments for the UI.
group = OptionGroup(parser, 'UI options') group = parser.add_argument_group('UI arguments', description="The argum
group.add_option('-p', '--prompt', action='store_true', dest='prompt', d ents for selecting between the different user interfaces (UI) of relax. If none
efault=0, help='launch relax in prompt mode after running any optionally supplie of these arguments are supplied relax will default into prompt mode or, if a sc
d scripts') ript is supplied, into script mode.")
group.add_option('-g', '--gui', action='store_true', dest='gui', default group.add_argument('-p', '--prompt', action='store_true', dest='prompt',
=0, help='launch the relax GUI') default=0, help='launch relax in prompt mode after running any optionally suppl
group.add_option('-i', '--info', action='store_true', dest='info', defau ied scripts')
lt=0, help='display information about this version of relax') group.add_argument('-g', '--gui', action='store_true', dest='gui', defau
group.add_option('-v', '--version', action='store_true', dest='version', lt=0, help='launch the relax graphical user interface (GUI)')
default=0, help='show the version number and exit') group.add_argument('-i', '--info', action='store_true', dest='info', def
group.add_option('--licence', action='store_true', dest='licence', defau ault=0, help='display information about this version of relax')
lt=0, help='display the licence') group.add_argument('-v', '--version', action='store_true', dest='version
group.add_option('--test', action='store_true', dest='test', default=0, ', default=0, help='show the version number and exit')
help='run relax in test mode') group.add_argument('--licence', action='store_true', dest='licence', def
parser.add_option_group(group) ault=0, help='display the licence')
group.add_argument('--test', action='store_true', dest='test', default=0
# Recognised command line options for the multiprocessor. , help='run relax in test mode')
group = OptionGroup(parser, 'Multi-processor options')
group.add_option('-m', '--multi', action='store', type='string', dest='m # Recognised command line arguments for the multiprocessor.
ultiprocessor', default='uni', help='set multi processor method') group = parser.add_argument_group('Multi-processor arguments', descripti
group.add_option('-n', '--processors', action='store', type='int', dest= on="The arguments allowing relax to run in multi-processor environments.")
'n_processors', default=-1, help='set number of processors (may be ignored)') group.add_argument('-m', '--multi', action='store', type=str, dest='mult
parser.add_option_group(group) iprocessor', default='uni', help='set multi processor method to one of \'uni\' o
r \'mpi4py\'')
# Recognised command line options for IO redirection. group.add_argument('-n', '--processors', action='store', type=int, dest=
group = OptionGroup(parser, 'IO redirection options') 'n_processors', default=-1, help='set number of processors (may be ignored)')
group.add_option('-l', '--log', action='store', type='string', dest='log
', help='log relax output to the file LOG_FILE', metavar='LOG_FILE') # Recognised command line arguments for IO redirection.
group.add_option('-t', '--tee', action='store', type='string', dest='tee group = parser.add_argument_group('IO redirection arguments', descriptio
', help='tee relax output to both stdout and the file LOG_FILE', metavar='LOG_FI n="The arguments for sending relax output into a file.")
LE') group.add_argument('-l', '--log', action='store', type=str, dest='log',
parser.add_option_group(group) help='log relax output to the file LOG_FILE', metavar='LOG_FILE')
group.add_argument('-t', '--tee', action='store', type=str, dest='tee',
# Recognised command line options for the test suite. help='tee relax output to both stdout and the file LOG_FILE', metavar='LOG_FILE'
group = OptionGroup(parser, 'Test suite options') )
group.add_option('-x', '--test-suite', action='store_true', dest='test_s
uite', default=0, help='execute the full relax test suite') # Recognised command line arguments for the test suite.
group.add_option('-s', '--system-tests', action='store_true', dest='syst group = parser.add_argument_group('Test suite arguments', description="T
em_tests', default=0, help='execute the system/functional tests. Test names, re he arguments for activating the relax test suite. A subset of tests can be sele
vealed with the --time option, can be supplied to perform a subset of all tests. cted by providing the name of one or more test classes, test modules, or individ
') ual tests. The names of the tests are shown if the test fails, errors, when the
group.add_option('-u', '--unit-tests', action='store_true', dest='unit_t test timings are active, or when IO capture is disabled.")
ests', default=0, help='execute the unit tests. Module names, revealed with the group.add_argument('-x', '--test-suite', action='store_true', dest='test
--time option, can be supplied to perform a subset of all tests.') _suite', default=0, help='execute the full relax test suite')
group.add_option('--gui-tests', action='store_true', dest='gui_tests', d group.add_argument('-s', '--system-tests', action='store_true', dest='sy
efault=0, help='execute the GUI tests. Test names, revealed with the --time opt stem_tests', default=0, help='execute the system/functional tests')
ion, can be supplied to perform a subset of all tests.') group.add_argument('-u', '--unit-tests', action='store_true', dest='unit
group.add_option('--verification-tests', action='store_true', dest='veri _tests', default=0, help='execute the unit tests')
fication_tests', default=0, help='execute the software verification tests. Test group.add_argument('--gui-tests', action='store_true', dest='gui_tests',
names, revealed with the --time option, can be supplied to perform a subset of default=0, help='execute the GUI tests')
all tests.') group.add_argument('--verification-tests', action='store_true', dest='ve
group.add_option('--time', action='store_true', dest='tt', default=0, he rification_tests', default=0, help='execute the software verification tests')
lp='print out the timings of individual tests in the test suite') group.add_argument('--time', action='store_true', dest='tt', default=0,
group.add_option('--no-skip', action='store_true', dest='no_skip', defau help='print out the timings of individual tests in the test suite')
lt=0, help='a debugging option for relax developers to turn on all blacklisted t group.add_argument('--no-capt', '--no-capture', action='store_true', des
ests, even those that will fail') t='no_capture', default=0, help='disable IO capture in the test suite')
parser.add_option_group(group) group.add_argument('--no-skip', action='store_true', dest='no_skip', def
ault=0, help='a debugging option for relax developers to turn on all blacklisted
# Recognised command line options for debugging. tests, even those that will fail')
group = OptionGroup(parser, 'Debugging options') group.add_argument('--list-tests', action='store_true', dest='list_tests
group.add_option('-d', '--debug', action='store_true', dest='debug', def ', default=0, help='list the selected tests instead of executing them')
ault=0, help='enable debugging output')
group.add_option('--error-state', action='store_true', dest='error_state # Recognised command line arguments for debugging.
', default=0, help='save a pickled state file when a RelaxError occurs') group = parser.add_argument_group('Debugging arguments', "The arguments
group.add_option('--traceback', action='store_true', dest='traceback', d for helping to debug relax.")
efault=0, help='show stack tracebacks on all RelaxErrors and RelaxWarnings') group.add_argument('-d', '--debug', action='store_true', dest='debug', d
group.add_option('-e', '--escalate', action='store_true', dest='escalate efault=0, help='enable verbose debugging output')
', default=0, help='escalate all warnings to errors') group.add_argument('--error-state', action='store_true', dest='error_sta
group.add_option('--numpy-raise', action='store_true', dest='numpy_raise te', default=0, help='save a pickled state file when a RelaxError occurs')
', default=0, help='convert numpy warnings to errors') group.add_argument('--traceback', action='store_true', dest='traceback',
parser.add_option_group(group) default=0, help='show stack tracebacks on all RelaxErrors and RelaxWarnings')
group.add_argument('-e', '--escalate', action='store_true', dest='escala
te', default=0, help='escalate all warnings into errors')
group.add_argument('--numpy-raise', action='store_true', dest='numpy_rai
se', default=0, help='convert numpy warnings into errors')
# Parse the options. # The script file or tests to run.
(options, args) = parser.parse_args() parser.add_argument('script', nargs='*', help='the script file or one or
more test classes or individual tests to run')
# Debugging options: Debugging flag, escalate flag, traceback flag, and # Parse the arguments.
numpy warning to error conversion. args = parser.parse_args()
if options.debug:
# Debugging arguments: Debugging flag, escalate flag, traceback flag, a
nd numpy warning to error conversion.
if args.debug:
status.debug = True status.debug = True
if options.escalate: if args.escalate:
lib.warnings.ESCALATE = True lib.warnings.ESCALATE = True
if options.traceback: if args.traceback:
status.traceback = True status.traceback = True
lib.warnings.TRACEBACK = True lib.warnings.TRACEBACK = True
if options.numpy_raise: if args.numpy_raise:
numpy.seterr(all='raise') numpy.seterr(all='raise')
if options.error_state: if args.error_state:
lib.errors.SAVE_ERROR_STATE = True lib.errors.SAVE_ERROR_STATE = True
# Script prompt interactive inspection flag. # Script prompt interactive inspection flag.
if options.prompt: if args.prompt:
status.prompt = True status.prompt = True
# Logging. # Logging.
if options.log: if args.log:
# Exclusive modes. # Exclusive modes.
if options.tee: if args.tee:
parser.error("the logging and tee options cannot be set simultan parser.error("The logging and tee arguments cannot be set simult
eously") aneously.")
# The log file. # The log file.
self.log_file = options.log self.log_file = args.log
# Fail if the file already exists. # Fail if the file already exists.
if access(self.log_file, F_OK): if access(self.log_file, F_OK):
parser.error("the log file " + repr(self.log_file) + " already e xists") parser.error("The log file '%s' already exists." % self.log_file )
else: else:
self.log_file = None self.log_file = None
# Tee. # Tee.
if options.tee: if args.tee:
# Exclusive modes. # Exclusive modes.
if options.log: if args.log:
parser.error("the tee and logging options cannot be set simultan parser.error("The tee and logging options cannot be set simultan
eously") eously.")
# The tee file. # The tee file.
self.tee_file = options.tee self.tee_file = args.tee
# Fail if the file already exists. # Fail if the file already exists.
if access(self.tee_file, F_OK): if access(self.tee_file, F_OK):
parser.error("the tee file " + repr(self.tee_file) + " already e xists") parser.error("The tee file '%s' already exists." % self.tee_file )
else: else:
self.tee_file = None self.tee_file = None
# Test suite mode, therefore the args are the tests to run and not a scr ipt file. # Test suite mode, therefore the args are the tests to run and not a scr ipt file.
if options.test_suite or options.system_tests or options.unit_tests or o ptions.gui_tests or options.verification_tests: if args.test_suite or args.system_tests or args.unit_tests or args.gui_t ests or args.verification_tests:
# Store the arguments. # Store the arguments.
self.tests = args self.tests = args.script
self.io_capture = not args.no_capture
self.list_tests = args.list_tests
# Test timings. # Test timings.
self.test_timings = False self.test_timings = False
if options.tt: if args.tt:
self.test_timings = True self.test_timings = True
# Run blacklisted tests. # Run blacklisted tests.
status.skip_blacklisted_tests = True status.skip_blacklisted_tests = True
if options.no_skip: if args.no_skip:
status.skip_blacklisted_tests = False status.skip_blacklisted_tests = False
# The argument is a script. # The argument is a script (or nothing has been supplied).
else: else:
# Number of positional arguments should only be 0 or 1. 1 should be the script file. # Number of positional arguments should only be 0 or 1. 1 should be the script file.
if len(args) > 1: if len(args.script) > 1:
parser.error("incorrect number of arguments") parser.error("Incorrect number of arguments.")
# Script file. # Script file.
self.script_file = None self.script_file = None
if len(args) == 1: if len(args.script) == 1:
self.script_file = args[0] self.script_file = args.script[0]
# Test if the script file exists. # Test if the script file exists.
if not access(self.script_file, F_OK): if not access(self.script_file, F_OK):
parser.error("the script file " + repr(self.script_file) + " does not exist") parser.error("The script file '%s' does not exist." % self.s cript_file)
# Set the multi-processor type and number. # Set the multi-processor type and number.
self.multiprocessor_type = options.multiprocessor if args.multiprocessor not in ['uni', 'mpi4py']:
self.n_processors = options.n_processors parser.error("The processor type '%s' is not supported.\n" % args.mu
ltiprocessor)
self.multiprocessor_type = args.multiprocessor
self.n_processors = args.n_processors
# Checks for the multiprocessor mode. # Checks for the multiprocessor mode.
if self.multiprocessor_type == 'mpi4py' and not dep_check.mpi4py_module: if self.multiprocessor_type == 'mpi4py' and not dep_check.mpi4py_module:
parser.error(dep_check.mpi4py_message) parser.error(dep_check.mpi4py_message)
# Determine the relax mode and test for mutually exclusive modes. # Determine the relax mode and test for mutually exclusive modes.
################################################################# #################################################################
# Show the version number. # Show the version number.
if options.version: if args.version:
self.mode = 'version' self.mode = 'version'
# Show the info about this relax version. # Show the info about this relax version.
elif options.info: elif args.info:
self.mode = 'info' self.mode = 'info'
# Run the relax tests. # Run the relax tests.
elif options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: elif args.test_suite or args.system_tests or args.unit_tests or args.gui _tests or args.verification_tests:
# Exclusive modes. # Exclusive modes.
if options.test: if args.test:
parser.error("executing the relax test suite and running relax i parser.error("Executing the relax test suite and running relax i
n test mode are mutually exclusive") n test mode are mutually exclusive.")
elif options.licence: elif args.licence:
parser.error("executing the relax test suite and running relax i parser.error("Executing the relax test suite and running relax i
n licence mode are mutually exclusive") n licence mode are mutually exclusive.")
# Set the mode. # Set the mode.
if options.test_suite: if args.test_suite:
self.mode = 'test suite' self.mode = 'test suite'
elif options.system_tests: elif args.system_tests:
self.mode = 'system tests' self.mode = 'system tests'
elif options.unit_tests: elif args.unit_tests:
self.mode = 'unit tests' self.mode = 'unit tests'
elif options.gui_tests: elif args.gui_tests:
self.mode = 'GUI tests' self.mode = 'GUI tests'
elif options.verification_tests: elif args.verification_tests:
self.mode = 'verification tests' self.mode = 'verification tests'
# Set the status flag. # Set the status flag.
status.test_mode = True status.test_mode = True
# Test mode. # Test mode.
elif options.test: elif args.test:
# Make sure no script is supplied. # Make sure no script is supplied.
if self.script_file: if self.script_file:
parser.error("a script should not be supplied in test mode") parser.error("A script should not be supplied in test mode.")
# Exclusive modes. # Exclusive modes.
if options.test_suite or options.system_tests or options.unit_tests if args.test_suite or args.system_tests or args.unit_tests or args.g
or options.gui_tests or options.verification_tests: ui_tests or args.verification_tests:
parser.error("the relax test mode and executing the test suite a parser.error("The relax test mode and executing the test suite a
re mutually exclusive") re mutually exclusive.")
elif options.licence: elif args.licence:
parser.error("the relax modes test and licence are mutually excl parser.error("The relax modes test and licence are mutually excl
usive") usive.")
# Set the mode. # Set the mode.
self.mode = 'test' self.mode = 'test'
# Licence mode. # Licence mode.
elif options.licence: elif args.licence:
# Make sure no script is supplied. # Make sure no script is supplied.
if self.script_file: if self.script_file:
parser.error("a script should not be supplied in test mode") parser.error("A script should not be supplied in test mode.")
# Exclusive modes. # Exclusive modes.
if options.test_suite or options.system_tests or options.unit_tests if args.test_suite or args.system_tests or args.unit_tests or args.g
or options.gui_tests or options.verification_tests: ui_tests or args.verification_tests:
parser.error("the relax licence mode and executing the test suit parser.error("The relax licence mode and executing the test suit
e are mutually exclusive") e are mutually exclusive.")
elif options.test: elif args.test:
parser.error("the relax modes licence and test are mutually excl parser.error("The relax modes licence and test are mutually excl
usive") usive.")
# Set the mode. # Set the mode.
self.mode = 'licence' self.mode = 'licence'
# GUI. # GUI.
elif options.gui: elif args.gui:
# Exclusive models. # Exclusive models.
if options.test_suite or options.system_tests or options.unit_tests if args.test_suite or args.system_tests or args.unit_tests or args.g
or options.gui_tests or options.verification_tests: ui_tests or args.verification_tests:
parser.error("the relax GUI mode and testing modes are mutually parser.error("The relax GUI mode and testing modes are mutually
exclusive") exclusive.")
elif options.licence: elif args.licence:
parser.error("the relax GUI mode and licence mode are mutually e parser.error("The relax GUI mode and licence mode are mutually e
xclusive") xclusive.")
# Missing wx module. # Missing wx module.
if not dep_check.wx_module: if not dep_check.wx_module:
# Not installed. # Not installed.
if dep_check.wx_module_message == "No module named 'wx'": if dep_check.wx_module_message == "No module named 'wx'":
parser.error("To use the GUI, the wxPython module must be in stalled.") parser.error("To use the GUI, the wxPython module must be in stalled.")
# Broken. # Broken.
else: else:
parser.error("The wxPython installation is broken:\n%s." % d ep_check.wx_module_message) parser.error("The wxPython installation is broken:\n%s." % d ep_check.wx_module_message)
skipping to change at line 515 skipping to change at line 519
# Present the GPL using paging. # Present the GPL using paging.
file = open('docs/COPYING') file = open('docs/COPYING')
pager(file.read()) pager(file.read())
def test_mode(self): def test_mode(self):
"""Relax test mode code.""" """Relax test mode code."""
# Don't actually do anything. # Don't actually do anything.
return return
class RelaxParser(OptionParser): class RelaxParser(ArgumentParser):
def __init__(self, relax, usage=None, option_list=None, option_class=Option, """A custom ArgumentParser class."""
version=None, conflict_handler="error", description=None, formatter=None, add_h
elp_option=1, prog=None):
"""Subclassed OptionParser class with a replacement error function."""
# Relax base class.
self.relax = relax
# Run the __init__ method of the OptionParser class.
OptionParser.__init__(self, usage, option_list, option_class, version, c
onflict_handler, description, formatter, add_help_option, prog)
def error(self, message): def error(self, message):
"""Replacement error function.""" """Replace ArgumentParser.error() with a custom function, enabling the u
se of RelaxErrors.
@param message: The error message to output.
@type message: str
"""
# Usage message. # Usage message.
self.print_usage(sys.stderr) self.print_usage(sys.stderr)
# Raise a clean error. # Raise a clean error.
try: try:
raise lib.errors.RelaxError(message) raise lib.errors.RelaxError(message)
except lib.errors.AllRelaxErrors: except lib.errors.AllRelaxErrors:
instance = sys.exc_info()[1] instance = sys.exc_info()[1]
sys.stderr.write(instance.__str__()) sys.stderr.write(instance.__str__())
# Exit. # Exit with the Unix command line error code of 2.
sys.exit() sys.exit(2)
# Start relax if this file is passed to Python. # Start relax if this file is passed to Python.
if __name__ == "__main__": if __name__ == "__main__":
start() start()
 End of changes. 54 change blocks. 
169 lines changed or deleted 177 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)