Coverage for sources/mimeogram/__/state.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-17 22:34 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-17 22:34 +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(
38 metaclass = __.ImmutableStandardDataclass,
39 decorators = ( __.standard_dataclass, ),
40):
41 ''' Immutable global data. Required by some library functions. '''
43 application: _application.Information
44 configuration: __.AccretiveDictionary[ str, __.typx.Any ]
45 directories: __.PlatformDirs
46 distribution: _distribution.Information
47 exits: __.ExitsAsync
49 def as_dictionary( self ) -> __.cabc.Mapping[ str, __.typx.Any ]:
50 ''' Returns shallow copy of state. '''
51 from dataclasses import fields
52 return {
53 field.name: getattr( self, field.name )
54 for field in fields( self ) }
56 def provide_cache_location( self, *appendages: str ) -> __.Path:
57 ''' Provides cache location from configuration. '''
58 return self.provide_location( DirectorySpecies.Cache, *appendages )
60 def provide_data_location( self, *appendages: str ) -> __.Path:
61 ''' Provides data location from configuration. '''
62 return self.provide_location( DirectorySpecies.Data, *appendages )
64 def provide_state_location( self, *appendages: str ) -> __.Path:
65 ''' Provides state location from configuration. '''
66 return self.provide_location( DirectorySpecies.State, *appendages )
68 def provide_location(
69 self, species: DirectorySpecies, *appendages: str
70 ) -> __.Path:
71 ''' Provides particular species of location from configuration. '''
72 species_name = species.value
73 base = getattr( self.directories, f"user_{species_name}_path" )
74 spec = self.configuration.get( 'locations', { } ).get( species_name )
75 if spec:
76 args: dict[ str, str | __.Path ] = {
77 f"user_{species_name}": base,
78 'user_home': __.Path.home( ),
79 'application_name': self.application.name,
80 }
81 base = __.Path( spec.format( **args ) )
82 if appendages: return base.joinpath( *appendages )
83 return base