1- # =============================================================================
2- # _____ _ _ _____ _ _ _ _ ___
3- # _ __ _ |_ _|__ ___ | (_)_ __ __ _|_ _|__ _ __ _ __ ___ (_)_ __ __ _| | | | |_ _|
4- # | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | | | | || |
5- # | |_) | |_| || | (_) | (_) | | | | | | (_| |_| | __/ | | | | | | | | | | | (_| | | |_| || |
6- # | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\___/|___|
7- # |_| |___/ |___/
8- # =============================================================================
9- # Authors: Patrick Lehmann
1+ # ==================================================================================================================== #
2+ # _____ _ _ _____ _ _ _ _ ___ #
3+ # _ __ _ |_ _|__ ___ | (_)_ __ __ _|_ _|__ _ __ _ __ ___ (_)_ __ __ _| | | | |_ _| #
4+ # | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | | | | || | #
5+ # | |_) | |_| || | (_) | (_) | | | | | | (_| |_| | __/ | | | | | | | | | | | (_| | | |_| || | #
6+ # | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\___/|___| #
7+ # |_| |___/ |___/ #
8+ # ==================================================================================================================== #
9+ # Authors: #
10+ # Patrick Lehmann #
11+ # #
12+ # License: #
13+ # ==================================================================================================================== #
14+ # Copyright 2017-2021 Patrick Lehmann - Bötzingen, Germany #
15+ # #
16+ # Licensed under the Apache License, Version 2.0 (the "License"); #
17+ # you may not use this file except in compliance with the License. #
18+ # You may obtain a copy of the License at #
19+ # #
20+ # http://www.apache.org/licenses/LICENSE-2.0 #
21+ # #
22+ # Unless required by applicable law or agreed to in writing, software #
23+ # distributed under the License is distributed on an "AS IS" BASIS, #
24+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
25+ # See the License for the specific language governing permissions and #
26+ # limitations under the License. #
27+ # #
28+ # SPDX-License-Identifier: Apache-2.0 #
29+ # ==================================================================================================================== #
1030#
11- # Python package: A set of helpers to implement a text user interface (TUI) in a terminal.
12- #
13- # License:
14- # ============================================================================
15- # Copyright 2017-2021 Patrick Lehmann - Bötzingen, Germany
16- #
17- # Licensed under the Apache License, Version 2.0 (the "License");
18- # you may not use this file except in compliance with the License.
19- # You may obtain a copy of the License at
20- #
21- # http://www.apache.org/licenses/LICENSE-2.0
22- #
23- # Unless required by applicable law or agreed to in writing, software
24- # distributed under the License is distributed on an "AS IS" BASIS,
25- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26- # See the License for the specific language governing permissions and
27- # limitations under the License.
28- #
29- # SPDX-License-Identifier: Apache-2.0
30- # ============================================================================
31- #
32- from enum import Enum , unique
33- from platform import system as platform_system
34- from typing import NoReturn , Tuple
35-
36- from pyTooling .Decorators import export
37- from pyTooling .MetaClasses import Singleton
38-
31+ """A set of helpers to implement a text user interface (TUI) in a terminal."""
3932__author__ = "Patrick Lehmann"
40334134__copyright__ = "2007-2021, Patrick Lehmann"
4235__license__ = "Apache License, Version 2.0"
4336__version__ = "1.5.6"
4437__keywords__ = ["terminal" , "shell" , "text user interface" , "TUI" , "console" , "message logging" ]
4538
39+ from enum import Enum , unique
40+ from platform import system as platform_system
41+ from typing import NoReturn , Tuple , Any
42+
43+ from pyTooling .Decorators import export
44+ from pyTooling .MetaClasses import Singleton
45+
4646
4747@export
4848class Terminal :
@@ -163,7 +163,6 @@ def versionCheck(cls, version) -> None:
163163 @classmethod
164164 def printException (cls , ex ) -> NoReturn :
165165 """Prints an exception of type :exc:`Exception`."""
166-
167166 from traceback import print_tb , walk_tb
168167
169168 cls .initColors ()
@@ -194,7 +193,6 @@ def printException(cls, ex) -> NoReturn:
194193 @classmethod
195194 def printNotImplementedError (cls , ex ) -> NoReturn :
196195 """Prints a not-implemented exception of type :exc:`NotImplementedError`."""
197-
198196 from traceback import walk_tb
199197
200198 cls .initColors ()
@@ -238,11 +236,7 @@ def Height(self) -> int:
238236
239237 @staticmethod
240238 def GetTerminalSize () -> Tuple [int , int ]:
241- """
242- Returns the terminal size as tuple (width, height) for Windows, Mac OS
243- (Darwin), Linux, cygwin (Windows), MinGW32/64 (Windows).
244- """
245-
239+ """Returns the terminal size as tuple (width, height) for Windows, Mac OS (Darwin), Linux, cygwin (Windows), MinGW32/64 (Windows)."""
246240 size = None
247241
248242 platform = platform_system ()
@@ -261,7 +255,6 @@ def GetTerminalSize() -> Tuple[int, int]:
261255 @staticmethod
262256 def __GetTerminalSizeOnWindows () -> Tuple [int , int ]:
263257 """Returns the current terminal window's size for Windows."""
264-
265258 try :
266259 from ctypes import windll , create_string_buffer
267260 from struct import unpack as struct_unpack
@@ -294,7 +287,6 @@ def __GetTerminalSizeWithTPut() -> Tuple[int, int]:
294287 @staticmethod
295288 def __GetTerminalSizeOnLinux () -> Tuple [int , int ]:
296289 """Returns the current terminal window's size for Linux."""
297-
298290 import os
299291
300292 def ioctl_GWINSZ (fd ):
@@ -345,23 +337,41 @@ class Severity(Enum):
345337 def __hash__ (self ):
346338 return hash (self .name )
347339
348- def __eq__ (self , other ):
349- return self .value == other .value
340+ def __eq__ (self , other : Any ):
341+ if isinstance (other , Severity ):
342+ return self .value == other .value
343+ else :
344+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by == operator." )
350345
351- def __ne__ (self , other ):
352- return self .value != other .value
346+ def __ne__ (self , other : Any ):
347+ if isinstance (other , Severity ):
348+ return self .value != other .value
349+ else :
350+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by != operator." )
353351
354- def __lt__ (self , other ):
355- return self .value < other .value
352+ def __lt__ (self , other : Any ):
353+ if isinstance (other , Severity ):
354+ return self .value < other .value
355+ else :
356+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by < operator." )
356357
357- def __le__ (self , other ):
358- return self .value <= other .value
358+ def __le__ (self , other : Any ):
359+ if isinstance (other , Severity ):
360+ return self .value <= other .value
361+ else :
362+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by <= operator." )
359363
360- def __gt__ (self , other ):
361- return self .value > other .value
364+ def __gt__ (self , other : Any ):
365+ if isinstance (other , Severity ):
366+ return self .value > other .value
367+ else :
368+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by > operator." )
362369
363- def __ge__ (self , other ):
364- return self .value >= other .value
370+ def __ge__ (self , other : Any ):
371+ if isinstance (other , Severity ):
372+ return self .value >= other .value
373+ else :
374+ raise TypeError (f"Second operand of type '{ other .__class__ .__name__ } ' is not supported by >= operator." )
365375
366376
367377@export
@@ -382,7 +392,6 @@ class Line:
382392
383393 def __init__ (self , message , severity = Severity .Normal , indent = 0 , appendLinebreak = True ) -> None :
384394 """Constructor for a new ``Line`` object."""
385-
386395 self ._severity = severity
387396 self ._message = message
388397 self ._indent = indent
@@ -421,7 +430,6 @@ class ILineTerminal:
421430
422431 def __init__ (self , terminal = None ) -> None :
423432 """MixIn initializer."""
424-
425433 self ._terminal = terminal
426434
427435 # FIXME: Alter methods if a terminal is present or set dummy methods
@@ -433,7 +441,6 @@ def Terminal(self) -> Terminal:
433441
434442 def WriteLine (self , line : Line , condition = True ):
435443 """Write an entry to the local terminal."""
436-
437444 if ((self ._terminal is not None ) and condition ):
438445 return self ._terminal .WriteLine (line )
439446 return False
@@ -445,63 +452,54 @@ def WriteLine(self, line : Line, condition=True):
445452
446453 def WriteFatal (self , * args , condition = True , ** kwargs ):
447454 """Write a fatal message if ``condition`` is true."""
448-
449455 if ((self ._terminal is not None ) and condition ):
450456 return self ._terminal .WriteFatal (* args , ** kwargs )
451457 return False
452458
453459 def WriteError (self , * args , condition = True , ** kwargs ):
454460 """Write an error message if ``condition`` is true."""
455-
456461 if ((self ._terminal is not None ) and condition ):
457462 return self ._terminal .WriteError (* args , ** kwargs )
458463 return False
459464
460465 def WriteWarning (self , * args , condition = True , ** kwargs ):
461466 """Write a warning message if ``condition`` is true."""
462-
463467 if ((self ._terminal is not None ) and condition ):
464468 return self ._terminal .WriteWarning (* args , ** kwargs )
465469 return False
466470
467471 def WriteInfo (self , * args , condition = True , ** kwargs ):
468472 """Write a info message if ``condition`` is true."""
469-
470473 if ((self ._terminal is not None ) and condition ):
471474 return self ._terminal .WriteInfo (* args , ** kwargs )
472475 return False
473476
474477 def WriteQuiet (self , * args , condition = True , ** kwargs ):
475478 """Write a message even in quiet mode if ``condition`` is true."""
476-
477479 if ((self ._terminal is not None ) and condition ):
478480 return self ._terminal .WriteQuiet (* args , ** kwargs )
479481 return False
480482
481483 def WriteNormal (self , * args , condition = True , ** kwargs ):
482484 """Write a *normal* message if ``condition`` is true."""
483-
484485 if ((self ._terminal is not None ) and condition ):
485486 return self ._terminal .WriteNormal (* args , ** kwargs )
486487 return False
487488
488489 def WriteVerbose (self , * args , condition = True , ** kwargs ):
489490 """Write a verbose message if ``condition`` is true."""
490-
491491 if ((self ._terminal is not None ) and condition ):
492492 return self ._terminal .WriteVerbose (* args , ** kwargs )
493493 return False
494494
495495 def WriteDebug (self , * args , condition = True , ** kwargs ):
496496 """Write a debug message if ``condition`` is true."""
497-
498497 if ((self ._terminal is not None ) and condition ):
499498 return self ._terminal .WriteDebug (* args , ** kwargs )
500499 return False
501500
502501 def WriteDryRun (self , * args , condition = True , ** kwargs ):
503502 """Write a dry-run message if ``condition`` is true."""
504-
505503 if ((self ._terminal is not None ) and condition ):
506504 return self ._terminal .WriteDryRun (* args , ** kwargs )
507505 return False
@@ -511,7 +509,6 @@ def WriteDryRun(self, *args, condition=True, **kwargs):
511509class LineTerminal (Terminal , ILineTerminal , metaclass = Singleton ):
512510 def __init__ (self , verbose = False , debug = False , quiet = False , writeToStdOut = True ) -> None :
513511 """Initializer of a line based terminal interface."""
514-
515512 Terminal .__init__ (self )
516513 ILineTerminal .__init__ (self , self )
517514
@@ -580,21 +577,18 @@ def BaseIndent(self, value: int) -> None:
580577
581578 def ExitOnPreviousErrors (self ) -> None :
582579 """Exit application if errors have been printed."""
583-
584580 if self ._errorCounter > 0 :
585581 self .WriteFatal ("Too many errors in previous steps." )
586582 self .fatalExit ()
587583
588584 def ExitOnPreviousWarnings (self ) -> None :
589585 """Exit application if warnings have been printed."""
590-
591586 if self ._warningCounter > 0 :
592587 self .WriteError ("Too many warnings in previous steps." )
593588 self .exit ()
594589
595590 def WriteLine (self , line : Line ):
596591 """Print a formatted line to the underlying terminal/console offered by the operating system."""
597-
598592 if (line .Severity >= self ._WriteLevel ):
599593 self ._lines .append (line )
600594 if self ._writeToStdOut :
0 commit comments