Coverage for sources/appcore/preparation.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-09-26 19:13 +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''' Preparation of the application core. ''' 

22 

23 

24from . import __ 

25from . import application as _application 

26from . import configuration as _configuration 

27from . import dictedits as _dictedits 

28from . import distribution as _distribution 

29from . import environment as _environment 

30from . import inscription as _inscription 

31from . import state as _state 

32 

33 

34_configuration_acquirer = _configuration.TomlAcquirer( ) 

35 

36async def prepare( # noqa: PLR0913 

37 exits: __.ctxl.AsyncExitStack, 

38 acquirer: _configuration.AcquirerAbc = _configuration_acquirer, 

39 application: __.Absential[ _application.Information ] = __.absent, 

40 configedits: _dictedits.Edits = ( ), 

41 configfile: __.Absential[ __.Path | __.io.TextIOBase ] = __.absent, 

42 directories: __.Absential[ __.pdirs.PlatformDirs ] = __.absent, 

43 distribution: __.Absential[ _distribution.Information ] = __.absent, 

44 environment: bool | __.NominativeDictionary = False, 

45 inscription: __.Absential[ _inscription.Control ] = __.absent, 

46) -> _state.Globals: 

47 ''' Prepares globals DTO to pass through application. 

48 

49 Also: 

50 * Optionally, configures logging for application 

51 * Optionally, loads process environment from files. 

52 

53 Note that asynchronous preparation allows for applications to 

54 concurrently initialize other entities outside of the library, even 

55 though the library initialization, itself, is inherently sequential. 

56 ''' 

57 if __.is_absent( distribution ): 

58 distribution = ( 

59 await _distribution.Information.prepare( exits = exits ) ) 

60 if __.is_absent( application ): 

61 application = ( 

62 _application.Information( name = distribution.name ) ) 

63 if __.is_absent( directories ): 

64 directories = application.produce_platform_directories( ) 

65 configuration = ( 

66 await acquirer( 

67 application_name = application.name, 

68 directories = directories, 

69 distribution = distribution, 

70 edits = configedits, 

71 file = configfile ) ) 

72 auxdata = _state.Globals( 

73 application = application, 

74 configuration = configuration, 

75 directories = directories, 

76 distribution = distribution, 

77 exits = exits ) 

78 if environment: 

79 if isinstance( environment, __.cabc.Mapping ): 

80 __.os.environ.update( environment ) 

81 else: await _environment.update( auxdata ) 

82 if __.is_absent( inscription ): 

83 inscription = _inscription.Control( ) 

84 _inscription.prepare( auxdata, control = inscription ) 

85 _inscribe_preparation_report( auxdata ) 

86 return auxdata 

87 

88 

89def _inscribe_preparation_report( auxdata: _state.Globals ): 

90 scribe = __.produce_scribe( __.package_name ) 

91 scribe.debug( f"Application Name: {auxdata.application.name}" ) 

92 scribe.debug( "Application Cache Location: {}".format( 

93 auxdata.provide_cache_location( ) ) ) 

94 scribe.debug( "Application Data Location: {}".format( 

95 auxdata.provide_data_location( ) ) ) 

96 scribe.debug( "Application State Location: {}".format( 

97 auxdata.provide_state_location( ) ) ) 

98 scribe.debug( "Package Data Location: {}".format( 

99 auxdata.distribution.provide_data_location( ) ) )