Edit on GitHub

sysmon_pytk.file_utils

File utility functions.

 1# SPDX-FileCopyrightText: © 2024 Stacey Adams <stacey.belle.rose@gmail.com>
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5File utility functions.
 6"""
 7
 8import inspect
 9import sys
10from distutils.sysconfig import get_python_lib  # pylint: disable=W4901
11from pathlib import Path
12
13import platformdirs
14
15SETTINGS_FILE = "sysmon.ini"
16"""The settings file used by the application."""
17
18
19def get_full_path(relative_path: str) -> str:
20    """
21    Get the full path of a file, based on its relative path to this project.
22    """
23    std_lib = Path(get_python_lib())
24    if Path.is_file(std_lib / __package__):
25        base_dir = (std_lib / __package__).parent
26    else:
27        base_dir = Path(__file__).parent
28    return f"{base_dir / relative_path}"
29
30
31def settings_path() -> str:
32    """
33    Get the full path for the settings file.
34    """
35    package = __package__ if __package__ else Path(__file__).parts[-2]
36    base_dir = platformdirs.user_config_path(package)
37    if not base_dir.exists():
38        base_dir.mkdir(parents=True)
39    return f"{base_dir / SETTINGS_FILE}"
40
41
42def get_main_script() -> str:
43    """
44    Get the name of the top level running script.
45    """
46    for frame in reversed(inspect.stack()):
47        if frame.filename.startswith(sys.exec_prefix):
48            continue
49        if frame.filename.startswith(sys.base_exec_prefix):
50            continue
51        return Path(frame.filename).name
52    return ""
SETTINGS_FILE = 'sysmon.ini'

The settings file used by the application.

def get_full_path(relative_path: str) -> str:
20def get_full_path(relative_path: str) -> str:
21    """
22    Get the full path of a file, based on its relative path to this project.
23    """
24    std_lib = Path(get_python_lib())
25    if Path.is_file(std_lib / __package__):
26        base_dir = (std_lib / __package__).parent
27    else:
28        base_dir = Path(__file__).parent
29    return f"{base_dir / relative_path}"

Get the full path of a file, based on its relative path to this project.

def settings_path() -> str:
32def settings_path() -> str:
33    """
34    Get the full path for the settings file.
35    """
36    package = __package__ if __package__ else Path(__file__).parts[-2]
37    base_dir = platformdirs.user_config_path(package)
38    if not base_dir.exists():
39        base_dir.mkdir(parents=True)
40    return f"{base_dir / SETTINGS_FILE}"

Get the full path for the settings file.

def get_main_script() -> str:
43def get_main_script() -> str:
44    """
45    Get the name of the top level running script.
46    """
47    for frame in reversed(inspect.stack()):
48        if frame.filename.startswith(sys.exec_prefix):
49            continue
50        if frame.filename.startswith(sys.base_exec_prefix):
51            continue
52        return Path(frame.filename).name
53    return ""

Get the name of the top level running script.