cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_dir_path_linux

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  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__all__ = ['AppDirPath']
 20
 21
 22"""
 23Module Docstring
 24Docstrings: http://www.python.org/dev/peps/pep-0257/
 25"""
 26
 27
 28__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 29__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 30__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 31__license__ = "Apache License, Version 2.0"
 32__version__ = "4.4.1"
 33__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 34__email__ = "gtalk@butenkoms.space"
 35# __status__ = "Prototype"
 36__status__ = "Development"
 37# __status__ = "Production"
 38
 39
 40# More info:
 41# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
 42# https://wiki.archlinux.org/title/XDG_Base_Directory
 43# https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html
 44
 45
 46import os
 47from cengal.file_system.directory_manager import ensure_dir as ensure_dir_exists
 48from .app_directory_types import AppDirectoryType
 49from .app_dir_path_base import AppDirPathBase, DirTypeMappingItem, DirTypeMappingItem, BaseDirID, DirNameOrPath, norm_dir_name_or_path
 50from typing import Optional, Tuple, Dict
 51from functools import lru_cache
 52from enum import Enum
 53
 54
 55class LinuxDirectoryType(Enum):
 56    local_data = 0
 57    local_cache = 1
 58    local_config = 2
 59    local_log = 3
 60    local_temp = 4
 61    local_runtime = 5
 62
 63    program_data = 6
 64    program_cache = 7
 65    program_config = 8
 66    program_log = 9
 67    program_temp = 10
 68    program_runtime = 11
 69
 70    program_files = 12
 71
 72    user_profile_data = 13
 73    user_profile_program_files = 14
 74    user_profile_program_files_common = 15
 75
 76    local_static_data = 16
 77    program_static_data = 17
 78
 79
 80dir_type_mapping: Dict[AppDirectoryType, Tuple[LinuxDirectoryType, Optional[DirNameOrPath], Optional[DirNameOrPath]]] = {
 81    AppDirectoryType.local_data: (LinuxDirectoryType.local_data, None, ('local', 'data')),
 82    AppDirectoryType.local_static_data: (LinuxDirectoryType.local_static_data, None, ('local', 'static_data')),
 83    AppDirectoryType.local_cache: (LinuxDirectoryType.local_cache, None, ('local', 'cache')),
 84    AppDirectoryType.local_config: (LinuxDirectoryType.local_config, None, ('local', 'config')),
 85    AppDirectoryType.local_log: (LinuxDirectoryType.local_log, None, ('local', 'log')),
 86    AppDirectoryType.local_temp: (LinuxDirectoryType.local_temp, None, ('local', 'temp')),
 87    AppDirectoryType.local_runtime: (LinuxDirectoryType.local_runtime, None, ('local', 'runtime')),
 88
 89    AppDirectoryType.local_low_data: (LinuxDirectoryType.local_data, None, ('local_low', 'data')),
 90    AppDirectoryType.local_low_static_data: (LinuxDirectoryType.local_static_data, None, ('local_low', 'static_data')),
 91    AppDirectoryType.local_low_cache: (LinuxDirectoryType.local_cache, None, ('local_low', 'cache')),
 92    AppDirectoryType.local_low_config: (LinuxDirectoryType.local_config, None, ('local_low', 'config')),
 93    AppDirectoryType.local_low_log: (LinuxDirectoryType.local_log, None, ('local_low', 'log')),
 94    AppDirectoryType.local_low_temp: (LinuxDirectoryType.local_temp, None, ('local_low', 'temp')),
 95    AppDirectoryType.local_low_runtime: (LinuxDirectoryType.local_runtime, None, ('local_low', 'runtime')),
 96
 97    AppDirectoryType.roaming_data: (LinuxDirectoryType.local_data, None, ('roaming', 'data')),
 98    AppDirectoryType.roaming_static_data: (LinuxDirectoryType.local_static_data, None, ('roaming', 'static_data')),
 99    AppDirectoryType.roaming_cache: (LinuxDirectoryType.local_cache, None, ('roaming', 'cache')),
100    AppDirectoryType.roaming_config: (LinuxDirectoryType.local_config, None, ('roaming', 'config')),
101    AppDirectoryType.roaming_log: (LinuxDirectoryType.local_log, None, ('roaming', 'log')),
102    AppDirectoryType.roaming_temp: (LinuxDirectoryType.local_temp, None, ('roaming', 'temp')),
103    AppDirectoryType.roaming_runtime: (LinuxDirectoryType.local_runtime, None, ('roaming', 'runtime')),
104
105    AppDirectoryType.program_data: (LinuxDirectoryType.program_data, None, 'data'),
106    AppDirectoryType.program_static_data: (LinuxDirectoryType.program_static_data, None, 'static_data'),
107    AppDirectoryType.program_cache: (LinuxDirectoryType.program_cache, None, 'cache'),
108    AppDirectoryType.program_config: (LinuxDirectoryType.program_config, None, 'config'),
109    AppDirectoryType.program_log: (LinuxDirectoryType.program_log, None, 'log'),
110    AppDirectoryType.program_temp: (LinuxDirectoryType.program_temp, None, 'temp'),
111    AppDirectoryType.program_runtime: (LinuxDirectoryType.program_runtime, None, 'runtime'),
112
113    AppDirectoryType.program_files: (LinuxDirectoryType.program_files, None, None),
114    AppDirectoryType.program_files_common: (LinuxDirectoryType.program_files, None, None),
115
116    AppDirectoryType.user_profile_data: (LinuxDirectoryType.user_profile_data, None, None),
117    AppDirectoryType.user_profile_program_files: (LinuxDirectoryType.local_data, None, None),
118    AppDirectoryType.user_profile_program_files_common: (LinuxDirectoryType.local_data, None, None),
119}
120
121
122class AppDirPath(AppDirPathBase):
123    def __init__(self, max_cache_size: Optional[int] = None) -> None:
124        super().__init__(max_cache_size)
125    
126    def dir_type_mapping(self, dir_type: AppDirectoryType) -> DirTypeMappingItem:
127        return dir_type_mapping[dir_type]
128    
129    def base_dir_id_to_path(self, base_dir_id: BaseDirID) -> str:
130        result_path = None
131
132        if LinuxDirectoryType.local_data == base_dir_id:
133            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
134        elif LinuxDirectoryType.local_static_data == base_dir_id:
135            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
136        elif LinuxDirectoryType.local_cache == base_dir_id:
137            result_path = os.environ.get('XDG_CACHE_HOME', os.path.join(os.path.expanduser('~'), '.cache'))
138        elif LinuxDirectoryType.local_config == base_dir_id:
139            result_path = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config'))
140        elif LinuxDirectoryType.local_log == base_dir_id:
141            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
142        elif LinuxDirectoryType.local_temp == base_dir_id:
143            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
144        elif LinuxDirectoryType.local_runtime == base_dir_id:
145            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
146        elif LinuxDirectoryType.program_data == base_dir_id:
147            result_path = '/var/lib'
148        elif LinuxDirectoryType.program_static_data == base_dir_id:
149            result_path = '/usr/share'
150        elif LinuxDirectoryType.program_cache == base_dir_id:
151            result_path = '/var/cache'
152        elif LinuxDirectoryType.program_config == base_dir_id:
153            result_path = '/etc'
154        elif LinuxDirectoryType.program_log == base_dir_id:
155            result_path = '/var/log'
156        elif LinuxDirectoryType.program_temp == base_dir_id:
157            result_path = '/tmp'
158        elif LinuxDirectoryType.program_runtime == base_dir_id:
159            result_path = '/var/run'
160        elif LinuxDirectoryType.program_files == base_dir_id:
161            result_path = '/opt'
162        elif LinuxDirectoryType.user_profile_data == base_dir_id:
163            result_path = os.path.expanduser('~')
164        elif LinuxDirectoryType.user_profile_program_files == base_dir_id:
165            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
166        elif LinuxDirectoryType.user_profile_program_files_common == base_dir_id:
167            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
168
169        return result_path
class AppDirPath(cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_dir_path_base.AppDirPathBase):
123class AppDirPath(AppDirPathBase):
124    def __init__(self, max_cache_size: Optional[int] = None) -> None:
125        super().__init__(max_cache_size)
126    
127    def dir_type_mapping(self, dir_type: AppDirectoryType) -> DirTypeMappingItem:
128        return dir_type_mapping[dir_type]
129    
130    def base_dir_id_to_path(self, base_dir_id: BaseDirID) -> str:
131        result_path = None
132
133        if LinuxDirectoryType.local_data == base_dir_id:
134            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
135        elif LinuxDirectoryType.local_static_data == base_dir_id:
136            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
137        elif LinuxDirectoryType.local_cache == base_dir_id:
138            result_path = os.environ.get('XDG_CACHE_HOME', os.path.join(os.path.expanduser('~'), '.cache'))
139        elif LinuxDirectoryType.local_config == base_dir_id:
140            result_path = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config'))
141        elif LinuxDirectoryType.local_log == base_dir_id:
142            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
143        elif LinuxDirectoryType.local_temp == base_dir_id:
144            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
145        elif LinuxDirectoryType.local_runtime == base_dir_id:
146            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
147        elif LinuxDirectoryType.program_data == base_dir_id:
148            result_path = '/var/lib'
149        elif LinuxDirectoryType.program_static_data == base_dir_id:
150            result_path = '/usr/share'
151        elif LinuxDirectoryType.program_cache == base_dir_id:
152            result_path = '/var/cache'
153        elif LinuxDirectoryType.program_config == base_dir_id:
154            result_path = '/etc'
155        elif LinuxDirectoryType.program_log == base_dir_id:
156            result_path = '/var/log'
157        elif LinuxDirectoryType.program_temp == base_dir_id:
158            result_path = '/tmp'
159        elif LinuxDirectoryType.program_runtime == base_dir_id:
160            result_path = '/var/run'
161        elif LinuxDirectoryType.program_files == base_dir_id:
162            result_path = '/opt'
163        elif LinuxDirectoryType.user_profile_data == base_dir_id:
164            result_path = os.path.expanduser('~')
165        elif LinuxDirectoryType.user_profile_program_files == base_dir_id:
166            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
167        elif LinuxDirectoryType.user_profile_program_files_common == base_dir_id:
168            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
169
170        return result_path
AppDirPath(max_cache_size: typing.Union[int, NoneType] = None)
124    def __init__(self, max_cache_size: Optional[int] = None) -> None:
125        super().__init__(max_cache_size)
def dir_type_mapping( self, dir_type: cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_directory_types.AppDirectoryType) -> Tuple[Any, Union[str, Sequence[str], NoneType], Union[str, Sequence[str], NoneType]]:
127    def dir_type_mapping(self, dir_type: AppDirectoryType) -> DirTypeMappingItem:
128        return dir_type_mapping[dir_type]
def base_dir_id_to_path(self, base_dir_id: typing.Any) -> str:
130    def base_dir_id_to_path(self, base_dir_id: BaseDirID) -> str:
131        result_path = None
132
133        if LinuxDirectoryType.local_data == base_dir_id:
134            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
135        elif LinuxDirectoryType.local_static_data == base_dir_id:
136            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
137        elif LinuxDirectoryType.local_cache == base_dir_id:
138            result_path = os.environ.get('XDG_CACHE_HOME', os.path.join(os.path.expanduser('~'), '.cache'))
139        elif LinuxDirectoryType.local_config == base_dir_id:
140            result_path = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config'))
141        elif LinuxDirectoryType.local_log == base_dir_id:
142            result_path = os.environ.get('XDG_STATE_HOME', os.path.join(os.path.expanduser('~'), '.local', 'state'))
143        elif LinuxDirectoryType.local_temp == base_dir_id:
144            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
145        elif LinuxDirectoryType.local_runtime == base_dir_id:
146            result_path = os.environ.get('XDG_RUNTIME_DIR', os.path.join(os.path.expanduser('~'), '.tmp'))
147        elif LinuxDirectoryType.program_data == base_dir_id:
148            result_path = '/var/lib'
149        elif LinuxDirectoryType.program_static_data == base_dir_id:
150            result_path = '/usr/share'
151        elif LinuxDirectoryType.program_cache == base_dir_id:
152            result_path = '/var/cache'
153        elif LinuxDirectoryType.program_config == base_dir_id:
154            result_path = '/etc'
155        elif LinuxDirectoryType.program_log == base_dir_id:
156            result_path = '/var/log'
157        elif LinuxDirectoryType.program_temp == base_dir_id:
158            result_path = '/tmp'
159        elif LinuxDirectoryType.program_runtime == base_dir_id:
160            result_path = '/var/run'
161        elif LinuxDirectoryType.program_files == base_dir_id:
162            result_path = '/opt'
163        elif LinuxDirectoryType.user_profile_data == base_dir_id:
164            result_path = os.path.expanduser('~')
165        elif LinuxDirectoryType.user_profile_program_files == base_dir_id:
166            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
167        elif LinuxDirectoryType.user_profile_program_files_common == base_dir_id:
168            result_path = os.environ.get('XDG_DATA_HOME', os.path.join(os.path.expanduser('~'), '.local', 'share'))
169
170        return result_path
Inherited Members
cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_dir_path_base.AppDirPathBase
get_cache_size
set_cache_size
cached