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

33 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-08 18:36 +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 website as _website 

29 

30 

31class VersionCommand( 

32 _interfaces.CliCommand, 

33 decorators = ( __.standard_tyro_class, ), 

34): 

35 ''' Prints version information. ''' 

36 

37 async def __call__( self, auxdata: __.Globals ) -> None: 

38 from . import __version__ # pylint: disable=cyclic-import 

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

40 raise SystemExit( 0 ) 

41 

42 

43class Cli( 

44 metaclass = __.ImmutableDataclass, 

45 decorators = ( __.simple_tyro_class, ), 

46): 

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

48 

49 application: __.ApplicationInformation 

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

51 # display: ConsoleDisplay 

52 command: __.typx.Union[ 

53 __.typx.Annotated[ 

54 _website.CommandDispatcher, 

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

56 ], 

57 __.typx.Annotated[ 

58 VersionCommand, 

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

60 ], 

61 ] 

62 

63 async def __call__( self ): 

64 ''' Invokes command after library preparation. ''' 

65 nomargs = self.prepare_invocation_args( ) 

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

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

68 ictr( 0 )( self.command ) 

69 await self.command( auxdata = auxdata ) 

70 # await self.command( auxdata = auxdata, display = self.display ) 

71 

72 def prepare_invocation_args( 

73 self, 

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

75 ''' Prepares arguments for initial configuration. ''' 

76 # configedits: __.DictionaryEdits = ( 

77 # self.command.provide_configuration_edits( ) ) 

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

79 application = self.application, 

80 # configedits = configedits, 

81 # environment = True, 

82 ) 

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

84 return args 

85 

86 

87def execute( ) -> None: 

88 ''' Entrypoint for CLI execution. ''' 

89 from asyncio import run 

90 config = ( 

91 __.tyro.conf.HelptextFromCommentsOff, 

92 ) 

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

94 except SystemExit: raise 

95 except BaseException: 

96 # TODO: Log exception. 

97 raise SystemExit( 1 ) from None 

98 

99 

100async def _prepare( 

101 application: __.ApplicationInformation, 

102 # configedits: __.DictionaryEdits, 

103 # environment: bool, 

104 exits: __.ctxl.AsyncExitStack, 

105) -> __.Globals: 

106 ''' Configures logging based on verbosity. ''' 

107 import ictruck 

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

109 ictruck.install( trace_levels = 9 ) 

110 auxdata = await __.prepare( 

111 application = application, 

112 # configedits = configedits, 

113 # environment = environment, 

114 exits = exits ) 

115 return auxdata