Coverage for sources/ictr/textualizers.py: 91%

31 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-26 12:43 +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''' Interfaces for compositors, introducers, et cetera. ''' 

22 

23 

24from . import __ 

25from . import flavors as _flavors 

26from . import printers as _printers 

27from . import records as _records 

28 

29 

30@__.typx.runtime_checkable 

31class Compositor( 

32 __.immut.DataclassProtocol, __.typx.Protocol, 

33 class_mutables = __.PROTOCOL_RTC_MUTABLES, 

34): 

35 ''' Abstract base class for compositors. ''' 

36 

37 @__.abc.abstractmethod 

38 def __call__( 

39 self, 

40 control: _printers.TextualizationControl, 

41 record: _records.Record, 

42 ) -> str: 

43 ''' Renders record as text. ''' 

44 raise NotImplementedError 

45 

46 

47CompositorFactory: __.typx.TypeAlias = ( 

48 __.typx.Callable[ [ str, _flavors.Flavor ], Compositor ] ) 

49 

50 

51@__.typx.runtime_checkable 

52class Introducer( 

53 __.immut.DataclassProtocol, __.typx.Protocol, 

54 class_mutables = __.PROTOCOL_RTC_MUTABLES, 

55): 

56 ''' Abstract base class for introducers. ''' 

57 

58 @__.abc.abstractmethod 

59 def __call__( 

60 self, 

61 control: _printers.TextualizationControl, 

62 record: _records.Record, 

63 columns_max: __.Absential[ int ] = __.absent, 

64 ) -> str: 

65 ''' Renders record as text label. ''' 

66 raise NotImplementedError 

67 

68 

69IntroducerUnion: __.typx.TypeAlias = str | Introducer 

70 

71 

72@__.typx.runtime_checkable 

73class Linearizer( 

74 __.immut.DataclassProtocol, __.typx.Protocol, 

75 class_mutables = __.PROTOCOL_RTC_MUTABLES, 

76): 

77 ''' Abstract base class for linearizers. ''' 

78 

79 @__.abc.abstractmethod 

80 def __call__( 

81 self, 

82 control: _printers.TextualizationControl, 

83 entity: object, 

84 columns_max: __.Absential[ int ] = __.absent, 

85 ) -> tuple[ str, ... ]: 

86 ''' Renders object as lines of text. ''' 

87 raise NotImplementedError 

88 

89 

90def produce_compositor_factory_default( 

91 introducer: __.Absential[ IntroducerUnion ] = __.absent, 

92 line_prefix_initial: str = '', 

93 line_prefix_subsequent: str = ' ', 

94 trace_exceptions: bool = False, 

95) -> CompositorFactory: 

96 ''' Produces default compositor factory. ''' 

97 def produce_compositor( 

98 address: str, flavor: _flavors.Flavor 

99 ) -> Compositor: 

100 from .standard import ( 

101 Compositor, 

102 CompositorConfiguration, 

103 ExceptionsConfiguration, 

104 LinearizerConfiguration, 

105 ) 

106 ecfg = ExceptionsConfiguration( 

107 enable_discovery = trace_exceptions, 

108 enable_stacktraces = trace_exceptions ) 

109 lcfg = LinearizerConfiguration( exceptionscfg = ecfg ) 

110 ccfg = CompositorConfiguration( 

111 line_prefix_initial = line_prefix_initial, 

112 line_prefix_subsequent = line_prefix_subsequent, 

113 linearizercfg = lcfg ) 

114 if __.is_absent( introducer ): 

115 return Compositor( configuration = ccfg ) 

116 return Compositor( configuration = ccfg, introducer = introducer ) 

117 

118 return produce_compositor