cengal.file_system.path_manager.versions.v_0.path_manager

  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__all__ = ['RelativePath', 'relative_to_src', 'path_relative_to_src', 'relative_to_cwd', 
 19           'path_relative_to_cwd', 'WrongBaseDirError', 'get_relative_path_part', 'sep', 
 20           'canonical_path']
 21
 22import os
 23from os.path import normpath, expanduser, realpath, abspath, normcase, sep
 24from typing import Optional
 25from cengal.file_system.directory_manager import current_src_dir
 26from cengal.text_processing.text_processing import removeprefix
 27
 28"""
 29Module Docstring
 30Docstrings: http://www.python.org/dev/peps/pep-0257/
 31"""
 32
 33__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 34__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 35__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 36__license__ = "Apache License, Version 2.0"
 37__version__ = "4.4.1"
 38__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 39__email__ = "gtalk@butenkoms.space"
 40# __status__ = "Prototype"
 41__status__ = "Development"
 42# __status__ = "Production"
 43
 44
 45class RelativePath:
 46    def __init__(self, base_path: str):
 47        self.base_path: str = base_path
 48
 49    def __call__(self, relative_path: str):
 50        return normpath(os.path.join(self.base_path, relative_path))
 51
 52
 53def relative_to_src(depth: Optional[int] = 1) -> RelativePath:
 54    """
 55
 56    :param depth: 0 - path of this file, 1 - path of the caller's file, etc.
 57    :return:
 58    """
 59    depth = depth or 0
 60    depth += 1
 61    return RelativePath(current_src_dir(depth))
 62
 63
 64def path_relative_to_src(relative_path: str, depth: Optional[int] = 1) -> str:
 65    """
 66
 67    :param relative_path:
 68    :param depth: 0 - path of this file, 1 - path of the caller's file, etc.
 69    :return:
 70    """
 71    depth = depth or 0
 72    depth += 1
 73    return RelativePath(current_src_dir(depth))(relative_path)
 74
 75
 76def relative_to_cwd() -> RelativePath:
 77    return RelativePath(os.getcwd())
 78
 79
 80def path_relative_to_cwd(relative_path: str) -> str:
 81    return RelativePath(os.getcwd())(relative_path)
 82
 83
 84class WrongBaseDirError(Exception):
 85    pass
 86
 87
 88def get_relative_path_part(path: str, base_dir: str) -> str:
 89    path = normpath(path)
 90    base_dir = normpath(base_dir)
 91    if not path.startswith(base_dir):
 92        raise WrongBaseDirError
 93    
 94    relative_part = removeprefix(path, base_dir)
 95    if relative_part.startswith(sep):
 96        relative_part = removeprefix(relative_part, sep)
 97    
 98    return relative_part
 99
100
101def canonical_path(path: str, resolve_symlinks: bool = True) -> str:
102    """
103    Convert a path to its canonical, case-normalized, absolute version.
104    """
105
106    path = expanduser(path)
107    if resolve_symlinks:
108        path = realpath(path)
109    else:
110        path = abspath(path)
111    
112    return normcase(path)
class RelativePath:
46class RelativePath:
47    def __init__(self, base_path: str):
48        self.base_path: str = base_path
49
50    def __call__(self, relative_path: str):
51        return normpath(os.path.join(self.base_path, relative_path))
RelativePath(base_path: str)
47    def __init__(self, base_path: str):
48        self.base_path: str = base_path
base_path: str
def relative_to_src( depth: typing.Union[int, NoneType] = 1) -> RelativePath:
54def relative_to_src(depth: Optional[int] = 1) -> RelativePath:
55    """
56
57    :param depth: 0 - path of this file, 1 - path of the caller's file, etc.
58    :return:
59    """
60    depth = depth or 0
61    depth += 1
62    return RelativePath(current_src_dir(depth))

:param depth: 0 - path of this file, 1 - path of the caller's file, etc. :return:

def path_relative_to_src(relative_path: str, depth: typing.Union[int, NoneType] = 1) -> str:
65def path_relative_to_src(relative_path: str, depth: Optional[int] = 1) -> str:
66    """
67
68    :param relative_path:
69    :param depth: 0 - path of this file, 1 - path of the caller's file, etc.
70    :return:
71    """
72    depth = depth or 0
73    depth += 1
74    return RelativePath(current_src_dir(depth))(relative_path)

:param relative_path: :param depth: 0 - path of this file, 1 - path of the caller's file, etc. :return:

def relative_to_cwd() -> RelativePath:
77def relative_to_cwd() -> RelativePath:
78    return RelativePath(os.getcwd())
def path_relative_to_cwd(relative_path: str) -> str:
81def path_relative_to_cwd(relative_path: str) -> str:
82    return RelativePath(os.getcwd())(relative_path)
class WrongBaseDirError(builtins.Exception):
85class WrongBaseDirError(Exception):
86    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
args
def get_relative_path_part(path: str, base_dir: str) -> str:
89def get_relative_path_part(path: str, base_dir: str) -> str:
90    path = normpath(path)
91    base_dir = normpath(base_dir)
92    if not path.startswith(base_dir):
93        raise WrongBaseDirError
94    
95    relative_part = removeprefix(path, base_dir)
96    if relative_part.startswith(sep):
97        relative_part = removeprefix(relative_part, sep)
98    
99    return relative_part
sep = '/'
def canonical_path(path: str, resolve_symlinks: bool = True) -> str:
102def canonical_path(path: str, resolve_symlinks: bool = True) -> str:
103    """
104    Convert a path to its canonical, case-normalized, absolute version.
105    """
106
107    path = expanduser(path)
108    if resolve_symlinks:
109        path = realpath(path)
110    else:
111        path = abspath(path)
112    
113    return normcase(path)

Convert a path to its canonical, case-normalized, absolute version.