cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_dir_path_base
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__ = ['DirNameOrPath', 'BaseDirID', 'DirTypeMapping', 'DirTypeMappingItem', 'norm_dir_name_or_path', 'AppDirPathBase'] 20 21 22""" 23Module Docstring 24Docstrings: http://www.python.org/dev/peps/pep-0257/ 25""" 26 27__author__ = "ButenkoMS <gtalk@butenkoms.space>" 28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 30__license__ = "Apache License, Version 2.0" 31__version__ = "4.4.1" 32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 33__email__ = "gtalk@butenkoms.space" 34# __status__ = "Prototype" 35__status__ = "Development" 36# __status__ = "Production" 37 38 39import os 40from cengal.file_system.directory_manager import ensure_dir as ensure_dir_exists 41from .app_directory_types import AppDirectoryType 42from typing import Union, Sequence, Any, Optional, Tuple, Dict 43from functools import lru_cache 44 45 46DirNameOrPath = Union[str, Sequence[str], None] 47BaseDirID = Any 48DirTypeMapping = Dict[AppDirectoryType, Tuple[BaseDirID, Optional[DirNameOrPath], Optional[DirNameOrPath]]] 49DirTypeMappingItem = Tuple[BaseDirID, Optional[DirNameOrPath], Optional[DirNameOrPath]] 50 51 52def norm_dir_name_or_path(dir_name_or_path: DirNameOrPath) -> Tuple[str, ...]: 53 if dir_name_or_path is None: 54 return () 55 elif isinstance(dir_name_or_path, str): 56 return (dir_name_or_path,) 57 elif isinstance(dir_name_or_path, (list, tuple)): 58 # return tuple(dir_name_or_path) 59 return dir_name_or_path 60 else: 61 raise TypeError('dir_name_or_path must be str, list or tuple') 62 63 64class AppDirPathBase: 65 def __init__(self, max_cache_size: Optional[int] = None) -> None: 66 if max_cache_size is None: 67 max_cache_size = len(AppDirectoryType) * 2 * 2 * 10 # 10 - is a number of applications; 2 - is `with_structure` [True/False]; 2 - is `ensure_dir` [True/False] 68 69 self._max_cache_size = max_cache_size 70 self._cached = self.__call__ 71 self.set_cache_size(max_cache_size) 72 73 def get_cache_size(self) -> int: 74 return self._max_cache_size 75 76 def set_cache_size(self, max_cache_size: int) -> None: 77 self._max_cache_size = max_cache_size 78 del self._cached 79 self._cached = lru_cache(maxsize=max_cache_size, typed=True)(self.__call__) 80 81 def dir_type_mapping(self, dir_type: AppDirectoryType) -> DirTypeMappingItem: 82 raise NotImplementedError 83 84 def base_dir_id_to_path(self, base_dir_id: BaseDirID) -> str: 85 raise NotImplementedError 86 87 def __call__(self, dir_type: AppDirectoryType, app_name_or_path: Optional[DirNameOrPath] = None, with_structure: bool = True, ensure_dir: bool = True) -> str: 88 if app_name_or_path is not None: 89 app_name_or_path = str(app_name_or_path) 90 91 if AppDirectoryType.user_profile_data == dir_type: 92 if app_name_or_path is not None: 93 app_name_or_path = '.' + os.path.join(*norm_dir_name_or_path(app_name_or_path)) 94 95 mapping: DirTypeMappingItem = self.dir_type_mapping(dir_type) 96 base_dir_path = self.base_dir_id_to_path(mapping[0]) 97 result_list = [base_dir_path] 98 result_list.extend(norm_dir_name_or_path(mapping[1])) 99 if app_name_or_path is not None: 100 result_list.extend(norm_dir_name_or_path(app_name_or_path)) 101 if with_structure: 102 result_list.extend(norm_dir_name_or_path(mapping[2])) 103 104 result_path = os.path.normpath(os.path.join(*result_list)) 105 if ensure_dir: 106 ensure_dir_exists(result_path) 107 108 return result_path 109 110 def cached(self, dir_type: AppDirectoryType, app_name_or_path: Optional[DirNameOrPath] = None, with_structure: bool = True, ensure_dir: bool = True) -> str: 111 return self._cached(dir_type, app_name_or_path, with_structure, ensure_dir)
DirNameOrPath =
typing.Union[str, typing.Sequence[str], NoneType]
BaseDirID =
typing.Any
DirTypeMapping =
typing.Dict[cengal.file_system.app_fs_structure.app_dir_path.versions.v_0.app_directory_types.AppDirectoryType, typing.Tuple[typing.Any, typing.Union[str, typing.Sequence[str], NoneType], typing.Union[str, typing.Sequence[str], NoneType]]]
DirTypeMappingItem =
typing.Tuple[typing.Any, typing.Union[str, typing.Sequence[str], NoneType], typing.Union[str, typing.Sequence[str], NoneType]]
def
norm_dir_name_or_path( dir_name_or_path: typing.Union[str, typing.Sequence[str], NoneType]) -> Tuple[str, ...]:
53def norm_dir_name_or_path(dir_name_or_path: DirNameOrPath) -> Tuple[str, ...]: 54 if dir_name_or_path is None: 55 return () 56 elif isinstance(dir_name_or_path, str): 57 return (dir_name_or_path,) 58 elif isinstance(dir_name_or_path, (list, tuple)): 59 # return tuple(dir_name_or_path) 60 return dir_name_or_path 61 else: 62 raise TypeError('dir_name_or_path must be str, list or tuple')
class
AppDirPathBase:
65class AppDirPathBase: 66 def __init__(self, max_cache_size: Optional[int] = None) -> None: 67 if max_cache_size is None: 68 max_cache_size = len(AppDirectoryType) * 2 * 2 * 10 # 10 - is a number of applications; 2 - is `with_structure` [True/False]; 2 - is `ensure_dir` [True/False] 69 70 self._max_cache_size = max_cache_size 71 self._cached = self.__call__ 72 self.set_cache_size(max_cache_size) 73 74 def get_cache_size(self) -> int: 75 return self._max_cache_size 76 77 def set_cache_size(self, max_cache_size: int) -> None: 78 self._max_cache_size = max_cache_size 79 del self._cached 80 self._cached = lru_cache(maxsize=max_cache_size, typed=True)(self.__call__) 81 82 def dir_type_mapping(self, dir_type: AppDirectoryType) -> DirTypeMappingItem: 83 raise NotImplementedError 84 85 def base_dir_id_to_path(self, base_dir_id: BaseDirID) -> str: 86 raise NotImplementedError 87 88 def __call__(self, dir_type: AppDirectoryType, app_name_or_path: Optional[DirNameOrPath] = None, with_structure: bool = True, ensure_dir: bool = True) -> str: 89 if app_name_or_path is not None: 90 app_name_or_path = str(app_name_or_path) 91 92 if AppDirectoryType.user_profile_data == dir_type: 93 if app_name_or_path is not None: 94 app_name_or_path = '.' + os.path.join(*norm_dir_name_or_path(app_name_or_path)) 95 96 mapping: DirTypeMappingItem = self.dir_type_mapping(dir_type) 97 base_dir_path = self.base_dir_id_to_path(mapping[0]) 98 result_list = [base_dir_path] 99 result_list.extend(norm_dir_name_or_path(mapping[1])) 100 if app_name_or_path is not None: 101 result_list.extend(norm_dir_name_or_path(app_name_or_path)) 102 if with_structure: 103 result_list.extend(norm_dir_name_or_path(mapping[2])) 104 105 result_path = os.path.normpath(os.path.join(*result_list)) 106 if ensure_dir: 107 ensure_dir_exists(result_path) 108 109 return result_path 110 111 def cached(self, dir_type: AppDirectoryType, app_name_or_path: Optional[DirNameOrPath] = None, with_structure: bool = True, ensure_dir: bool = True) -> str: 112 return self._cached(dir_type, app_name_or_path, with_structure, ensure_dir)
AppDirPathBase(max_cache_size: typing.Union[int, NoneType] = None)
66 def __init__(self, max_cache_size: Optional[int] = None) -> None: 67 if max_cache_size is None: 68 max_cache_size = len(AppDirectoryType) * 2 * 2 * 10 # 10 - is a number of applications; 2 - is `with_structure` [True/False]; 2 - is `ensure_dir` [True/False] 69 70 self._max_cache_size = max_cache_size 71 self._cached = self.__call__ 72 self.set_cache_size(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]]: