3131#
3232from enum import Enum , unique
3333from platform import system as platform_system
34+ from typing import NoReturn , Tuple
3435
3536from pyTooling .Decorators import export
3637from pyTooling .MetaClasses import Singleton
@@ -95,7 +96,7 @@ class Terminal:
9596 _width : int = None #: Terminal width in characters
9697 _height : int = None #: Terminal height in characters
9798
98- def __init__ (self ):
99+ def __init__ (self ) -> None :
99100 """
100101 Initialize a terminal.
101102
@@ -108,7 +109,7 @@ def __init__(self):
108109 (self ._width , self ._height ) = self .GetTerminalSize ()
109110
110111 @classmethod
111- def initColors (cls ):
112+ def initColors (cls ) -> None :
112113 """Initialize the terminal for color support by colorama."""
113114 try :
114115 from colorama import init
@@ -118,7 +119,7 @@ def initColors(cls):
118119 pass
119120
120121 @classmethod
121- def deinitColors (cls ):
122+ def deinitColors (cls ) -> None :
122123 """Uninitialize the terminal for color support by colorama."""
123124 try :
124125 from colorama import deinit
@@ -128,18 +129,18 @@ def deinitColors(cls):
128129 pass
129130
130131 @classmethod
131- def fatalExit (cls , returnCode :int = 0 ):
132+ def fatalExit (cls , returnCode :int = 0 ) -> NoReturn :
132133 """Exit the terminal application by uninitializing color support and returning a fatal exit code."""
133134 cls .exit (cls .FATAL_EXIT_CODE if returnCode == 0 else returnCode )
134135
135136 @classmethod
136- def exit (cls , returnCode :int = 0 ):
137+ def exit (cls , returnCode :int = 0 ) -> NoReturn :
137138 """Exit the terminal application by uninitializing color support and returning an exit code."""
138139 cls .deinitColors ()
139140 exit (returnCode )
140141
141142 @classmethod
142- def versionCheck (cls , version ):
143+ def versionCheck (cls , version ) -> None :
143144 """Check if the used Python interpreter fulfills the minimum version requirements."""
144145
145146 from sys import version_info
@@ -159,7 +160,7 @@ def versionCheck(cls, version):
159160 cls .exit (1 )
160161
161162 @classmethod
162- def printException (cls , ex ):
163+ def printException (cls , ex ) -> NoReturn :
163164 """Prints an exception of type :exc:`Exception`."""
164165
165166 from traceback import print_tb , walk_tb
@@ -190,7 +191,7 @@ def printException(cls, ex):
190191 cls .exit (1 )
191192
192193 @classmethod
193- def printNotImplementedError (cls , ex ):
194+ def printNotImplementedError (cls , ex ) -> NoReturn :
194195 """Prints a not-implemented exception of type :exc:`NotImplementedError`."""
195196
196197 from traceback import walk_tb
@@ -213,7 +214,7 @@ def printNotImplementedError(cls, ex):
213214 cls .exit (1 )
214215
215216 @classmethod
216- def printExceptionBase (cls , ex ):
217+ def printExceptionBase (cls , ex ) -> NoReturn :
217218 cls .initColors ()
218219
219220 print ("{RED}FATAL: A known but unhandled exception reached the topmost exception handler!{NOCOLOR}" .format (** cls .Foreground ))
@@ -225,17 +226,17 @@ def printExceptionBase(cls, ex):
225226 cls .exit (1 )
226227
227228 @property
228- def Width (self ):
229+ def Width (self ) -> int :
229230 """Returns the current terminal window's width."""
230231 return self ._width
231232
232233 @property
233- def Height (self ):
234+ def Height (self ) -> int :
234235 """Returns the current terminal window's height."""
235236 return self ._height
236237
237238 @staticmethod
238- def GetTerminalSize ():
239+ def GetTerminalSize () -> Tuple [ int , int ] :
239240 """
240241 Returns the terminal size as tuple (width, height) for Windows, Mac OS
241242 (Darwin), Linux, cygwin (Windows), MinGW32/64 (Windows).
@@ -257,7 +258,7 @@ def GetTerminalSize():
257258 return size
258259
259260 @staticmethod
260- def __GetTerminalSizeOnWindows ():
261+ def __GetTerminalSizeOnWindows () -> Tuple [ int , int ] :
261262 """Returns the current terminal window's size for Windows."""
262263
263264 try :
@@ -278,7 +279,7 @@ def __GetTerminalSizeOnWindows():
278279 return Terminal .__GetTerminalSizeWithTPut ()
279280
280281 @staticmethod
281- def __GetTerminalSizeWithTPut ():
282+ def __GetTerminalSizeWithTPut () -> Tuple [ int , int ] :
282283 from shlex import split as shlex_split
283284 from subprocess import check_output
284285
@@ -290,7 +291,7 @@ def __GetTerminalSizeWithTPut():
290291 pass
291292
292293 @staticmethod
293- def __GetTerminalSizeOnLinux ():
294+ def __GetTerminalSizeOnLinux () -> Tuple [ int , int ] :
294295 """Returns the current terminal window's size for Linux."""
295296
296297 import os
@@ -343,12 +344,23 @@ class Severity(Enum):
343344 def __hash__ (self ):
344345 return hash (self .name )
345346
346- def __eq__ (self , other ): return self .value == other .value
347- def __ne__ (self , other ): return self .value != other .value
348- def __lt__ (self , other ): return self .value < other .value
349- def __le__ (self , other ): return self .value <= other .value
350- def __gt__ (self , other ): return self .value > other .value
351- def __ge__ (self , other ): return self .value >= other .value
347+ def __eq__ (self , other ):
348+ return self .value == other .value
349+
350+ def __ne__ (self , other ):
351+ return self .value != other .value
352+
353+ def __lt__ (self , other ):
354+ return self .value < other .value
355+
356+ def __le__ (self , other ):
357+ return self .value <= other .value
358+
359+ def __gt__ (self , other ):
360+ return self .value > other .value
361+
362+ def __ge__ (self , other ):
363+ return self .value >= other .value
352364
353365
354366@export
@@ -367,7 +379,7 @@ class Line:
367379 Severity .DryRun : "DRYRUN: {message}"
368380 } #: Terminal messages formatting rules
369381
370- def __init__ (self , message , severity = Severity .Normal , indent = 0 , appendLinebreak = True ):
382+ def __init__ (self , message , severity = Severity .Normal , indent = 0 , appendLinebreak = True ) -> None :
371383 """Constructor for a new ``Line`` object."""
372384
373385 self ._severity = severity
@@ -377,25 +389,25 @@ def __init__(self, message, severity=Severity.Normal, indent=0, appendLinebreak=
377389
378390
379391 @property
380- def Severity (self ):
392+ def Severity (self ) -> Severity :
381393 """Return the line's severity level."""
382394 return self ._severity
383395
384396 @property
385- def Indent (self ):
397+ def Indent (self ) -> int :
386398 """Return the line's indentation level."""
387399 return self ._indent
388400
389401 @property
390- def Message (self ):
402+ def Message (self ) -> str :
391403 """Return the indented line."""
392404 return (" " * self ._indent ) + self ._message
393405
394- def IndentBy (self , indent ):
406+ def IndentBy (self , indent ) -> None :
395407 """Increase a line's indentation level."""
396408 self ._indent += indent
397409
398- def __str__ (self ):
410+ def __str__ (self ) -> str :
399411 """Returns a formatted version of a ``Line`` objects as a string."""
400412 return self ._LOG_MESSAGE_FORMAT__ [self ._severity ].format (message = self ._message )
401413
@@ -406,15 +418,15 @@ class ILineTerminal:
406418
407419 _terminal = None
408420
409- def __init__ (self , terminal = None ):
421+ def __init__ (self , terminal = None ) -> None :
410422 """MixIn initializer."""
411423
412424 self ._terminal = terminal
413425
414426 # FIXME: Alter methods if a terminal is present or set dummy methods
415427
416428 @property
417- def Terminal (self ):
429+ def Terminal (self ) -> Terminal :
418430 """Return the local terminal instance."""
419431 return self ._terminal
420432
@@ -496,7 +508,7 @@ def WriteDryRun(self, *args, condition=True, **kwargs):
496508
497509@export
498510class LineTerminal (Terminal , ILineTerminal , metaclass = Singleton ):
499- def __init__ (self , verbose = False , debug = False , quiet = False , writeToStdOut = True ):
511+ def __init__ (self , verbose = False , debug = False , quiet = False , writeToStdOut = True ) -> None :
500512 """Initializer of a line based terminal interface."""
501513
502514 Terminal .__init__ (self )
@@ -523,34 +535,34 @@ def __init__(self, verbose=False, debug=False, quiet=False, writeToStdOut=True):
523535 self ._warningCounter = 0
524536
525537 @property
526- def Verbose (self ):
538+ def Verbose (self ) -> bool :
527539 """Returns true, if verbose messages are enabled."""
528540 return self ._verbose
529541
530542 @property
531- def Debug (self ):
543+ def Debug (self ) -> bool :
532544 """Returns true, if debug messages are enabled."""
533545 return self ._debug
534546
535547 @property
536- def Quiet (self ):
548+ def Quiet (self ) -> bool :
537549 """Returns true, if quiet mode is enabled."""
538550 return self ._quiet
539551
540552 @property
541- def LogLevel (self ):
553+ def LogLevel (self ) -> Severity :
542554 """Return the current minimal severity level for writing."""
543555 return self ._WriteLevel
544556 @LogLevel .setter
545- def LogLevel (self , value ) :
557+ def LogLevel (self , value : Severity ) -> None :
546558 """Set the minimal severity level for writing."""
547559 self ._WriteLevel = value
548560
549561 @property
550- def BaseIndent (self ):
562+ def BaseIndent (self ) -> int :
551563 return self ._baseIndent
552564 @BaseIndent .setter
553- def BaseIndent (self , value ) :
565+ def BaseIndent (self , value : int ) -> None :
554566 self ._baseIndent = value
555567
556568 _LOG_MESSAGE_FORMAT__ = {
@@ -565,14 +577,14 @@ def BaseIndent(self, value):
565577 Severity .Debug : "{DARK_GRAY}{message}{NOCOLOR}"
566578 } #: Message formatting rules.
567579
568- def ExitOnPreviousErrors (self ):
580+ def ExitOnPreviousErrors (self ) -> None :
569581 """Exit application if errors have been printed."""
570582
571583 if self ._errorCounter > 0 :
572584 self .WriteFatal ("Too many errors in previous steps." )
573585 self .fatalExit ()
574586
575- def ExitOnPreviousWarnings (self ):
587+ def ExitOnPreviousWarnings (self ) -> None :
576588 """Exit application if warnings have been printed."""
577589
578590 if self ._warningCounter > 0 :
@@ -590,7 +602,7 @@ def WriteLine(self, line : Line):
590602 else :
591603 return False
592604
593- def TryWriteLine (self , line ):
605+ def TryWriteLine (self , line ) -> bool :
594606 return (line .Severity >= self ._WriteLevel )
595607
596608 def WriteFatal (self , message , indent = 0 , appendLinebreak = True , immediateExit = True ):
0 commit comments