Tool Base Class

A Base class for tools, which provides the essential functionality required from all tools. Deriving from this class ensures seemless integration with the sk.py executable.

class pysk.tools.ToolBase(parent=None, description='A command line tool', **kwargs)

Base class for command line tools

Parameters:
  • parent – Parent, e.g. if tool is used as sub-tool of another tool. Defaults to None.
  • description – Description forwarded to ArgumentParser.
  • kwargs – Keyword arguments forwarded to constructor of ArgumentParser
__call__(args=None)

Process command line options and invoke self._exec

Parameters:args – Command line arguments forwarded to argparse. Defaults to None. (i.e. sys.argv)
static defaultConfiguration()

Get Default Configuration Options

Returns:Default configuration object with the following arguments
  • force= False
  • logStream= sys.stderr
  • parseCommandLine= True
  • verbose= 1
displayHelp()

Print help to self.config.logStream

error(message)

Print error message to output stream

Parameters:message (str) – Message to print to stream
helpMessage()

Get help message as string

Returns:Help message as string
importConfiguration(config)

Copy configuration options from another instance

Adds each attribute found in config to the current configuration. Existing attributes will be overwritten.

Parameters:config – Configuration to copy
log(message, verbose=0)

Print log message to output stream

Parameters:
  • message (string) – Message to print to stream
  • verbose (int) – Verbose mode. Message is only printed, if self.verbose is greater or equal this value. Defaults to 0 (print always)
main()

Main wrapper

Executes the tool as main routine. All exceptions are are caught and converted to error messages.

The exit code is set to 0, if the program terminated successfully or to 1 in case of errors.

A stack trace is printed in addition to the error message, if config.debug is set to True.

Returns:exit code
mayOverwrite(path=None, message=None, retries=None)

Check if a given file may be overwritten.

Parameters:
  • path (str) – Path to file. Ignored if message is specified.
  • message (str) – Message displayed. Defaults to ‘<path> exists. Overwrite (yes/no) ?’
  • retries (int) – Number of retries allowed. An IOError is raised if the number is exceeded. A value of None, allows for infinite retries. Default is None.
Returns:

True if and only if either self.config.force is True or the user approves.

parseCommandLineOptions(args=None)

Parse command line options

Parameters:args – Arguments to parse. If None, defaults to command line arguments. Defaults to None.
warn(message)

Print warning message to output stream

Parameters:message (str) – Message to print to stream