|
| 1 | +"""Core utils used in OSEkit backend.""" |
| 2 | + |
1 | 3 | from __future__ import annotations |
2 | 4 |
|
3 | | -import json |
4 | 5 | import os |
5 | | -import shutil |
6 | 6 | import time |
7 | 7 | from bisect import bisect |
8 | | -from importlib.resources import as_file |
9 | 8 | from importlib.util import find_spec |
10 | | -from pathlib import Path |
11 | | -from typing import NamedTuple |
12 | | - |
13 | | -try: |
14 | | - import tomllib |
15 | | -except ModuleNotFoundError: |
16 | | - import tomli as tomllib |
17 | | - |
| 9 | +from typing import TYPE_CHECKING |
18 | 10 |
|
19 | 11 | from osekit.config import global_logging_context as glc |
20 | | -from osekit.config import print_logger |
21 | | - |
22 | | -_is_grp_supported = bool(find_spec("grp")) |
23 | | - |
24 | | - |
25 | | -@glc.set_logger(print_logger) |
26 | | -def display_folder_storage_info(dir_path: str) -> None: |
27 | | - """Print a detail of the current disk usage.""" |
28 | | - usage = shutil.disk_usage(dir_path) |
29 | | - |
30 | | - def str_usage(key: str, value: int) -> str: |
31 | | - return f"{f'{key} storage space:':<30}{f'{round(value / (1024**4), 1)} TB':>10}" |
32 | 12 |
|
33 | | - total = str_usage("Total", usage.total) |
34 | | - used = str_usage("Used", usage.used) |
35 | | - free = str_usage("Available", usage.free) |
36 | | - glc.logger.info("%s\n%s\n%s\n%s", total, used, f"{'-' * 30:^40}", free) |
| 13 | +if TYPE_CHECKING: |
| 14 | + from pathlib import Path |
37 | 15 |
|
38 | | - |
39 | | -def read_config(raw_config: str | dict | Path) -> NamedTuple: |
40 | | - """Read the given configuration file or dict and converts it to a namedtuple. Only TOML and JSON formats are accepted for now. |
41 | | -
|
42 | | - Parameter |
43 | | - --------- |
44 | | - raw_config : `str` or `Path` or `dict` |
45 | | - The path of the configuration file, or the dict object containing the configuration. |
46 | | -
|
47 | | - Returns |
48 | | - ------- |
49 | | - config : `namedtuple` |
50 | | - The configuration as a `namedtuple` object. |
51 | | -
|
52 | | - Raises |
53 | | - ------ |
54 | | - FileNotFoundError |
55 | | - Raised if the raw_config is a string that does not correspond to a valid path, or the raw_config file is not in TOML, JSON or YAML formats. |
56 | | - TypeError |
57 | | - Raised if the raw_config is anything else than a string, a PurePath or a dict. |
58 | | - NotImplementedError |
59 | | - Raised if the raw_config file is in YAML format |
60 | | -
|
61 | | - """ |
62 | | - match raw_config: |
63 | | - case Path(): |
64 | | - with as_file(raw_config) as input_config: |
65 | | - raw_config = input_config |
66 | | - |
67 | | - case str(): |
68 | | - if not Path(raw_config).is_file: |
69 | | - raise FileNotFoundError( |
70 | | - f"The configuration file {raw_config} does not exist.", |
71 | | - ) |
72 | | - |
73 | | - case dict(): |
74 | | - pass |
75 | | - case _: |
76 | | - raise TypeError( |
77 | | - "The raw_config must be either of type str, dict or Traversable.", |
78 | | - ) |
79 | | - |
80 | | - if not isinstance(raw_config, dict): |
81 | | - with open(raw_config, "rb") as input_config: |
82 | | - match Path(raw_config).suffix: |
83 | | - case ".toml": |
84 | | - raw_config = tomllib.load(input_config) |
85 | | - case ".json": |
86 | | - raw_config = json.load(input_config) |
87 | | - case ".yaml": |
88 | | - raise NotImplementedError( |
89 | | - "YAML support will eventually get there (unfortunately)", |
90 | | - ) |
91 | | - case _: |
92 | | - raise FileNotFoundError( |
93 | | - f"The provided configuration file extension ({Path(raw_config).suffix} is not a valid extension. Please use .toml or .json files.", |
94 | | - ) |
95 | | - |
96 | | - return raw_config |
| 16 | +_is_grp_supported = bool(find_spec("grp")) |
97 | 17 |
|
98 | 18 |
|
99 | 19 | def chmod_if_needed(path: Path, mode: int) -> None: |
|
0 commit comments