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

33 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-29 21:50 +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 metaclass = __.ImmutableDataclass, 

46 decorators = ( __.simple_tyro_class, ), 

47): 

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

49 

50 application: __.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 ] 

67 

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 ) 

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

76 

77 def prepare_invocation_args( 

78 self, 

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

80 ''' Prepares arguments for initial configuration. ''' 

81 # configedits: __.DictionaryEdits = ( 

82 # self.command.provide_configuration_edits( ) ) 

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

84 application = self.application, 

85 # configedits = configedits, 

86 # environment = True, 

87 ) 

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

89 return args 

90 

91 

92def execute( ) -> None: 

93 ''' Entrypoint for CLI execution. ''' 

94 from asyncio import run 

95 config = ( 

96 __.tyro.conf.HelptextFromCommentsOff, 

97 ) 

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

99 except SystemExit: raise 

100 except BaseException: 

101 # TODO: Log exception. 

102 raise SystemExit( 1 ) from None 

103 

104 

105async def _prepare( 

106 application: __.ApplicationInformation, 

107 # configedits: __.DictionaryEdits, 

108 # environment: bool, 

109 exits: __.ctxl.AsyncExitStack, 

110) -> __.Globals: 

111 ''' Configures logging based on verbosity. ''' 

112 import ictruck 

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

114 ictruck.install( trace_levels = 9 ) 

115 return await __.prepare( 

116 application = application, 

117 # configedits = configedits, 

118 # environment = environment, 

119 exits = exits )