Coverage for sources/emcdproj/cli.py: 39%

33 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-16 21:44 +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''' Command-line interface. ''' 

22 

23 

24from . import __ 

25from . import interfaces as _interfaces 

26from . import template as _template 

27from . import website as _website 

28 

29 

30class VersionCommand( 

31 _interfaces.CliCommand, 

32 decorators = ( __.standard_tyro_class, ), 

33): 

34 ''' Prints version information. ''' 

35 

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 ) 

42 

43 

44class Cli( 

45 __.immut.DataclassObject, 

46 decorators = ( __.simple_tyro_class, ), 

47): 

48 ''' Various utilities for projects by Github user '@emcd'. ''' 

49 

50 # configfile: __.typx.Optional[ str ] = None 

51 display: _interfaces.ConsoleDisplay 

52 command: __.typx.Union[ 

53 __.typx.Annotated[ 

54 _template.CommandDispatcher, 

55 __.tyro.conf.subcommand( 'template', prefix_name = False ), 

56 ], 

57 __.typx.Annotated[ 

58 _website.CommandDispatcher, 

59 __.tyro.conf.subcommand( 'website', prefix_name = False ), 

60 ], 

61 __.typx.Annotated[ 

62 VersionCommand, 

63 __.tyro.conf.subcommand( 'version', prefix_name = False ), 

64 ], 

65 ] 

66 

67 async def __call__( self ): 

68 ''' Invokes command after library preparation. ''' 

69 nomargs = self.prepare_invocation_args( ) 

70 async with __.ctxl.AsyncExitStack( ) as exits: 

71 auxdata = await _prepare( exits = exits, **nomargs ) 

72 ictr( 0 )( self.command ) 

73 await self.command( auxdata = auxdata, display = self.display ) 

74 

75 def prepare_invocation_args( 

76 self, 

77 ) -> __.cabc.Mapping[ str, __.typx.Any ]: 

78 ''' Prepares arguments for initial configuration. ''' 

79 args: dict[ str, __.typx.Any ] = dict( 

80 environment = True, 

81 ) 

82 # if self.configfile: args[ 'configfile' ] = self.configfile 

83 return args 

84 

85 

86def execute( ) -> None: 

87 ''' Entrypoint for CLI execution. ''' 

88 from asyncio import run 

89 config = ( 

90 __.tyro.conf.HelptextFromCommentsOff, 

91 ) 

92 try: run( __.tyro.cli( Cli, config = config )( ) ) 

93 except SystemExit: raise 

94 except BaseException as exc: 

95 print( exc, file = __.sys.stderr ) 

96 raise SystemExit( 1 ) from None 

97 

98 

99async def _prepare( 

100 environment: bool, 

101 exits: __.ctxl.AsyncExitStack, 

102) -> __.Globals: 

103 ''' Configures logging based on verbosity. ''' 

104 import ictruck 

105 # TODO: Finetune Icecream truck installation from CLI arguments. 

106 ictruck.install( trace_levels = 9 ) 

107 return await __.appcore.prepare( 

108 environment = environment, exits = exits )