Source code for mypythontools.paths

"""
Module where you can configure and process paths.
"""
from pathlib import Path
import sys
import builtins

import mylogging


# Root is usually current working directory, if not, use `set_paths` function.
root = None  # Path where all project is (docs, tests...)
app = None  # Folder where python scripts are (and __init__.py)
init = None  # Path to __init__.py


[docs]def set_paths(set_root=None, set_init=None): """Parse python project application structure, add paths to sys.path and save to paths module variables. Get next paths: root - Folder where all project is (docs, tests...) app - Folder where python scripts are (and __init__.py) init - Path to __init__.py Args: set_root ((str, pathlib.Path), optional): Path to project root where tests and docs folder are. If None, then cwd (current working directory) is used. Defaults to None. set_init ((str, pathlib.Path), optional): Path to project `__init__.py`. If None, then first found `__init__.py` is used. Defaults to None. Example: >>> import mypythontools >>> mypythontools.paths.set_paths() >>> mypythontools.paths.root WindowsPath('... >>> mypythontools.paths.app WindowsPath('... """ global init global app set_root(set_root) init = find_path("__init__.py", root) if not set_init else Path(set_init) app = init.parent
[docs]def set_root(set_root=None): """Set project root path and add it to sys.path if it's not already there. Args: set_root ((str, pathlib.Path), optional): Path to project root where tests and docs folder are. If None, then cwd (current working directory) is used. Defaults to None. Note: If working from jupyter notebook, works only if in directory `tests`. """ global root root = Path(set_root) if set_root else Path.cwd() root = root.resolve() # If using jupyter notebook from tests - very specific use case if root.name == "tests" and hasattr(builtins, "__IPYTHON__"): root = root.parent if not root.as_posix() in sys.path: sys.path.insert(0, root.as_posix())
[docs]def find_path(file, folder=None, exclude=["node_modules", "build", "dist"], levels=5): """Search for file in defined folder (cwd() by default) and return it's path. Args: file (str): Name with extension e.g. "app.py". folder (str, optional): Where to search. If None, then root is used (cwd by default). Defaults to None. exclude (str, optional): List of folder names (anywhere in path) that will be ignored. Defaults to ['node_modules', 'build', 'dist']. levels (str, optional): Recursive number of analyzed folders. Defaults to 5. Returns: Path: Path of file. Raises: FileNotFoundError: If file is not found. """ folder = root if not folder else Path(folder).resolve() for lev in range(levels): glob_file_str = f"{'*/' * lev}{file}" for i in folder.glob(glob_file_str): isthatfile = True for j in exclude: if j in i.parts: isthatfile = False break if isthatfile: return i # If not returned - not found raise FileNotFoundError(mylogging.format_str(f"File `{file}` not found"))
[docs]def get_desktop_path(): """Get desktop path. Returns: Path: Return pathlib Path object. If you want string, use `.as_posix()` """ return Path.home() / "Desktop"