File: files.py

package info (click to toggle)
python-sphinx-chango 0.5.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,776 kB
  • sloc: python: 4,909; javascript: 74; makefile: 23
file content (31 lines) | stat: -rw-r--r-- 779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#  SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
#  SPDX-License-Identifier: MIT
import os
from contextlib import contextmanager
from pathlib import Path

from chango._utils.types import PathLike

PROJECT_ROOT_PATH = Path(__file__).parent.parent.parent.resolve()
TEST_DATA_PATH = PROJECT_ROOT_PATH / "tests" / "data"


def data_path(filename: PathLike) -> Path:
    return TEST_DATA_PATH / filename


def path_to_python_string(path: Path, output_type: type[str] | type[Path]) -> str:
    if output_type is str:
        return f"'{path.as_posix()}'"
    return f"Path(r'{path}')"


@contextmanager
def temporary_chdir(path: Path):
    current_dir = Path.cwd()
    try:
        os.chdir(path)
        yield
    finally:
        os.chdir(current_dir)