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

35 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-13 01:21 +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 application: __.appcore.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, display = self.display ) 

75 

76 def prepare_invocation_args( 

77 self, 

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

79 ''' Prepares arguments for initial configuration. ''' 

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

81 application = self.application, 

82 environment = True, 

83 ) 

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

85 return args 

86 

87 

88def execute( ) -> None: 

89 ''' Entrypoint for CLI execution. ''' 

90 from asyncio import run 

91 config = ( 

92 __.tyro.conf.HelptextFromCommentsOff, 

93 ) 

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

95 except SystemExit: raise 

96 except BaseException as exc: 

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

98 raise SystemExit( 1 ) from None 

99 

100 

101async def _prepare( 

102 application: __.appcore.ApplicationInformation, 

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 distribution = ( 

111 await __.appcore.DistributionInformation.prepare( 

112 exits, package = __.package_name ) ) 

113 return await __.appcore.prepare( 

114 application = application, 

115 distribution = distribution, 

116 environment = environment, 

117 exits = exits )