Coverage for sources/emcdproj/cli.py: 41%
34 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-11 14:08 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-11 14:08 +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''' Command-line interface. '''
24from . import __
25from . import interfaces as _interfaces
26from . import template as _template
27from . import website as _website
30class VersionCommand(
31 _interfaces.CliCommand,
32 decorators = ( __.standard_tyro_class, ),
33):
34 ''' Prints version information. '''
36 async def __call__(
37 self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay
38 ) -> None:
39 from . import __version__
40 print( f"{__package__} {__version__}" )
41 raise SystemExit( 0 )
44class Cli(
45 __.immut.DataclassObject,
46 decorators = ( __.simple_tyro_class, ),
47):
48 ''' Various utilities for projects by Github user '@emcd'. '''
50 application: __.appcore.ApplicationInformation
51 # configfile: __.typx.Optional[ str ] = None
52 display: _interfaces.ConsoleDisplay
53 command: __.typx.Union[
54 __.typx.Annotated[
55 _template.CommandDispatcher,
56 __.tyro.conf.subcommand( 'template', prefix_name = False ),
57 ],
58 __.typx.Annotated[
59 _website.CommandDispatcher,
60 __.tyro.conf.subcommand( 'website', prefix_name = False ),
61 ],
62 __.typx.Annotated[
63 VersionCommand,
64 __.tyro.conf.subcommand( 'version', prefix_name = False ),
65 ],
66 ]
68 async def __call__( self ):
69 ''' Invokes command after library preparation. '''
70 nomargs = self.prepare_invocation_args( )
71 async with __.ctxl.AsyncExitStack( ) as exits:
72 auxdata = await _prepare( exits = exits, **nomargs )
73 ictr( 0 )( self.command )
74 await self.command( auxdata = auxdata, display = self.display )
76 def prepare_invocation_args(
77 self,
78 ) -> __.cabc.Mapping[ str, __.typx.Any ]:
79 ''' Prepares arguments for initial configuration. '''
80 # configedits: __.DictionaryEdits = (
81 # self.command.provide_configuration_edits( ) )
82 args: dict[ str, __.typx.Any ] = dict(
83 application = self.application,
84 # configedits = configedits,
85 environment = True,
86 )
87 # if self.configfile: args[ 'configfile' ] = self.configfile
88 return args
91def execute( ) -> None:
92 ''' Entrypoint for CLI execution. '''
93 from asyncio import run
94 config = (
95 __.tyro.conf.HelptextFromCommentsOff,
96 )
97 try: run( __.tyro.cli( Cli, config = config )( ) )
98 except SystemExit: raise
99 except BaseException as exc:
100 print( exc, file = __.sys.stderr )
101 raise SystemExit( 1 ) from None
104async def _prepare(
105 application: __.appcore.ApplicationInformation,
106 # configedits: __.DictionaryEdits,
107 environment: bool,
108 exits: __.ctxl.AsyncExitStack,
109) -> __.Globals:
110 ''' Configures logging based on verbosity. '''
111 import ictruck
112 # TODO: Finetune Icecream truck installation from CLI arguments.
113 ictruck.install( trace_levels = 9 )
114 return await __.appcore.prepare(
115 application = application,
116 # configedits = configedits,
117 environment = environment,
118 exits = exits )