Coverage for sources/mimeogram/create.py: 83%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-22 03:00 +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''' Creation of mimeograms. ''' 

22# TODO? Use BSD sysexits. 

23 

24 

25from __future__ import annotations 

26 

27from . import __ 

28from . import interfaces as _interfaces 

29 

30 

31_scribe = __.produce_scribe( __name__ ) 

32 

33 

34class Command( 

35 _interfaces.CliCommand, 

36 decorators = ( __.standard_dataclass, __.standard_tyro_class ), 

37): 

38 ''' Creates mimeogram from filesystem locations or URLs. ''' 

39 

40 sources: __.typx.Annotated[ 

41 __.tyro.conf.Positional[ list[ str ] ], 

42 __.tyro.conf.arg( 

43 help = "Filesystem locations or URLs.", 

44 prefix_name = False ), 

45 ] 

46 clip: __.typx.Annotated[ 

47 __.typx.Optional[ bool ], 

48 __.tyro.conf.arg( 

49 aliases = ( '--clipboard', '--to-clipboard' ), 

50 help = "Copy mimeogram to clipboard." ), 

51 ] = None 

52 edit: __.typx.Annotated[ 

53 bool, 

54 __.tyro.conf.arg( 

55 aliases = ( '-e', '--edit-message' ), 

56 help = "Spawn editor to capture an introductory message." ), 

57 ] = False 

58 prepend_prompt: __.typx.Annotated[ 

59 bool, 

60 __.tyro.conf.arg( 

61 help = "Prepend mimeogram format instructions." ), 

62 ] = False 

63 recurse: __.typx.Annotated[ 

64 __.typx.Optional[ bool ], 

65 __.tyro.conf.arg( 

66 aliases = ( '-r', '--recurse-directories', '--recursive' ), 

67 help = "Recurse into directories." ), 

68 ] = None 

69 strict: __.typx.Annotated[ 

70 __.typx.Optional[ bool ], 

71 __.tyro.conf.arg( 

72 aliases = ( '--fail-on-invalid', ), 

73 help = "Fail on invalid contents? True, fail. False, skip." ), 

74 ] = None 

75 

76 async def __call__( self, auxdata: __.Globals ) -> None: 

77 ''' Executes command to create mimeogram. ''' 

78 await create( auxdata, self ) 

79 

80 def provide_configuration_edits( self ) -> __.DictionaryEdits: 

81 ''' Provides edits against configuration from options. ''' 

82 edits: list[ __.DictionaryEdit ] = [ ] 

83 if None is not self.clip: 

84 edits.append( __.SimpleDictionaryEdit( # pyright: ignore 

85 address = ( 'create', 'to-clipboard' ), value = self.clip ) ) 

86 if None is not self.recurse: 

87 edits.append( __.SimpleDictionaryEdit( # pyright: ignore 

88 address = ( 'acquire-parts', 'recurse-directories' ), 

89 value = self.recurse ) ) 

90 if None is not self.strict: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 edits.append( __.SimpleDictionaryEdit( # pyright: ignore 

92 address = ( 'acquire-parts', 'fail-on-invalid' ), 

93 value = self.strict ) ) 

94 return tuple( edits ) 

95 

96 

97async def _acquire_prompt( auxdata: __.Globals ) -> str: 

98 from .prompt import acquire_prompt 

99 return await acquire_prompt( auxdata ) 

100 

101 

102async def _copy_to_clipboard( mimeogram: str ) -> None: 

103 from pyperclip import copy 

104 copy( mimeogram ) 

105 _scribe.info( "Copied mimeogram to clipboard." ) 

106 

107 

108async def _edit_message( ) -> str: 

109 from .edit import edit_content 

110 return edit_content( ) 

111 

112 

113async def create( # pylint: disable=too-complex,too-many-locals 

114 auxdata: __.Globals, 

115 command: Command, *, 

116 editor: __.cabc.Callable[ 

117 [ ], __.cabc.Coroutine[ None, None, str ] ] = _edit_message, 

118 clipcopier: __.cabc.Callable[ 

119 [ str ], __.cabc.Coroutine[ None, None, None ] ] = _copy_to_clipboard, 

120 prompter: __.cabc.Callable[ 

121 [ __.Globals ], 

122 __.cabc.Coroutine[ None, None, str ] ] = _acquire_prompt, 

123) -> __.typx.Never: 

124 ''' Creates mimeogram. ''' 

125 from .acquirers import acquire 

126 from .formatters import format_mimeogram 

127 with __.report_exceptions( 

128 _scribe, "Could not acquire mimeogram parts." 

129 ): parts = await acquire( auxdata, command.sources ) 

130 if command.edit: 

131 with __.report_exceptions( 

132 _scribe, "Could not acquire user message." 

133 ): message = await editor( ) 

134 else: message = None 

135 mimeogram = format_mimeogram( parts, message = message ) 

136 # TODO? Pass prompt to 'format_mimeogram'. 

137 if command.prepend_prompt: 

138 prompt = await prompter( auxdata ) 

139 mimeogram = f"{prompt}\n\n{mimeogram}" 

140 options = auxdata.configuration.get( 'create', { } ) 

141 if options.get( 'to-clipboard', False ): 

142 with __.report_exceptions( 

143 _scribe, "Could not copy mimeogram to clipboard." 

144 ): await clipcopier( mimeogram ) 

145 else: print( mimeogram ) # TODO? Use output stream from configuration. 

146 raise SystemExit( 0 )