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

28 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''' 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 

30class Compositor( __.immut.DataclassProtocol, __.typx.Protocol ): 

31 ''' Abstract base class for compositors. ''' 

32 

33 @__.abc.abstractmethod 

34 def __call__( 

35 self, 

36 control: _printers.TextualizationControl, 

37 record: _records.Record, 

38 ) -> str: 

39 ''' Renders record as text. ''' 

40 raise NotImplementedError 

41 

42 

43CompositorFactory: __.typx.TypeAlias = ( 

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

45 

46 

47class Introducer( __.immut.DataclassProtocol, __.typx.Protocol ): 

48 ''' Abstract base class for introducers. ''' 

49 

50 @__.abc.abstractmethod 

51 def __call__( 

52 self, 

53 control: _printers.TextualizationControl, 

54 record: _records.Record, 

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

56 ) -> str: 

57 ''' Renders record as text label. ''' 

58 raise NotImplementedError 

59 

60 

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

62 

63 

64class Linearizer( __.immut.DataclassProtocol, __.typx.Protocol ): 

65 ''' Abstract base class for linearizers. ''' 

66 

67 @__.abc.abstractmethod 

68 def __call__( 

69 self, 

70 control: _printers.TextualizationControl, 

71 entity: object, 

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

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

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

75 raise NotImplementedError 

76 

77 

78def produce_compositor_factory_default( 

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

80 line_prefix_initial: str = '', 

81 line_prefix_subsequent: str = ' ', 

82 trace_exceptions: bool = False, 

83) -> CompositorFactory: 

84 ''' Produces default compositor factory. ''' 

85 def produce_compositor( 

86 address: str, flavor: _flavors.Flavor 

87 ) -> Compositor: 

88 from .standard import ( 

89 Compositor, 

90 CompositorConfiguration, 

91 ExceptionsConfiguration, 

92 LinearizerConfiguration, 

93 ) 

94 ecfg = ExceptionsConfiguration( 

95 enable_discovery = trace_exceptions, 

96 enable_stacktraces = trace_exceptions ) 

97 lcfg = LinearizerConfiguration( exceptionscfg = ecfg ) 

98 ccfg = CompositorConfiguration( 

99 line_prefix_initial = line_prefix_initial, 

100 line_prefix_subsequent = line_prefix_subsequent, 

101 linearizercfg = lcfg ) 

102 if __.is_absent( introducer ): 

103 return Compositor( configuration = ccfg ) 

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

105 

106 return produce_compositor