pyTooling.TerminalUI
A set of helpers to implement a text user interface (TUI) in a terminal.
Classes
TerminalBaseApplication
: The class offers a basic terminal application base-class.Severity
: Logging message severity levels.Mode
: Create a collection of name/value pairs.Line
: Represents a single message line with a severity and indentation level.ILineTerminal
: A mixin class (interface) to provide class-local terminal writing methods.TerminalApplication
: The class offers a basic terminal application base-class.
Classes
- class pyTooling.TerminalUI.TerminalBaseApplication[source]
The class offers a basic terminal application base-class.
It offers basic colored output via colorama as well as retrieving the terminal’s width.
Inheritance
-
NOT_IMPLEMENTED_EXCEPTION_EXIT_CODE:
ClassVar
[int
] = 240 Return code, if unimplemented methods or code sections were called.
-
UNHANDLED_EXCEPTION_EXIT_CODE:
ClassVar
[int
] = 241 Return code, if an unhandled exception reached the topmost exception handler.
-
PYTHON_VERSION_CHECK_FAILED_EXIT_CODE:
ClassVar
[int
] = 254 Return code, if version check was not successful.
- Foreground = {'BLUE': '\x1b[94m', 'CYAN': '\x1b[96m', 'DARK_BLUE': '\x1b[34m', 'DARK_CYAN': '\x1b[36m', 'DARK_GRAY': '\x1b[90m', 'DARK_GREEN': '\x1b[32m', 'DARK_RED': '\x1b[31m', 'DARK_YELLOW': '\x1b[33m', 'ERROR': '\x1b[91m', 'GRAY': '\x1b[37m', 'GREEN': '\x1b[92m', 'HEADLINE': '\x1b[95m', 'MAGENTA': '\x1b[95m', 'NOCOLOR': '\x1b[39m', 'RED': '\x1b[91m', 'WARNING': '\x1b[93m', 'WHITE': '\x1b[97m', 'YELLOW': '\x1b[93m'}
Terminal colors
- __init__()[source]
Initialize a terminal.
If the Python package colorama [1] is available, then initialize it for colored outputs.
- Return type:
None
-
_stdin:
TextIOWrapper
STDIN
-
_stdout:
TextIOWrapper
STDOUT
-
_stderr:
TextIOWrapper
STDERR
- InitializeColors()[source]
Initialize the terminal for color support by colorama.
- Return type:
- Returns:
True, if ‘colorama’ package could be imported and initialized.
- UninitializeColors()[source]
Uninitialize the terminal for color support by colorama.
- Return type:
- Returns:
True, if ‘colorama’ package could be imported and uninitialized.
- static GetTerminalSize()[source]
Returns the terminal size as tuple (width, height) for Windows, macOS (Darwin), Linux, cygwin (Windows), MinGW32/64 (Windows).
- Return type:
- Returns:
A tuple containing width and height of the terminal’s size in characters.
- Raises:
PlatformNotSupportedException – When a platform is not yet supported.
- __GetTerminalSizeOnWindows()
Returns the current terminal window’s size for Windows.
kernel32.dll:GetConsoleScreenBufferInfo()
is used to retrieve the information.
- __GetTerminalSizeOnLinux()
Returns the current terminal window’s size for Linux.
ioctl(TIOCGWINSZ)
is used to retrieve the information. As a fallback, environment variablesCOLUMNS
andLINES
are checked.
- WriteLineToStdOut(message, end='\\n')[source]
Low-level method for writing to
STDOUT
.- Parameters:
- Return type:
- ``.
- return:
Number of written characters.
- WriteLineToStdErr(message, end='\\n')[source]
Low-level method for writing to
STDERR
.- Parameters:
- Return type:
- ``.
- return:
Number of written characters.
- FatalExit(returnCode=0)[source]
Exit the terminal application by uninitializing color support and returning a fatal Exit code.
- Exit(returnCode=0)[source]
Exit the terminal application by uninitializing color support and returning an Exit code.
- CheckPythonVersion(version)[source]
Check if the used Python interpreter fulfills the minimum version requirements.
- Return type:
- PrintException(ex)[source]
Prints an exception of type
Exception
and its traceback.If the exception as a nested action, the cause is printed as well.
If
ISSUE_TRACKER_URL
is configured, a URL to the issue tracker is added.
- PrintNotImplementedError(ex)[source]
Prints a not-implemented exception of type
NotImplementedError
.- Return type:
- Parameters:
ex (NotImplementedError)
- PrintExceptionBase(ex)[source]
Prints an exception of type
ExceptionBase
and its traceback.If the exception as a nested action, the cause is printed as well.
If
ISSUE_TRACKER_URL
is configured, a URL to the issue tracker is added.
-
NOT_IMPLEMENTED_EXCEPTION_EXIT_CODE:
- class pyTooling.TerminalUI.Severity(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Logging message severity levels.
Inheritance
- Fatal = 100
Fatal messages
- Error = 70
Error messages
- Quiet = 60
Always visible messages, even in quiet mode.
- Warning = 50
Warning messages
- Info = 20
Informative messages
- Normal = 10
Normal messages
- DryRun = 8
Messages visible in a dry-run
- Verbose = 5
Verbose messages
- Debug = 2
Debug messages
- All = 0
All messages
- class pyTooling.TerminalUI.Mode(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Inheritance
- class pyTooling.TerminalUI.Line(message, severity=Severity.Normal, indent=0, appendLinebreak=True)[source]
Represents a single message line with a severity and indentation level.
Inheritance
- _LOG_MESSAGE_FORMAT__ = {Severity.Debug: 'DEBUG: {message}', Severity.Verbose: 'VERBOSE: {message}', Severity.DryRun: 'DRYRUN: {message}', Severity.Normal: '{message}', Severity.Info: 'INFO: {message}', Severity.Warning: 'WARNING: {message}', Severity.Quiet: '{message}', Severity.Error: 'ERROR: {message}', Severity.Fatal: 'FATAL: {message}'}
Message line formatting rules.
- __init__(message, severity=Severity.Normal, indent=0, appendLinebreak=True)[source]
Constructor for a new
Line
object.
- property Severity: Severity
Return the line’s severity level.
- Returns:
Severity level of the message line.
- property AppendLinebreak: bool
Returns if a linebreak should be added at the end of the message.
- Returns:
True, if a linebreak should be added.
- class pyTooling.TerminalUI.ILineTerminal(terminal=None)[source]
A mixin class (interface) to provide class-local terminal writing methods.
Inheritance
- Parameters:
terminal (TerminalBaseApplication | None)
- __init__(terminal=None)[source]
MixIn initializer.
- Parameters:
terminal (TerminalBaseApplication | None)
- Return type:
None
- property Terminal: TerminalBaseApplication
Return the local terminal instance.
- WriteWarning(*args, condition=True, **kwargs)[source]
Write a warning message if
condition
is true.
- WriteQuiet(*args, condition=True, **kwargs)[source]
Write a message even in quiet mode if
condition
is true.
- WriteVerbose(*args, condition=True, **kwargs)[source]
Write a verbose message if
condition
is true.
- class pyTooling.TerminalUI.TerminalApplication(mode=Mode.AllLinearToStdOut)[source]
Inheritance
- _LOG_MESSAGE_FORMAT__ = {Severity.Debug: '{DARK_GRAY}{message}{NOCOLOR}', Severity.Verbose: '{GRAY}{message}{NOCOLOR}', Severity.DryRun: '{DARK_CYAN}[DRY] {message}{NOCOLOR}', Severity.Normal: '{WHITE}{message}{NOCOLOR}', Severity.Info: '{WHITE}{message}{NOCOLOR}', Severity.Warning: '{YELLOW}[WARNING] {message}{NOCOLOR}', Severity.Quiet: '{WHITE}{message}{NOCOLOR}', Severity.Error: '{RED}[ERROR] {message}{NOCOLOR}', Severity.Fatal: '{DARK_RED}[FATAL] {message}{NOCOLOR}'}
Message formatting rules.
- __init__(mode=Mode.AllLinearToStdOut)[source]
Initializer of a line based terminal interface.
- Parameters:
mode (Mode)
- Return type:
None
- CheckPythonVersion(version)
Check if the used Python interpreter fulfills the minimum version requirements.
- Return type:
- Exit(returnCode=0)
Exit the terminal application by uninitializing color support and returning an Exit code.
- FatalExit(returnCode=0)
Exit the terminal application by uninitializing color support and returning a fatal Exit code.
- Foreground = {'BLUE': '\x1b[94m', 'CYAN': '\x1b[96m', 'DARK_BLUE': '\x1b[34m', 'DARK_CYAN': '\x1b[36m', 'DARK_GRAY': '\x1b[90m', 'DARK_GREEN': '\x1b[32m', 'DARK_RED': '\x1b[31m', 'DARK_YELLOW': '\x1b[33m', 'ERROR': '\x1b[91m', 'GRAY': '\x1b[37m', 'GREEN': '\x1b[92m', 'HEADLINE': '\x1b[95m', 'MAGENTA': '\x1b[95m', 'NOCOLOR': '\x1b[39m', 'RED': '\x1b[91m', 'WARNING': '\x1b[93m', 'WHITE': '\x1b[97m', 'YELLOW': '\x1b[93m'}
Terminal colors
- static GetTerminalSize()
Returns the terminal size as tuple (width, height) for Windows, macOS (Darwin), Linux, cygwin (Windows), MinGW32/64 (Windows).
- Return type:
- Returns:
A tuple containing width and height of the terminal’s size in characters.
- Raises:
PlatformNotSupportedException – When a platform is not yet supported.
- class property HasClassAttributes: bool
Check if class has Attributes.
- Returns:
True
, if the class has Attributes.
- class property HasMethodAttributes: bool
Check if class has any method with Attributes.
- Returns:
True
, if the class has any method with Attributes.
- InitializeColors()
Initialize the terminal for color support by colorama.
- Return type:
- Returns:
True, if ‘colorama’ package could be imported and initialized.
-
NOT_IMPLEMENTED_EXCEPTION_EXIT_CODE:
ClassVar
[int
] = 240 Return code, if unimplemented methods or code sections were called.
-
PYTHON_VERSION_CHECK_FAILED_EXIT_CODE:
ClassVar
[int
] = 254 Return code, if version check was not successful.
- PrintException(ex)
Prints an exception of type
Exception
and its traceback.If the exception as a nested action, the cause is printed as well.
If
ISSUE_TRACKER_URL
is configured, a URL to the issue tracker is added.
- PrintExceptionBase(ex)
Prints an exception of type
ExceptionBase
and its traceback.If the exception as a nested action, the cause is printed as well.
If
ISSUE_TRACKER_URL
is configured, a URL to the issue tracker is added.
- PrintNotImplementedError(ex)
Prints a not-implemented exception of type
NotImplementedError
.- Return type:
- Parameters:
ex (NotImplementedError)
-
UNHANDLED_EXCEPTION_EXIT_CODE:
ClassVar
[int
] = 241 Return code, if an unhandled exception reached the topmost exception handler.
- UninitializeColors()
Uninitialize the terminal for color support by colorama.
- Return type:
- Returns:
True, if ‘colorama’ package could be imported and uninitialized.
- WriteLineToStdErr(message, end='\\n')
Low-level method for writing to
STDERR
.- Parameters:
- Return type:
- ``.
- return:
Number of written characters.
- WriteLineToStdOut(message, end='\\n')
Low-level method for writing to
STDOUT
.- Parameters:
- Return type:
- ``.
- return:
Number of written characters.
- WriteToStdErr(message)
Low-level method for writing to
STDERR
.
- WriteToStdOut(message)
Low-level method for writing to
STDOUT
.
-
_stderr:
TextIOWrapper
STDERR
-
_stdin:
TextIOWrapper
STDIN
-
_stdout:
TextIOWrapper
STDOUT
- WriteLine(line)[source]
Print a formatted line to the underlying terminal/console offered by the operating system.