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

34 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-05-27 20:29 +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 __future__ import annotations 

25 

26from . import __ 

27from . import interfaces as _interfaces 

28from . import template as _template 

29from . import website as _website 

30 

31 

32class VersionCommand( 

33 _interfaces.CliCommand, 

34 decorators = ( __.standard_tyro_class, ), 

35): 

36 ''' Prints version information. ''' 

37 

38 async def __call__( 

39 self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay 

40 ) -> None: 

41 from . import __version__ 

42 print( f"{__package__} {__version__}" ) 

43 raise SystemExit( 0 ) 

44 

45 

46class Cli( 

47 metaclass = __.ImmutableDataclass, 

48 decorators = ( __.simple_tyro_class, ), 

49): 

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

51 

52 application: __.ApplicationInformation 

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

54 display: _interfaces.ConsoleDisplay 

55 command: __.typx.Union[ 

56 __.typx.Annotated[ 

57 _template.CommandDispatcher, 

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

59 ], 

60 __.typx.Annotated[ 

61 _website.CommandDispatcher, 

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

63 ], 

64 __.typx.Annotated[ 

65 VersionCommand, 

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

67 ], 

68 ] 

69 

70 async def __call__( self ): 

71 ''' Invokes command after library preparation. ''' 

72 nomargs = self.prepare_invocation_args( ) 

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

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

75 ictr( 0 )( self.command ) 

76 # await self.command( auxdata = auxdata ) 

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

78 

79 def prepare_invocation_args( 

80 self, 

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

82 ''' Prepares arguments for initial configuration. ''' 

83 # configedits: __.DictionaryEdits = ( 

84 # self.command.provide_configuration_edits( ) ) 

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

86 application = self.application, 

87 # configedits = configedits, 

88 # environment = True, 

89 ) 

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

91 return args 

92 

93 

94def execute( ) -> None: 

95 ''' Entrypoint for CLI execution. ''' 

96 from asyncio import run 

97 config = ( 

98 __.tyro.conf.HelptextFromCommentsOff, 

99 ) 

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

101 except SystemExit: raise 

102 except BaseException: 

103 # TODO: Log exception. 

104 raise SystemExit( 1 ) from None 

105 

106 

107async def _prepare( 

108 application: __.ApplicationInformation, 

109 # configedits: __.DictionaryEdits, 

110 # environment: bool, 

111 exits: __.ctxl.AsyncExitStack, 

112) -> __.Globals: 

113 ''' Configures logging based on verbosity. ''' 

114 import ictruck 

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

116 ictruck.install( trace_levels = 9 ) 

117 return await __.prepare( 

118 application = application, 

119 # configedits = configedits, 

120 # environment = environment, 

121 exits = exits )