[MouseFox logo]
The MouseFox Project
Join The Community Discord Server
[Discord logo]
Edit on GitHub

mousefox.util

Utitilies subpackage.

 1"""Utitilies subpackage."""
 2
 3from pathlib import Path
 4import os
 5import platform
 6import tomli
 7
 8
 9def toml_load(file: os.PathLike) -> dict:
10    """Load TOML file as a dictionary."""
11    return tomli.loads(file_load(file))
12
13
14def file_load(file: os.PathLike) -> str:
15    """Loads *file* and returns the contents as a string."""
16    with open(file, "r") as f:
17        d = f.read()
18    return d
19
20
21def file_dump(file: os.PathLike, d: str, clear: bool = True):
22    """Saves the string *d* to *file*.
23
24    Will overwrite the file if *clear* is True, otherwise will append to it.
25    """
26    with open(file, "w" if clear else "a", encoding="utf-8") as f:
27        f.write(d)
28
29
30def get_appdata_dir() -> Path:
31    r"""Return path to the user's app data folder.
32
33    - Windows: `~\\AppData\\Local\\mousefox`
34    - Mac OS: `~/Library/Local/mousefox`
35    - Linux: `~/.local/share/mousefox`
36    """
37    if platform.system() == "Windows":
38        parts = ["AppData", "Local"]
39    elif platform.system() == "Darwin":
40        parts = ["Library"]
41    else:
42        parts = [".local", "share"]
43    path = Path.home().joinpath(*parts) / "mousefox"
44    path.mkdir(parents=True, exist_ok=True)
45    return path
def toml_load(file: os.PathLike) -> dict:
10def toml_load(file: os.PathLike) -> dict:
11    """Load TOML file as a dictionary."""
12    return tomli.loads(file_load(file))

Load TOML file as a dictionary.

def file_load(file: os.PathLike) -> str:
15def file_load(file: os.PathLike) -> str:
16    """Loads *file* and returns the contents as a string."""
17    with open(file, "r") as f:
18        d = f.read()
19    return d

Loads file and returns the contents as a string.

def file_dump(file: os.PathLike, d: str, clear: bool = True):
22def file_dump(file: os.PathLike, d: str, clear: bool = True):
23    """Saves the string *d* to *file*.
24
25    Will overwrite the file if *clear* is True, otherwise will append to it.
26    """
27    with open(file, "w" if clear else "a", encoding="utf-8") as f:
28        f.write(d)

Saves the string d to file.

Will overwrite the file if clear is True, otherwise will append to it.

def get_appdata_dir() -> pathlib.Path:
31def get_appdata_dir() -> Path:
32    r"""Return path to the user's app data folder.
33
34    - Windows: `~\\AppData\\Local\\mousefox`
35    - Mac OS: `~/Library/Local/mousefox`
36    - Linux: `~/.local/share/mousefox`
37    """
38    if platform.system() == "Windows":
39        parts = ["AppData", "Local"]
40    elif platform.system() == "Darwin":
41        parts = ["Library"]
42    else:
43        parts = [".local", "share"]
44    path = Path.home().joinpath(*parts) / "mousefox"
45    path.mkdir(parents=True, exist_ok=True)
46    return path

Return path to the user's app data folder.

  • Windows: ~\\AppData\\Local\\mousefox
  • Mac OS: ~/Library/Local/mousefox
  • Linux: ~/.local/share/mousefox