|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = "heider" |
| 5 | +__doc__ = r""" |
| 6 | +
|
| 7 | + Created on 8/18/22 |
| 8 | + """ |
| 9 | + |
| 10 | +__all__ = ["auto_add_readme", "recursive_add_readmes", "TouchModeEnum"] |
| 11 | + |
| 12 | +from enum import Enum |
| 13 | +from pathlib import Path |
| 14 | +from typing import Callable, Iterable, Optional |
| 15 | + |
| 16 | +from sorcery import assigned_names |
| 17 | + |
| 18 | + |
| 19 | +class TouchModeEnum(Enum): |
| 20 | + """ |
| 21 | + The touch mode enum |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + breadcrumb, empty, parent_name = assigned_names() |
| 26 | + |
| 27 | + |
| 28 | +def auto_add_readme( |
| 29 | + path: Path, |
| 30 | + *, |
| 31 | + touch_mode: TouchModeEnum = TouchModeEnum.breadcrumb, |
| 32 | + readme_name: str = "README.md", |
| 33 | + root_path: Optional[Path] = Path.cwd(), |
| 34 | + prefix="# ", |
| 35 | +) -> None: |
| 36 | + """ |
| 37 | + Add a readme to the root_path if it does not exist |
| 38 | +
|
| 39 | + :param path: |
| 40 | + :param touch_mode: |
| 41 | + :param readme_name: |
| 42 | + :param root_path: |
| 43 | + :param prefix: |
| 44 | + :return: |
| 45 | +
|
| 46 | + """ |
| 47 | + path = Path(path) |
| 48 | + readme_file = path / readme_name |
| 49 | + if not readme_file.exists(): |
| 50 | + readme_file.touch() |
| 51 | + if touch_mode == TouchModeEnum.breadcrumb: |
| 52 | + assert root_path is not None |
| 53 | + readme_file.write_text(prefix + str(path.relative_to(root_path))) |
| 54 | + elif touch_mode == TouchModeEnum.empty: |
| 55 | + readme_file.write_text("") |
| 56 | + elif touch_mode == TouchModeEnum.parent_name: |
| 57 | + readme_file.write_text(prefix + str(readme_file.parent.name)) |
| 58 | + else: |
| 59 | + raise ValueError(f"Unknown touch mode {touch_mode}") |
| 60 | + |
| 61 | + |
| 62 | +def is_python_module(path: Path) -> bool: |
| 63 | + """ |
| 64 | + Check if path is a python module |
| 65 | + """ |
| 66 | + return path.is_file() and path.suffix == ".py" |
| 67 | + |
| 68 | + |
| 69 | +def is_python_package(path: Path) -> bool: |
| 70 | + """ |
| 71 | + Check if path is a python package |
| 72 | + """ |
| 73 | + return path.is_dir() and (path / "__init__.py").exists() |
| 74 | + |
| 75 | + |
| 76 | +def negate(f: Callable) -> Callable: |
| 77 | + """ |
| 78 | + Negate a function |
| 79 | + """ |
| 80 | + return lambda *args, **kwargs: not f(*args, **kwargs) |
| 81 | + |
| 82 | + |
| 83 | +def recursive_add_readmes( |
| 84 | + path: Path, |
| 85 | + exclusion_filter: Optional[Iterable[Callable]] = (negate(is_python_package),), |
| 86 | + *, |
| 87 | + touch_mode: TouchModeEnum = TouchModeEnum.breadcrumb, |
| 88 | + readme_name: str = "README.md", |
| 89 | + root_path: Optional[Path] = None, |
| 90 | +) -> None: |
| 91 | + """ |
| 92 | + recursively add readmes to all children spanning from root_path |
| 93 | +
|
| 94 | + """ |
| 95 | + path = Path(path) |
| 96 | + if root_path is None: |
| 97 | + root_path = path.parent |
| 98 | + |
| 99 | + auto_add_readme( |
| 100 | + path, touch_mode=touch_mode, readme_name=readme_name, root_path=root_path |
| 101 | + ) |
| 102 | + |
| 103 | + for child in path.iterdir(): |
| 104 | + if exclusion_filter is None or not any(flt(child) for flt in exclusion_filter): |
| 105 | + if child.is_dir(): |
| 106 | + recursive_add_readmes( |
| 107 | + child, |
| 108 | + exclusion_filter, |
| 109 | + touch_mode=touch_mode, |
| 110 | + readme_name=readme_name, |
| 111 | + root_path=root_path, |
| 112 | + ) |
| 113 | + elif child.is_file(): |
| 114 | + pass |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + recursive_add_readmes("exclude") |
0 commit comments