Coverage for sources/mimeogram/display.py: 29%

38 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-05 19:46 +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''' System pager interaction. ''' 

22 

23 

24from . import __ 

25 

26 

27_scribe = __.produce_scribe( __name__ ) 

28 

29 

30def discover_pager( ) -> __.cabc.Callable[ [ str ], None ]: 

31 ''' Discovers pager and returns executor function. ''' 

32 from shutil import which 

33 from subprocess import run 

34 pager = __.os.environ.get( 'PAGER', 'less' ) 

35 for pager_ in ( pager, 'less', 'more' ): 

36 if ( pager := which( pager_ ) ): 

37 match __.sys.platform: 

38 case 'win32': 

39 # Windows 'more.com' does not support UTF-8. 

40 if pager.lower( ).endswith( '\\more.com' ): continue 

41 case _: pass 

42 break 

43 else: pager = '' 

44 

45 if pager: 

46 

47 # TODO? async 

48 def pager_executor( filename: str ) -> None: 

49 ''' Executes pager with file. ''' 

50 run( ( pager, filename ), check = True ) # noqa: S603 

51 

52 return pager_executor 

53 

54 # TODO? async 

55 def console_display( filename: str ) -> None: 

56 ''' Prints file to stdout and waits for ENTER key. ''' 

57 with open( filename, 'r', encoding = 'utf-8' ) as stream: 

58 content = stream.read( ) 

59 print( f"\n\n{content}\n\n" ) 

60 if __.sys.stdin.isatty( ): input( "Press Enter to continue..." ) 

61 

62 _scribe.warning( "Could not find pager program for display." ) 

63 return console_display 

64 

65 

66def display_content( 

67 content: str, *, 

68 suffix: str = '.txt', 

69 pager_discoverer: __.cabc.Callable[ 

70 [ ], __.cabc.Callable[ [ str ], None ] ] = discover_pager, 

71) -> None: 

72 ''' Displays content via discovered pager. ''' 

73 from .exceptions import PagerFailure 

74 pager = pager_discoverer( ) 

75 import tempfile 

76 from pathlib import Path 

77 # Using delete = False to handle file cleanup manually. This ensures 

78 # the file handle is properly closed before the pager attempts to read it, 

79 # which is particularly important on Windows where open files cannot be 

80 # simultaneously accessed by other processes without a read share. 

81 with tempfile.NamedTemporaryFile( 

82 mode = 'w', suffix = suffix, delete = False, encoding = 'utf-8' 

83 ) as tmp: 

84 filename = tmp.name 

85 tmp.write( content ) 

86 try: pager( filename ) 

87 except Exception as exc: raise PagerFailure( cause = exc ) from exc 

88 finally: 

89 try: Path( filename ).unlink( ) 

90 except Exception: 

91 _scribe.exception( f"Failed to cleanup {filename}" )