Repository navigation
Expand file tree
/
Copy pathpath.py
More file actions
87 lines (65 loc) · 2.58 KB
/
Copy pathpath.py
File metadata and controls
87 lines (65 loc) · 2.58 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2024-2025, QUEENS contributors.
#
# This file is part of QUEENS.
#
# QUEENS is free software: you can redistribute it and/or modify it under the terms of the GNU
# Lesser General Public License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version. QUEENS is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You
# should have received a copy of the GNU Lesser General Public License along with QUEENS. If not,
# see <https://www.gnu.org/licenses/>.
#
"""Path utilities for QUEENS."""
from pathlib import Path
from typing import Sequence
import queens
PATH_TO_QUEENS_SOURCE = Path(queens.__file__).parent
# this path to root is used during runtime and thus needs to be shipped with a build even though it
# is not useful in that scenario.
PATH_TO_ROOT = Path(__file__).parents[3]
def relative_path_from_queens_source(relative_path: str) -> Path:
"""Create relative path from *src/queens/*.
For example, to create *src/queens/folder/file.A*, call
*relative_path_from_queens_source("folder/file.A")* .
Args:
relative_path: Path starting from *src/queens/*
Returns:
Absolute path to the file
"""
full_path = PATH_TO_QUEENS_SOURCE / relative_path
return full_path
def create_folder_if_not_existent(path: Path | str) -> Path:
"""Create folder if not existent.
Args:
path: Path to be created
Returns:
Path object
"""
path_obj = Path(path)
path_obj.mkdir(parents=True, exist_ok=True)
return path_obj
def check_if_path_exists(path: Path, error_message: str = "") -> bool:
"""Check if a path exists.
Args:
path: Path to be checked
error_message: If an additional message is desired
Returns:
`True` if the path exists, `False` otherwise.
Raises:
FileNotFoundError: If the path does not exist.
"""
path_exists = Path(path).exists()
if not path_exists:
raise FileNotFoundError(error_message + f"\nPath {path} does not exist.")
return path_exists
def is_empty(paths: str | Path | Sequence) -> bool:
"""Check whether paths is empty.
Args:
paths: (List of) path-like objects
"""
if not isinstance(paths, (str, Path, Sequence)):
raise TypeError("paths must be a string, a Path object, or a sequence.")
return not bool(paths)