|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# gceutils |
| 4 | + |
| 5 | +Python utilities for DRY tools, rich repr/validation helpers, object-tree iteration & more. |
| 6 | + |
| 7 | +</div> |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +The public API is kept intentionally small. Highlights: |
| 14 | + |
| 15 | +- `grepr_dataclass`, `field`: Enhanced dataclasses with validation and improved representation |
| 16 | +- `HasGreprValidate`: Protocol reflecting the effects of `grepr_dataclass` |
| 17 | +- `AbstractTreePath`: Path abstraction for nested object trees (attributes, indexes, keys) |
| 18 | +- `NotSet`, `NotSetType`: Unique sentinel useful for keyword arguments and defaults |
| 19 | +- `enforce_argument_types`: Runtime enforcement of function argument types from annotations |
| 20 | +- `enforce_type`: Recursive type checking against rich typing constructs |
| 21 | +- `DualKeyDict`: Dictionary supporting two linked key spaces with full mapping features |
| 22 | +- `GU_PathValidationError`: Validation error carrying an `AbstractTreePath` context |
| 23 | + |
| 24 | +- `read_all_files_of_zip`: Read all files from a ZIP into a name→bytes map (with robust errors) |
| 25 | +- `read_file_text`: Read text files with encoding and solid error reporting |
| 26 | +- `write_file_text`: Write text files with encoding and solid error reporting |
| 27 | +- `delete_file`: Remove a file with clear, specific exceptions |
| 28 | +- `delete_directory`: Recursively remove a directory with clear, specific exceptions |
| 29 | +- `create_zip_file`: Build a ZIP file from an in-memory name→bytes mapping |
| 30 | +- `file_exists`: Lightweight existence check for a path |
| 31 | + |
| 32 | +- `KeyReprDict`: Dict wrapper whose repr displays only keys |
| 33 | +- `grepr`: Flexible pretty-printer for dataclasses, collections, dicts, and `DualKeyDict` |
| 34 | +- `GEnum`: Enum base class with concise `Class.Member` repr |
| 35 | + |
| 36 | +- `TreeVisitor`: Recursive traversal over dataclass-based object trees with type filtering |
| 37 | + |
| 38 | +- `ValidateAttribute`: Prebuilt validators (type/range/length/formats) |
| 39 | +- `is_valid_js_data_uri`: Validate JS data URIs |
| 40 | +- `is_valid_directory_path`: Validate an existing or creatable, writable directory path |
| 41 | +- `is_valid_url`: Validate basic HTTP(S) URLs with a domain |
| 42 | + |
| 43 | +For the full, always-up-to-date list, see [features.md](features.md). |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Install |
| 48 | + |
| 49 | +Python 3.12+ is required. |
| 50 | + |
| 51 | +- From PyPI (if published): |
| 52 | + |
| 53 | +```bash |
| 54 | +pip install gceutils |
| 55 | +``` |
| 56 | + |
| 57 | +- From source (editable): |
| 58 | + |
| 59 | +```bash |
| 60 | +git clone https://github.com/GermanCodeEngineer/gceutils.git |
| 61 | +cd gceutils |
| 62 | +pip install -e . |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## Quick Examples |
| 68 | + |
| 69 | +Validate attributes using built-in validators: |
| 70 | + |
| 71 | +```python |
| 72 | +from gceutils import grepr_dataclass, ValidateAttribute as VA, AbstractTreePath, HasGreprValidate |
| 73 | +from datetime import date |
| 74 | + |
| 75 | +@grepr_dataclass() |
| 76 | +class Config(HasGreprValidate): # HasGreprValidate is optional, just helps type checkers |
| 77 | + color: str |
| 78 | + |
| 79 | + def post_validate(self, path: AbstractTreePath) -> None: |
| 80 | + # Additional validation can be done here if needed |
| 81 | + if date.today().weekday() == 1: |
| 82 | + VA.VA_HEX_COLOR(self, path, "color", condition="on mondays") |
| 83 | + |
| 84 | +cfg = Config(color="#FF0956e") # not valid |
| 85 | +cfg.validate(path=AbstractTreePath(())) # Ensures color is a str and hex color |
| 86 | +``` |
| 87 | +Output(abbreviated): |
| 88 | +``` |
| 89 | +gceutils.errors.GU_InvalidValueError: on tuesdays: color of a __main__.Config must be a valid hex color eg. '#FF0956' |
| 90 | +``` |
| 91 | + |
| 92 | +Traverse an object tree and collect specific node types: |
| 93 | + |
| 94 | +```python |
| 95 | +from gceutils import grepr_dataclass, grepr, TreeVisitor |
| 96 | + |
| 97 | +@grepr_dataclass() |
| 98 | +class Node: |
| 99 | + name: str |
| 100 | + children: list["Node"] |
| 101 | + |
| 102 | +root = Node("root", [Node("a", []), Node("b", [])]) |
| 103 | +visitor = TreeVisitor.create_new_include_only([Node]) |
| 104 | +matches = visitor.visit_tree(root) # {AbstractTreePath: Node} |
| 105 | +print("My matches:", grepr(matches)) |
| 106 | +``` |
| 107 | +Output: |
| 108 | +``` |
| 109 | +My matches: { |
| 110 | + AbstractTreePath(.children[0]): Node(name="a", children=[]), |
| 111 | + AbstractTreePath(.children[1]): Node(name="b", children=[]), |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Read all files from a ZIP and pretty-print a structure: |
| 116 | + |
| 117 | +```python |
| 118 | +from gceutils import read_all_files_of_zip, grepr |
| 119 | + |
| 120 | +contents = read_all_files_of_zip("archive.zip") # {"path/inside.txt": b"..."} |
| 121 | +print(grepr(list(contents.keys()), indent=2)) |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## Testing |
| 127 | + |
| 128 | +This repo uses `pytest`. |
| 129 | + |
| 130 | +```bash |
| 131 | +pytest |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Contributing |
| 137 | + |
| 138 | +Contribution is welcomed and encouraged. |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +GPL-3.0-or-later — see [LICENSE](LICENSE). |
| 145 | + |
0 commit comments