kellerlab-pre-commit-hooks 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kellerlab_pre_commit_hooks-0.2.1.dist-info/METADATA +102 -0
- kellerlab_pre_commit_hooks-0.2.1.dist-info/RECORD +9 -0
- kellerlab_pre_commit_hooks-0.2.1.dist-info/WHEEL +5 -0
- kellerlab_pre_commit_hooks-0.2.1.dist-info/entry_points.txt +3 -0
- kellerlab_pre_commit_hooks-0.2.1.dist-info/licenses/LICENSE +21 -0
- kellerlab_pre_commit_hooks-0.2.1.dist-info/top_level.txt +1 -0
- pre_commit_hooks/__init__.py +3 -0
- pre_commit_hooks/flatten_validate.py +135 -0
- pre_commit_hooks/optimize_images.py +88 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kellerlab-pre-commit-hooks
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Reusable pre-commit hooks for OpenSCAD projects
|
|
5
|
+
Author-email: Patrick PΓΆtz <kellervater@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/kellerlabs/pre-commit-hooks
|
|
8
|
+
Project-URL: Repository, https://github.com/kellerlabs/pre-commit-hooks
|
|
9
|
+
Project-URL: Issues, https://github.com/kellerlabs/pre-commit-hooks/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/kellerlabs/pre-commit-hooks/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: pre-commit,openscad,image-optimization,scadm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: scadm>=0.6.0
|
|
24
|
+
Requires-Dist: Pillow>=11.0.0
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest; extra == "test"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# π§ Pre-commit Hooks
|
|
30
|
+
|
|
31
|
+
Reusable [pre-commit](https://pre-commit.com/) hooks for OpenSCAD projects.
|
|
32
|
+
|
|
33
|
+
Published on PyPI as [`kellerlab-pre-commit-hooks`](https://pypi.org/project/kellerlab-pre-commit-hooks/).
|
|
34
|
+
|
|
35
|
+
## π What
|
|
36
|
+
|
|
37
|
+
Two hooks for OpenSCAD-based 3D printing projects:
|
|
38
|
+
|
|
39
|
+
| Hook | Description |
|
|
40
|
+
|------|-------------|
|
|
41
|
+
| `flatten-validate` | Runs `scadm flatten --all` and fails if flattened files have uncommitted changes |
|
|
42
|
+
| `optimize-images` | Resizes oversized JPEGs, compresses them, and strips EXIF data (privacy) |
|
|
43
|
+
|
|
44
|
+
## π§ Usage
|
|
45
|
+
|
|
46
|
+
Add to your `.pre-commit-config.yaml`:
|
|
47
|
+
|
|
48
|
+
```yaml
|
|
49
|
+
- repo: https://github.com/kellerlabs/pre-commit-hooks
|
|
50
|
+
rev: v0.1.0 # use latest tag
|
|
51
|
+
hooks:
|
|
52
|
+
- id: flatten-validate
|
|
53
|
+
- id: optimize-images
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### `flatten-validate`
|
|
57
|
+
|
|
58
|
+
Flattens OpenSCAD files via [scadm](https://pypi.org/project/scadm/) and validates that the output is committed. Reads flatten config from `scadm.json` automatically.
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
- id: flatten-validate
|
|
62
|
+
# Override trigger pattern (default: always_run)
|
|
63
|
+
always_run: false
|
|
64
|
+
files: '^(scadm\.json|models/.*\.scad)'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**Options:**
|
|
68
|
+
|
|
69
|
+
| Arg | Default | Description |
|
|
70
|
+
|-----|---------|-------------|
|
|
71
|
+
| `--flatten-dir` | Read from `scadm.json` | Override flatten output dirs (list of paths/globs) |
|
|
72
|
+
|
|
73
|
+
### `optimize-images`
|
|
74
|
+
|
|
75
|
+
Resizes JPEGs exceeding max width, compresses to target quality, and strips EXIF metadata (removes GPS coordinates from real-life photos).
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
- id: optimize-images
|
|
79
|
+
args: [--max-width=1920, --quality=85]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Options:**
|
|
83
|
+
|
|
84
|
+
| Arg | Default | Description |
|
|
85
|
+
|-----|---------|-------------|
|
|
86
|
+
| `--max-width` | `1920` | Maximum image width in pixels |
|
|
87
|
+
| `--quality` | `85` | JPEG compression quality (1-95) |
|
|
88
|
+
|
|
89
|
+
Both hooks **fail when files are modified**, printing instructions to `git add` the changes. Re-run `git commit` after staging.
|
|
90
|
+
|
|
91
|
+
## ποΈ Used In
|
|
92
|
+
|
|
93
|
+
- [kellerlabs/homeracker](https://github.com/kellerlabs/homeracker) β HomeRacker Core
|
|
94
|
+
- [kellerlabs/homeracker-exclusive](https://github.com/kellerlabs/homeracker-exclusive) β HomeRacker MakerWorld models
|
|
95
|
+
|
|
96
|
+
## π€ Contributing
|
|
97
|
+
|
|
98
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
99
|
+
|
|
100
|
+
## π License
|
|
101
|
+
|
|
102
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/licenses/LICENSE,sha256=54mVbAWTJRVwZmhlOyauv_RlF3YsmceA605ZKVUEhXI,1067
|
|
2
|
+
pre_commit_hooks/__init__.py,sha256=Ca0TTblLl6bkJjtsWn-3Am9laLcVbyF0AIRiVyt7avY,78
|
|
3
|
+
pre_commit_hooks/flatten_validate.py,sha256=RvDWX9oiFT4Kyt3KKFc1tKUU44kHP9VP0_JVIQGF3aQ,3769
|
|
4
|
+
pre_commit_hooks/optimize_images.py,sha256=3iJdzcmMFaRYaoK_1v59HmHdjupNhwbThXtjPMKyjcc,2579
|
|
5
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/METADATA,sha256=--DqsNjb1Hu3RaUYiAp6yiDaNcU44yHTa1BYBRDHAmE,3316
|
|
6
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/entry_points.txt,sha256=h0pVueZqICvvKQTt2guMZVKfAMNROVDLps8NKve3Zdc,132
|
|
8
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/top_level.txt,sha256=VYPubsyOzuMlsdcvTMQYKVCB1e-HcsV9kZ0Q7TlZuc4,17
|
|
9
|
+
kellerlab_pre_commit_hooks-0.2.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KellerLabs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pre_commit_hooks
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Pre-commit hook: flatten OpenSCAD files and validate they are committed."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import shlex
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _read_flatten_dirs():
|
|
12
|
+
"""Read flatten destination dirs from scadm.json.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
List of destination directory paths from the flatten config,
|
|
16
|
+
or empty list if scadm.json is missing or has no flatten key.
|
|
17
|
+
"""
|
|
18
|
+
scadm_json = Path("scadm.json")
|
|
19
|
+
if not scadm_json.exists():
|
|
20
|
+
print("WARNING: scadm.json not found, skipping flatten dir validation")
|
|
21
|
+
return []
|
|
22
|
+
|
|
23
|
+
with open(scadm_json, encoding="utf-8") as f:
|
|
24
|
+
config = json.load(f)
|
|
25
|
+
|
|
26
|
+
flatten_entries = config.get("flatten", [])
|
|
27
|
+
if not flatten_entries:
|
|
28
|
+
print("WARNING: No 'flatten' entries in scadm.json")
|
|
29
|
+
return []
|
|
30
|
+
|
|
31
|
+
return [entry["dest"] for entry in flatten_entries if "dest" in entry]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _git_modified(paths):
|
|
35
|
+
"""Get modified tracked files (not staged) under the given paths.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
paths: List of directory paths to check.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
List of modified file paths.
|
|
42
|
+
|
|
43
|
+
Raises:
|
|
44
|
+
SystemExit: If git diff fails.
|
|
45
|
+
"""
|
|
46
|
+
if not paths:
|
|
47
|
+
return []
|
|
48
|
+
result = subprocess.run(
|
|
49
|
+
["git", "diff", "--name-only", "--"] + paths,
|
|
50
|
+
capture_output=True,
|
|
51
|
+
text=True,
|
|
52
|
+
check=False,
|
|
53
|
+
)
|
|
54
|
+
if result.returncode != 0:
|
|
55
|
+
print(f"ERROR: git diff failed: {result.stderr.strip()}")
|
|
56
|
+
sys.exit(1)
|
|
57
|
+
return [line for line in result.stdout.strip().split("\n") if line]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _git_untracked(paths):
|
|
61
|
+
"""Get untracked files under the given paths.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
paths: List of directory paths to check.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
List of untracked file paths.
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
SystemExit: If git ls-files fails.
|
|
71
|
+
"""
|
|
72
|
+
if not paths:
|
|
73
|
+
return []
|
|
74
|
+
result = subprocess.run(
|
|
75
|
+
["git", "ls-files", "--others", "--exclude-standard", "--"] + paths,
|
|
76
|
+
capture_output=True,
|
|
77
|
+
text=True,
|
|
78
|
+
check=False,
|
|
79
|
+
)
|
|
80
|
+
if result.returncode != 0:
|
|
81
|
+
print(f"ERROR: git ls-files failed: {result.stderr.strip()}")
|
|
82
|
+
sys.exit(1)
|
|
83
|
+
return [line for line in result.stdout.strip().split("\n") if line]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def main():
|
|
87
|
+
"""Entry point for the flatten-validate pre-commit hook."""
|
|
88
|
+
parser = argparse.ArgumentParser(description="Validate flattened OpenSCAD exports are committed.")
|
|
89
|
+
parser.add_argument(
|
|
90
|
+
"--flatten-dir",
|
|
91
|
+
nargs="+",
|
|
92
|
+
default=None,
|
|
93
|
+
help="Override flatten destination dirs (default: read from scadm.json).",
|
|
94
|
+
)
|
|
95
|
+
args = parser.parse_args()
|
|
96
|
+
|
|
97
|
+
flatten_dirs = args.flatten_dir if args.flatten_dir else _read_flatten_dirs()
|
|
98
|
+
|
|
99
|
+
# Run scadm flatten
|
|
100
|
+
result = subprocess.run(["scadm", "flatten", "--all"], check=False)
|
|
101
|
+
if result.returncode != 0:
|
|
102
|
+
print("ERROR: scadm flatten --all failed")
|
|
103
|
+
return 1
|
|
104
|
+
|
|
105
|
+
if not flatten_dirs:
|
|
106
|
+
print("β No flatten directories to validate")
|
|
107
|
+
return 0
|
|
108
|
+
|
|
109
|
+
modified = _git_modified(flatten_dirs)
|
|
110
|
+
untracked = _git_untracked(flatten_dirs)
|
|
111
|
+
|
|
112
|
+
if modified or untracked:
|
|
113
|
+
print("")
|
|
114
|
+
print("ERROR: Flattened files have uncommitted/unstaged changes")
|
|
115
|
+
print("")
|
|
116
|
+
if modified:
|
|
117
|
+
print("Modified files (not staged):")
|
|
118
|
+
for f in modified:
|
|
119
|
+
print(f" {f}")
|
|
120
|
+
if untracked:
|
|
121
|
+
print("Untracked files:")
|
|
122
|
+
for f in untracked:
|
|
123
|
+
print(f" {f}")
|
|
124
|
+
dirs = " ".join(shlex.quote(d) for d in flatten_dirs)
|
|
125
|
+
print("")
|
|
126
|
+
print("Run the following to stage them:")
|
|
127
|
+
print(f" git add {dirs}")
|
|
128
|
+
return 1
|
|
129
|
+
|
|
130
|
+
print("β All flattened files are committed")
|
|
131
|
+
return 0
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
if __name__ == "__main__":
|
|
135
|
+
sys.exit(main())
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Pre-commit hook: resize and compress JPEG images."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import shlex
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from PIL import Image, ImageOps
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _optimize_file(filepath, max_width, quality):
|
|
11
|
+
"""Resize, compress, and strip EXIF from a single JPEG file.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
filepath: Path to the JPEG file.
|
|
15
|
+
max_width: Maximum allowed width in pixels.
|
|
16
|
+
quality: JPEG compression quality (1-95).
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
True if the file was modified, False otherwise.
|
|
20
|
+
"""
|
|
21
|
+
with Image.open(filepath) as img:
|
|
22
|
+
# Apply EXIF orientation before checking size
|
|
23
|
+
img = ImageOps.exif_transpose(img)
|
|
24
|
+
width, height = img.size
|
|
25
|
+
needs_resize = width > max_width
|
|
26
|
+
has_exif = bool(img.info.get("exif"))
|
|
27
|
+
|
|
28
|
+
if not needs_resize and not has_exif:
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
if needs_resize:
|
|
32
|
+
ratio = max_width / width
|
|
33
|
+
new_height = max(1, round(height * ratio))
|
|
34
|
+
new_size = (max_width, new_height)
|
|
35
|
+
img = img.resize(new_size, Image.LANCZOS)
|
|
36
|
+
|
|
37
|
+
if img.mode in ("RGBA", "P"):
|
|
38
|
+
img = img.convert("RGB")
|
|
39
|
+
img.save(filepath, "JPEG", quality=quality, optimize=True)
|
|
40
|
+
|
|
41
|
+
return True
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def main():
|
|
45
|
+
"""Entry point for the optimize-images pre-commit hook."""
|
|
46
|
+
parser = argparse.ArgumentParser(description="Resize and compress JPEG images.")
|
|
47
|
+
parser.add_argument("filenames", nargs="*", help="JPEG files to check.")
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
"--max-width",
|
|
50
|
+
type=int,
|
|
51
|
+
default=1920,
|
|
52
|
+
help="Maximum image width in pixels (default: 1920).",
|
|
53
|
+
)
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
"--quality",
|
|
56
|
+
type=int,
|
|
57
|
+
default=85,
|
|
58
|
+
help="JPEG compression quality 1-95 (default: 85).",
|
|
59
|
+
)
|
|
60
|
+
args = parser.parse_args()
|
|
61
|
+
|
|
62
|
+
modified = []
|
|
63
|
+
failed = False
|
|
64
|
+
for filepath in args.filenames:
|
|
65
|
+
try:
|
|
66
|
+
if _optimize_file(filepath, args.max_width, args.quality):
|
|
67
|
+
modified.append(filepath)
|
|
68
|
+
except Exception as exc: # pylint: disable=broad-exception-caught
|
|
69
|
+
print(f"ERROR: Failed to process {filepath}: {exc}")
|
|
70
|
+
failed = True
|
|
71
|
+
|
|
72
|
+
if modified:
|
|
73
|
+
quoted = " ".join(shlex.quote(f) for f in modified)
|
|
74
|
+
print("")
|
|
75
|
+
print("Images were resized/compressed by this hook:")
|
|
76
|
+
print("")
|
|
77
|
+
for f in modified:
|
|
78
|
+
print(f" {f}")
|
|
79
|
+
print("")
|
|
80
|
+
print("Run the following to stage the optimized files:")
|
|
81
|
+
print(f" git add {quoted}")
|
|
82
|
+
return 1
|
|
83
|
+
|
|
84
|
+
return 1 if failed else 0
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
if __name__ == "__main__":
|
|
88
|
+
sys.exit(main())
|