mcruft 0.1.2__tar.gz

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.
Files changed (40) hide show
  1. mcruft-0.1.2/.gitignore +230 -0
  2. mcruft-0.1.2/PKG-INFO +27 -0
  3. mcruft-0.1.2/pyproject.toml +47 -0
  4. mcruft-0.1.2/src/mcruft/__init__.py +80 -0
  5. mcruft-0.1.2/src/mcruft/__version__.py +24 -0
  6. mcruft-0.1.2/src/mcruft/editing.py +145 -0
  7. mcruft-0.1.2/src/mcruft/engines/__init__.py +1 -0
  8. mcruft-0.1.2/src/mcruft/engines/base.py +49 -0
  9. mcruft-0.1.2/src/mcruft/engines/cli.py +140 -0
  10. mcruft-0.1.2/src/mcruft/engines/lib.py +166 -0
  11. mcruft-0.1.2/src/mcruft/engines/libs_pdfium.py +55 -0
  12. mcruft-0.1.2/src/mcruft/engines/libs_psd.py +36 -0
  13. mcruft-0.1.2/src/mcruft/engines/libs_resvg.py +46 -0
  14. mcruft-0.1.2/src/mcruft/engines/registry.py +64 -0
  15. mcruft-0.1.2/src/mcruft/mcp.py +72 -0
  16. mcruft-0.1.2/src/mcruft/model.py +173 -0
  17. mcruft-0.1.2/src/mcruft/ops.py +136 -0
  18. mcruft-0.1.2/src/mcruft/package.py +174 -0
  19. mcruft-0.1.2/src/mcruft/page_ops.py +143 -0
  20. mcruft-0.1.2/src/mcruft/report.py +48 -0
  21. mcruft-0.1.2/tests/conftest.py +25 -0
  22. mcruft-0.1.2/tests/fakes.py +29 -0
  23. mcruft-0.1.2/tests/test_adapter_fixes.py +23 -0
  24. mcruft-0.1.2/tests/test_bundle.py +79 -0
  25. mcruft-0.1.2/tests/test_convert_pages.py +60 -0
  26. mcruft-0.1.2/tests/test_editing.py +85 -0
  27. mcruft-0.1.2/tests/test_editing_regressions.py +107 -0
  28. mcruft-0.1.2/tests/test_engines.py +119 -0
  29. mcruft-0.1.2/tests/test_lib.py +81 -0
  30. mcruft-0.1.2/tests/test_libs.py +133 -0
  31. mcruft-0.1.2/tests/test_mcp.py +100 -0
  32. mcruft-0.1.2/tests/test_metadata.py +18 -0
  33. mcruft-0.1.2/tests/test_ops.py +185 -0
  34. mcruft-0.1.2/tests/test_ops_cli.py +134 -0
  35. mcruft-0.1.2/tests/test_package.py +125 -0
  36. mcruft-0.1.2/tests/test_page_fallbacks.py +116 -0
  37. mcruft-0.1.2/tests/test_page_owner.py +28 -0
  38. mcruft-0.1.2/tests/test_raster_policy.py +31 -0
  39. mcruft-0.1.2/tests/test_release_tooling.py +67 -0
  40. mcruft-0.1.2/tests/test_safety.py +90 -0
@@ -0,0 +1,230 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ *.lcov
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ # Pipfile.lock
97
+
98
+ # uv
99
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # uv.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ # poetry.lock
110
+ # poetry.toml
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
115
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
116
+ # pdm.lock
117
+ # pdm.toml
118
+ .pdm-python
119
+ .pdm-build/
120
+
121
+ # pixi
122
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
123
+ # pixi.lock
124
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
125
+ # in the .venv directory. It is recommended not to include this directory in version control.
126
+ .pixi/*
127
+ !.pixi/config.toml
128
+
129
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130
+ __pypackages__/
131
+
132
+ # Celery stuff
133
+ celerybeat-schedule*
134
+ celerybeat.pid
135
+
136
+ # Redis
137
+ *.rdb
138
+ *.aof
139
+ *.pid
140
+
141
+ # RabbitMQ
142
+ mnesia/
143
+ rabbitmq/
144
+ rabbitmq-data/
145
+
146
+ # ActiveMQ
147
+ activemq-data/
148
+
149
+ # SageMath parsed files
150
+ *.sage.py
151
+
152
+ # Environments
153
+ .env
154
+ .envrc
155
+ .venv
156
+ env/
157
+ venv/
158
+ ENV/
159
+ env.bak/
160
+ venv.bak/
161
+
162
+ # Spyder project settings
163
+ .spyderproject
164
+ .spyproject
165
+
166
+ # Rope project settings
167
+ .ropeproject
168
+
169
+ # mkdocs/Zensical documentation
170
+ /site
171
+
172
+ # mypy
173
+ .mypy_cache/
174
+ .dmypy.json
175
+ dmypy.json
176
+
177
+ # Pyre type checker
178
+ .pyre/
179
+
180
+ # pytype static type analyzer
181
+ .pytype/
182
+
183
+ # Cython debug symbols
184
+ cython_debug/
185
+
186
+ # PyCharm
187
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
188
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
189
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
190
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
191
+ # .idea/
192
+
193
+ # Abstra
194
+ # Abstra is an AI-powered process automation framework.
195
+ # Ignore directories containing user credentials, local state, and settings.
196
+ # Learn more at https://abstra.io/docs
197
+ .abstra/
198
+
199
+ # Visual Studio Code
200
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
201
+ # can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
202
+ # and can be added to the global gitignore or merged into this file. However, if you prefer, you
203
+ # could uncomment the following to ignore the entire vscode folder
204
+ # .vscode/
205
+ # Temporary file for partial code execution
206
+ tempCodeRunnerFile.py
207
+
208
+ # Ruff stuff:
209
+ .ruff_cache/
210
+
211
+ # PyPI configuration file
212
+ .pypirc
213
+
214
+ # Marimo
215
+ marimo/_static/
216
+ marimo/_lsp/
217
+ __marimo__/
218
+
219
+ # Streamlit
220
+ .streamlit/secrets.toml
221
+
222
+ # OMC runtime state
223
+ .omc/
224
+ .superpowers/
225
+
226
+ # Generated by hatch-vcs; keep the release helper under lib/.
227
+ packages/*/src/*/__version__.py
228
+ !/lib/
229
+ /lib/*
230
+ !/lib/guard.sh
mcruft-0.1.2/PKG-INFO ADDED
@@ -0,0 +1,27 @@
1
+ Metadata-Version: 2.5
2
+ Name: mcruft
3
+ Version: 0.1.2
4
+ Summary: Unified document API for paged media
5
+ License-Expression: Apache-2.0
6
+ Requires-Python: >=3.12
7
+ Requires-Dist: pillow
8
+ Requires-Dist: pypdf
9
+ Requires-Dist: python-pptx
10
+ Provides-Extra: engines
11
+ Requires-Dist: mcruft-deckcraft; extra == 'engines'
12
+ Requires-Dist: mcruft-designcraft; extra == 'engines'
13
+ Requires-Dist: mcruft-pdfcraft; extra == 'engines'
14
+ Requires-Dist: mcruft-photocraft; extra == 'engines'
15
+ Requires-Dist: mcruft-vectorcraft; extra == 'engines'
16
+ Provides-Extra: libs
17
+ Requires-Dist: psd-tools; extra == 'libs'
18
+ Requires-Dist: pypdfium2; extra == 'libs'
19
+ Requires-Dist: resvg-py; extra == 'libs'
20
+ Provides-Extra: mcp
21
+ Requires-Dist: mcp; extra == 'mcp'
22
+ Provides-Extra: pdf
23
+ Requires-Dist: pypdfium2; extra == 'pdf'
24
+ Provides-Extra: psd
25
+ Requires-Dist: psd-tools; extra == 'psd'
26
+ Provides-Extra: svg
27
+ Requires-Dist: resvg-py; extra == 'svg'
@@ -0,0 +1,47 @@
1
+ # this_file: packages/mcruft/pyproject.toml
2
+ [project]
3
+ license = "Apache-2.0"
4
+ name = "mcruft"
5
+ description = "Unified document API for paged media"
6
+ dynamic = ["version"]
7
+ requires-python = ">=3.12"
8
+ dependencies = ["pypdf", "python-pptx", "Pillow"]
9
+
10
+ [project.optional-dependencies]
11
+ mcp = ["mcp"]
12
+ pdf = ["pypdfium2"]
13
+ svg = ["resvg-py"]
14
+ psd = ["psd-tools"]
15
+ libs = ["pypdfium2", "resvg-py", "psd-tools"]
16
+ engines = [
17
+ "mcruft-vectorcraft", "mcruft-deckcraft", "mcruft-photocraft",
18
+ "mcruft-pdfcraft", "mcruft-designcraft",
19
+ ]
20
+
21
+ [tool.uv.sources]
22
+ mcruft-vectorcraft = { workspace = true }
23
+ mcruft-deckcraft = { workspace = true }
24
+ mcruft-photocraft = { workspace = true }
25
+ mcruft-pdfcraft = { workspace = true }
26
+ mcruft-designcraft = { workspace = true }
27
+
28
+ [project.entry-points."mcruft.engines"]
29
+ lib = "mcruft.engines.lib:engine"
30
+
31
+ [build-system]
32
+ requires = ["hatchling", "hatch-vcs"]
33
+ build-backend = "hatchling.build"
34
+
35
+ [tool.hatch.version]
36
+ source = "vcs"
37
+ fallback-version = "0.0.0"
38
+
39
+ [tool.hatch.version.raw-options]
40
+ root = "../.."
41
+
42
+ [tool.hatch.build.hooks.vcs]
43
+ version-file = "src/mcruft/__version__.py"
44
+
45
+ [tool.uv]
46
+ # Rebuild editable installs when git state changes, so hatch-vcs metadata stays current.
47
+ cache-keys = [{ file = "pyproject.toml" }, { git = { commit = true, tags = true } }]
@@ -0,0 +1,80 @@
1
+ # this_file: packages/mcruft/src/mcruft/__init__.py
2
+ from pathlib import Path
3
+
4
+ from .engines.base import Capability, Engine, EngineInfo
5
+ from .engines.registry import engines
6
+ from .model import Asset, Document, Frame, Media, Page, merge
7
+ from .ops import convert
8
+ from .package import load, refresh, save
9
+ from .report import (
10
+ EngineFailed,
11
+ EngineUnavailable,
12
+ Loss,
13
+ McruftError,
14
+ PackageError,
15
+ Policy,
16
+ Report,
17
+ UnsupportedRoute,
18
+ UnsupportedSchema,
19
+ )
20
+
21
+ try:
22
+ from .__version__ import __version__ as __version__
23
+ except ImportError:
24
+ from importlib.metadata import version
25
+
26
+ __version__ = version("mcruft")
27
+
28
+ __all__ = [
29
+ "Asset",
30
+ "Capability",
31
+ "Document",
32
+ "Engine",
33
+ "EngineFailed",
34
+ "EngineInfo",
35
+ "EngineUnavailable",
36
+ "Frame",
37
+ "Loss",
38
+ "McruftError",
39
+ "Media",
40
+ "PackageError",
41
+ "Page",
42
+ "Policy",
43
+ "Report",
44
+ "UnsupportedRoute",
45
+ "UnsupportedSchema",
46
+ "__version__",
47
+ "convert",
48
+ "convert_editable",
49
+ "convert_fidelity",
50
+ "engines",
51
+ "load",
52
+ "merge",
53
+ "new",
54
+ "open",
55
+ "refresh",
56
+ "save",
57
+ ]
58
+
59
+
60
+ def new(title: str = "") -> Document:
61
+ doc = Document()
62
+ doc.meta["title"] = title
63
+ return doc
64
+
65
+
66
+ def open(path: Path | str) -> Document:
67
+ path = Path(path)
68
+ if path.is_dir():
69
+ return load(path)
70
+ doc = new(path.stem)
71
+ doc.add_file(path)
72
+ return doc
73
+
74
+
75
+ def convert_editable(src: Path | str, dst: Path | str) -> Report:
76
+ return convert(src, dst, policy="editable")
77
+
78
+
79
+ def convert_fidelity(src: Path | str, dst: Path | str) -> Report:
80
+ return convert(src, dst, policy="fidelity")
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.1.2'
22
+ __version_tuple__ = version_tuple = (0, 1, 2)
23
+
24
+ __commit_id__ = commit_id = None
@@ -0,0 +1,145 @@
1
+ # this_file: packages/mcruft/src/mcruft/editing.py
2
+ """Page edit transactions and optional PDF previews."""
3
+
4
+ import mimetypes
5
+ import os
6
+ import shutil
7
+ import tempfile
8
+ from contextlib import contextmanager
9
+ from pathlib import Path
10
+
11
+ from .engines.base import normalize_format
12
+ from .engines.registry import inspect_file
13
+ from .model import Asset, Document, Page, identifier
14
+ from .ops import convert, export, owner
15
+ from .package import digest, member
16
+ from .page_ops import joined, run
17
+ from .report import Report, UnsupportedRoute
18
+
19
+
20
+ def previews(doc: Document, root: Path) -> Report:
21
+ reports = []
22
+ pending = []
23
+ with tempfile.TemporaryDirectory(dir=root) as directory:
24
+ for n, page in enumerate(doc.pages):
25
+ previous = doc.assets.get(page.preview)
26
+ source = doc.assets[page.source["asset"]]
27
+ path = member(root, f"previews/page-{n:04d}.pdf")
28
+ valid = (
29
+ previous
30
+ and previous.role == "preview"
31
+ and previous.derived_from == source.id
32
+ and previous.path.is_file()
33
+ and digest(previous.path) == previous.sha256
34
+ and digest(source.path) == source.sha256
35
+ )
36
+ if valid and previous.path == path:
37
+ continue
38
+ staged = Path(directory) / path.name
39
+ if valid:
40
+ shutil.copyfile(previous.path, staged)
41
+ via = previous.via
42
+ else:
43
+ report = export(doc, staged, policy="editable", pages=[n], fonts="embed")
44
+ reports.append(report)
45
+ via = {
46
+ "engine": report.engine,
47
+ "engine_version": report.engine_version,
48
+ "op": "export_pages",
49
+ "policy": "editable",
50
+ }
51
+ asset = Asset(
52
+ identifier(), path, "application/pdf", digest(staged), "preview", source.id, via
53
+ )
54
+ doc.assets[asset.id] = asset
55
+ page.preview = asset.id
56
+ pending.append((staged, path))
57
+ # Stage every page before replacing paths: reordering must not overwrite another preview.
58
+ for staged, path in pending:
59
+ member(root, "previews").mkdir(parents=True, exist_ok=True)
60
+ os.replace(staged, path)
61
+ referenced = {page.preview for page in doc.pages}
62
+ doc.assets = {
63
+ key: a for key, a in doc.assets.items() if a.role != "preview" or key in referenced
64
+ }
65
+ return joined("save", None, reports, [])
66
+
67
+
68
+ @contextmanager
69
+ def edit_in(page: Page, engine: str):
70
+ """Register changed edits before inspection; inspection errors retain the old size and propagate.
71
+
72
+ A failed body leaves the document unchanged but preserves a changed working file.
73
+ """
74
+ native = {
75
+ "photocraft": {"png", "jpg", "psd", "pcraft"},
76
+ "vectorcraft": {"svg", "ai", "vectorcraft", "pdf"},
77
+ "deckcraft": {"pptx", "deckcraft"},
78
+ "pdfcraft": {"pdf"},
79
+ "designcraft": {"idml", "designcraft"},
80
+ }
81
+ formats = {
82
+ "photocraft": "psd",
83
+ "vectorcraft": "vectorcraft",
84
+ "deckcraft": "pptx",
85
+ "pdfcraft": "pdf",
86
+ "designcraft": "designcraft",
87
+ }
88
+ if engine not in formats:
89
+ raise UnsupportedRoute(f"Unknown edit engine: {engine}")
90
+ doc = owner(page)
91
+ source = doc.assets[page.source["asset"]]
92
+ fmt = normalize_format(source.path.suffix[1:])
93
+ copied = fmt in native[engine]
94
+ index = page.source["index"] if copied else 0
95
+ folder = (
96
+ member(doc.path, "derived") if doc.path else Path(tempfile.mkdtemp(prefix="mcruft-edit-"))
97
+ )
98
+ folder.mkdir(parents=True, exist_ok=True)
99
+ suffix = source.path.suffix if copied else f".{formats[engine]}"
100
+ path = folder / f"{identifier()}{suffix}"
101
+ retained = False
102
+ try:
103
+ if copied:
104
+ shutil.copyfile(source.path, path)
105
+ elif formats[engine] == "pdf":
106
+ with tempfile.TemporaryDirectory(dir=folder) as directory:
107
+ whole = Path(directory) / "converted.pdf"
108
+ convert(source.path, whole, policy="editable")
109
+ run(
110
+ "extract_pages",
111
+ whole,
112
+ path,
113
+ "editable",
114
+ pages=[page.source["index"]],
115
+ )
116
+ else:
117
+ if len(inspect_file(source.path)) != 1:
118
+ raise UnsupportedRoute(f"No single-page editable route: {fmt} -> {formats[engine]}")
119
+ convert(source.path, path, policy="editable")
120
+ before = digest(path)
121
+ try:
122
+ yield path
123
+ finally:
124
+ retained = True
125
+ after = digest(path)
126
+ retained = after != before
127
+ if retained:
128
+ asset = Asset(
129
+ identifier(),
130
+ path,
131
+ mimetypes.guess_type(path)[0] or "application/octet-stream",
132
+ after,
133
+ "derived",
134
+ page.source["asset"],
135
+ {"engine": engine, "op": "edit_in"},
136
+ )
137
+ doc.assets[asset.id] = asset
138
+ page.source = {"asset": asset.id, "index": index}
139
+ page.preview = None
140
+ page.size = inspect_file(path)[index]
141
+ finally:
142
+ if not retained:
143
+ path.unlink(missing_ok=True)
144
+ if doc.path is None:
145
+ folder.rmdir()
@@ -0,0 +1 @@
1
+ # this_file: packages/mcruft/src/mcruft/engines/__init__.py
@@ -0,0 +1,49 @@
1
+ # this_file: packages/mcruft/src/mcruft/engines/base.py
2
+ from dataclasses import dataclass
3
+ from pathlib import Path
4
+ from typing import Literal
5
+
6
+ from ..report import Policy, Report, UnsupportedRoute
7
+
8
+
9
+ def normalize_format(fmt: str) -> str:
10
+ return {"jpeg": "jpg", "tif": "tiff"}.get(fmt.lower(), fmt.lower())
11
+
12
+
13
+ @dataclass(frozen=True)
14
+ class Capability:
15
+ op: str
16
+ src: str
17
+ dst: str
18
+ policy: Policy
19
+ verified_with: str | None = None
20
+ note: str = ""
21
+
22
+
23
+ class Engine:
24
+ name: str
25
+ kind: Literal["cli", "mcp", "native", "lib"]
26
+
27
+ def available(self) -> bool:
28
+ return False
29
+
30
+ def version(self) -> str | None:
31
+ return None
32
+
33
+ def capabilities(self) -> list[Capability]:
34
+ return []
35
+
36
+ def run(self, op: str, src: Path, dst: Path, *, policy: Policy, **opts) -> Report:
37
+ raise UnsupportedRoute(f"{self.name}: {op} {policy}")
38
+
39
+ def inspect(self, path: Path) -> list[tuple[float, float]]:
40
+ raise UnsupportedRoute(f"{self.name}: cannot inspect {path.suffix}")
41
+
42
+
43
+ @dataclass
44
+ class EngineInfo:
45
+ name: str
46
+ kind: str
47
+ available: bool
48
+ version: str | None
49
+ capabilities: list[Capability]