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

32 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-07 05:16 +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 website as _website 

27 

28 

29class VersionCommand( 

30 _interfaces.CliCommand, 

31 decorators = ( __.standard_tyro_class, ), 

32): 

33 ''' Prints version information. ''' 

34 

35 async def __call__( 

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

37 ) -> None: 

38 from . import __version__ 

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

40 raise SystemExit( 0 ) 

41 

42 

43class Cli( 

44 __.immut.DataclassObject, 

45 decorators = ( __.simple_tyro_class, ), 

46): 

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

48 

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

50 display: _interfaces.ConsoleDisplay 

51 command: __.typx.Union[ 

52 __.typx.Annotated[ 

53 _website.CommandDispatcher, 

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

55 ], 

56 __.typx.Annotated[ 

57 VersionCommand, 

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

59 ], 

60 ] 

61 

62 async def __call__( self ): 

63 ''' Invokes command after library preparation. ''' 

64 nomargs = self.prepare_invocation_args( ) 

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

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

67 ictr( 0 )( self.command ) 

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

69 

70 def prepare_invocation_args( 

71 self, 

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

73 ''' Prepares arguments for initial configuration. ''' 

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

75 environment = True, 

76 ) 

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

78 return args 

79 

80 

81def execute( ) -> None: 

82 ''' Entrypoint for CLI execution. ''' 

83 from asyncio import run 

84 config = ( 

85 __.tyro.conf.EnumChoicesFromValues, 

86 __.tyro.conf.HelptextFromCommentsOff, 

87 ) 

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

89 except SystemExit: raise 

90 except BaseException as exc: 

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

92 raise SystemExit( 1 ) from None 

93 

94 

95async def _prepare( 

96 environment: bool, 

97 exits: __.ctxl.AsyncExitStack, 

98) -> __.Globals: 

99 ''' Configures logging based on verbosity. ''' 

100 import ictruck 

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

102 ictruck.install( trace_levels = 9 ) 

103 return await __.appcore.prepare( 

104 environment = environment, exits = exits )