Coverage for sources/mimeogram/__/environment.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-16 01:42 +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''' Persistent and active process environment values. ''' 

22 

23 

24from . import imports as __ 

25from . import io as _io 

26from . import state as _state 

27 

28 

29_index_name = 'environment' 

30 

31 

32async def update( auxdata: _state.Globals ): 

33 ''' Updates process environment from dot files. 

34 

35 For editable installations (development environments): 

36 - If project-level .env exists, use it exclusively. 

37 - Otherwise fall through to normal behavior. 

38 

39 For normal installations: 

40 - Merge configured and local .env files. 

41 - Local values take precedence over configured values. 

42 ''' 

43 if auxdata.distribution.editable: 

44 location = __.Path( auxdata.distribution.location ) / '.env' 

45 if location.exists( ): 

46 files = ( 

47 location.glob( '*.env' ) 

48 if location.is_dir( ) else ( location, ) ) 

49 await _io.acquire_text_files_async( 

50 *( file for file in files ), 

51 deserializer = _inject_dotenv_data ) 

52 return 

53 locations: list[ __.Path ] = [ ] 

54 template = auxdata.configuration.get( 'locations', { } ).get( _index_name ) 

55 if template: 

56 location = __.Path( template.format( 

57 user_configuration = auxdata.directories.user_config_path, 

58 user_home = __.Path.home( ) ) ) 

59 if location.exists( ): locations.append( location ) 

60 location = __.Path( ) / '.env' 

61 if location.exists( ): locations.append( location ) 

62 # Process locations in reverse precedence order. 

63 for location in reversed( locations ): 

64 files = ( 

65 location.glob( '*.env' ) 

66 if location.is_dir( ) else ( location, ) ) 

67 await _io.acquire_text_files_async( 

68 *( file for file in files ), 

69 deserializer = _inject_dotenv_data ) 

70 

71 

72def _inject_dotenv_data( data: str ) -> bool: 

73 from io import StringIO 

74 from dotenv import load_dotenv 

75 return load_dotenv( stream = StringIO( data ) )