Coverage for sources/mimeogram/create.py: 80%
60 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-17 00:11 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-17 00:11 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
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#============================================================================#
21''' Creation of mimeograms. '''
22# TODO? Use BSD sysexits.
25from __future__ import annotations
27from . import __
28from . import interfaces as _interfaces
31_scribe = __.produce_scribe( __name__ )
34class Command(
35 _interfaces.CliCommand,
36 decorators = ( __.standard_dataclass, __.standard_tyro_class ),
37):
38 ''' Creates mimeogram from filesystem locations or URLs. '''
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 # help = "Fail on invalid contents instead of skipping them." ),
73 # ] = None
75 async def __call__( self, auxdata: __.Globals ) -> None:
76 ''' Executes command to create mimeogram. '''
77 await create( auxdata, self )
79 def provide_configuration_edits( self ) -> __.DictionaryEdits:
80 ''' Provides edits against configuration from options. '''
81 edits: list[ __.DictionaryEdit ] = [ ]
82 if None is not self.clip:
83 edits.append( __.SimpleDictionaryEdit( # pyright: ignore
84 address = ( 'create', 'to-clipboard' ), value = self.clip ) )
85 if None is not self.recurse:
86 edits.append( __.SimpleDictionaryEdit( # pyright: ignore
87 address = ( 'acquire-parts', 'recurse-directories' ),
88 value = self.recurse ) )
89 # if None is not self.strict:
90 # edits.append( __.SimpleDictionaryEdit( # pyright: ignore
91 # address = ( 'acquire-parts', 'fail-on-invalid' ),
92 # value = self.strict ) )
93 return tuple( edits )
96async def _acquire_prompt( auxdata: __.Globals ) -> str:
97 from .prompt import acquire_prompt
98 return await acquire_prompt( auxdata )
101async def _copy_to_clipboard( mimeogram: str ) -> None:
102 from pyperclip import copy
103 try: copy( mimeogram )
104 except Exception as exc:
105 _scribe.exception( "Could not copy mimeogram to clipboard." )
106 raise SystemExit( 1 ) from exc
107 _scribe.info( "Copied mimeogram to clipboard." )
110async def _edit_message( ) -> str:
111 from .edit import edit_content
112 try: return edit_content( )
113 except Exception as exc:
114 _scribe.exception( "Could not acquire user message." )
115 raise SystemExit( 1 ) from exc
118async def create( # pylint: disable=too-many-locals
119 auxdata: __.Globals,
120 command: Command, *,
121 editor: __.cabc.Callable[
122 [ ], __.cabc.Coroutine[ None, None, str ] ] = _edit_message,
123 clipcopier: __.cabc.Callable[
124 [ str ], __.cabc.Coroutine[ None, None, None ] ] = _copy_to_clipboard,
125 prompter: __.cabc.Callable[
126 [ __.Globals ],
127 __.cabc.Coroutine[ None, None, str ] ] = _acquire_prompt,
128) -> __.typx.Never:
129 ''' Creates mimeogram. '''
130 from .acquirers import acquire
131 from .formatters import format_mimeogram
132 if command.edit:
133 try: message = await editor( )
134 except Exception as exc:
135 _scribe.exception( "Could not acquire user message." )
136 raise SystemExit( 1 ) from exc
137 else: message = None
138 try: parts = await acquire( auxdata, command.sources )
139 except Exception as exc:
140 _scribe.exception( "Could not acquire mimeogram parts." )
141 raise SystemExit( 1 ) from exc
142 mimeogram = format_mimeogram( parts, message = message )
143 # TODO? Pass prompt to 'format_mimeogram'.
144 if command.prepend_prompt:
145 prompt = await prompter( auxdata )
146 mimeogram = f"{prompt}\n\n{mimeogram}"
147 options = auxdata.configuration.get( 'create', { } )
148 if options.get( 'to-clipboard', False ):
149 try: await clipcopier( mimeogram )
150 except Exception as exc:
151 _scribe.exception( "Could not copy mimeogram to clipboard." )
152 raise SystemExit( 1 ) from exc
153 else: print( mimeogram ) # TODO? Use output stream from configuration.
154 raise SystemExit( 0 )