Coverage for sources / ictr / standard / printers.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-12 01:33 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Printers and printer factories. ''' 

22 

23 

24from . import __ 

25 

26 

27_validate_arguments = ( 

28 __.validate_arguments( 

29 globalvars = globals( ), 

30 errorclass = __.ArgumentClassInvalidity ) ) 

31 

32 

33class Printer( __.Printer ): 

34 ''' Simple printer that writes to a text stream. ''' 

35 

36 target: __.io.TextIOBase 

37 force_color: bool = False 

38 

39 def __call__( self, record: str | __.Record ) -> None: 

40 text = record if isinstance( record, str ) else str( record ) 

41 if not self._determine_colorization( ): 

42 text = __.remove_ansi_c1_sequences( text ) 

43 print( text, file = self.target ) 

44 

45 def provide_textualization_control( 

46 self 

47 ) -> __.typx.Optional[ __.TextualizationControl ]: 

48 colorize = self._determine_colorization( ) 

49 columns_max_calculator = ( 

50 __.produce_columns_max_calculator( self.target ) ) 

51 charset = getattr( self.target, 'encoding', None ) 

52 if charset: 

53 charset = __.codecs.lookup( charset ).name # normalize 

54 return __.TextualizationControl( 

55 charset = charset, 

56 colorize = colorize, 

57 columns_max_calculator = columns_max_calculator ) 

58 

59 def _determine_colorization( self ) -> bool: 

60 colorize = self.target.isatty( ) 

61 if __.os.environ.get( 'NO_COLOR' ): colorize = False 

62 return colorize or self.force_color