Coverage for sources/mimeogram/__/state.py: 100%
31 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-05 19:15 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-05 19:15 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
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#============================================================================#
21''' Immutable global state. '''
24from . import imports as __
25from . import application as _application
26from . import distribution as _distribution
29class DirectorySpecies( __.enum.Enum ): # TODO: Python 3.11: StrEnum
30 ''' Possible species for locations. '''
32 Cache = 'cache'
33 Data = 'data'
34 State = 'state'
37class Globals( __.immut.DataclassObject ):
38 ''' Immutable global data. Required by some library functions. '''
40 application: _application.Information
41 configuration: __.accret.Dictionary[ str, __.typx.Any ]
42 directories: __.PlatformDirs
43 distribution: _distribution.Information
44 exits: __.ExitsAsync
46 def as_dictionary( self ) -> __.cabc.Mapping[ str, __.typx.Any ]:
47 ''' Returns shallow copy of state. '''
48 from dataclasses import fields
49 return {
50 field.name: getattr( self, field.name )
51 for field in fields( self ) }
53 def provide_cache_location( self, *appendages: str ) -> __.Path:
54 ''' Provides cache location from configuration. '''
55 return self.provide_location( DirectorySpecies.Cache, *appendages )
57 def provide_data_location( self, *appendages: str ) -> __.Path:
58 ''' Provides data location from configuration. '''
59 return self.provide_location( DirectorySpecies.Data, *appendages )
61 def provide_state_location( self, *appendages: str ) -> __.Path:
62 ''' Provides state location from configuration. '''
63 return self.provide_location( DirectorySpecies.State, *appendages )
65 def provide_location(
66 self, species: DirectorySpecies, *appendages: str
67 ) -> __.Path:
68 ''' Provides particular species of location from configuration. '''
69 species_name = species.value
70 base = getattr( self.directories, f"user_{species_name}_path" )
71 spec = self.configuration.get( 'locations', { } ).get( species_name )
72 if spec:
73 args: dict[ str, str | __.Path ] = {
74 f"user_{species_name}": base,
75 'user_home': __.Path.home( ),
76 'application_name': self.application.name,
77 }
78 base = __.Path( spec.format( **args ) )
79 if appendages: return base.joinpath( *appendages )
80 return base