-
Notifications
You must be signed in to change notification settings - Fork 5
feat(pixel-perfect): expose standalone native tool API #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huyanxius
wants to merge
17
commits into
1024XEngineer:main
Choose a base branch
from
huyanxius:feat/492-pixel-perfect-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,750
−11
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
64ac460
feat(pixel-perfect): define tool contracts
huyanxius c80dcd3
feat(pixel-perfect): add bounded native adapters
huyanxius 13aa8a0
feat(pixel-perfect): compose local grid tools
huyanxius 313a5e8
feat(pixel-perfect): expose standalone file API
huyanxius 02fb019
build(pixel-perfect): declare image inspection dependency
huyanxius 57eb318
build(pixel-perfect): enforce pipeline isolation
huyanxius a30a0aa
build(pixel-perfect): package native tool binaries
huyanxius 38d1185
test(pixel-perfect): cover standalone tool boundaries
huyanxius 9c01648
docs(pixel-perfect): publish file API contract
huyanxius da07ddd
refactor(pixel-perfect): call the native extension directly
huyanxius 2182cda
test(pixel-perfect): cover the PyO3 adapter contract
huyanxius 56b825c
build(pixel-perfect): define the PyO3 extension package
huyanxius be6e23c
feat(pixel-perfect): expose Rust algorithms through PyO3
huyanxius ef58d29
test(pixel-perfect): verify the native binding contract
huyanxius 2a6cf23
docs(pixel-perfect): document native binding usage
huyanxius 94e955c
chore(pixel-perfect): ignore native build output
huyanxius cbbae0b
build(pixel-perfect): install the native wheel in containers
huyanxius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ** | ||
| !backend/ | ||
| !backend/** | ||
| !native/ | ||
| !native/pixel-perfect/ | ||
| !native/pixel-perfect/** | ||
|
|
||
| backend/.venv/ | ||
| backend/.pytest_cache/ | ||
| backend/.ruff_cache/ | ||
| backend/**/__pycache__/ | ||
| native/pixel-perfect/**/target/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/packages/app/src/windup_app/server/pixel_perfect/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from windup_app.server.pixel_perfect.errors import ( | ||
| PixelPerfectBusyError, | ||
| PixelPerfectError, | ||
| PixelPerfectInputError, | ||
| PixelPerfectUnavailableError, | ||
| ) | ||
| from windup_app.server.pixel_perfect.factory import create_pixel_perfect_tool | ||
| from windup_app.server.pixel_perfect.model import GridDetection, PixelPerfectResult | ||
| from windup_app.server.pixel_perfect.native import ( | ||
| NativeGridDetector, | ||
| NativeGridReconstructor, | ||
| ) | ||
| from windup_app.server.pixel_perfect.service import ( | ||
| GridDetector, | ||
| GridReconstructor, | ||
| PixelPerfectTool, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "GridDetection", | ||
| "GridDetector", | ||
| "GridReconstructor", | ||
| "NativeGridDetector", | ||
| "NativeGridReconstructor", | ||
| "PixelPerfectBusyError", | ||
| "PixelPerfectError", | ||
| "PixelPerfectInputError", | ||
| "PixelPerfectResult", | ||
| "PixelPerfectTool", | ||
| "PixelPerfectUnavailableError", | ||
| "create_pixel_perfect_tool", | ||
| ] |
17 changes: 17 additions & 0 deletions
17
backend/packages/app/src/windup_app/server/pixel_perfect/errors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| """完美像素工具可以映射为稳定 API 结果的失败类型。""" | ||
|
|
||
|
|
||
| class PixelPerfectError(Exception): | ||
| """本地工具可预期失败的基类。""" | ||
|
|
||
|
|
||
| class PixelPerfectBusyError(PixelPerfectError): | ||
| pass | ||
|
|
||
|
|
||
| class PixelPerfectInputError(PixelPerfectError): | ||
| pass | ||
|
|
||
|
|
||
| class PixelPerfectUnavailableError(PixelPerfectError): | ||
| pass | ||
18 changes: 18 additions & 0 deletions
18
backend/packages/app/src/windup_app/server/pixel_perfect/factory.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """完美像素工具在应用 composition root 使用的本地装配。""" | ||
|
|
||
| import os | ||
|
|
||
| from windup_app.server.pixel_perfect.native import ( | ||
| NativeGridDetector, | ||
| NativeGridReconstructor, | ||
| ) | ||
| from windup_app.server.pixel_perfect.service import PixelPerfectTool | ||
|
|
||
|
|
||
| def create_pixel_perfect_tool() -> PixelPerfectTool: | ||
| max_concurrency = int(os.getenv("PIXEL_PERFECT_CONCURRENCY", "1")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 配置选项应该收缩到framework.conf层级下。
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return PixelPerfectTool( | ||
| detector=NativeGridDetector(), | ||
| reconstructor=NativeGridReconstructor(), | ||
| max_concurrency=max_concurrency, | ||
| ) | ||
18 changes: 18 additions & 0 deletions
18
backend/packages/app/src/windup_app/server/pixel_perfect/model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """完美像素工具在检测、重建与 API 之间传递的最小契约。""" | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class GridDetection: | ||
| cols: int | ||
| rows: int | ||
| step_x: float | ||
| step_y: float | ||
| consensus: str | ||
| confidence: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PixelPerfectResult(GridDetection): | ||
| png: bytes |
116 changes: 116 additions & 0 deletions
116
backend/packages/app/src/windup_app/server/pixel_perfect/native.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """PyO3 原生扩展与 Python 工具编排之间的适配层。""" | ||
|
|
||
| from collections.abc import Callable | ||
| from importlib import import_module | ||
| import math | ||
| from typing import Protocol | ||
|
|
||
| from windup_app.server.pixel_perfect.errors import ( | ||
| PixelPerfectInputError, | ||
| PixelPerfectUnavailableError, | ||
| ) | ||
| from windup_app.server.pixel_perfect.model import GridDetection | ||
|
|
||
|
|
||
| class PixelPerfectNativeModule(Protocol): | ||
| def detect(self, source: bytes, mode: str) -> object: ... | ||
|
|
||
| def reconstruct( | ||
| self, | ||
| source: bytes, | ||
| cols: int, | ||
| rows: int, | ||
| colors: int, | ||
| ) -> object: ... | ||
|
|
||
|
|
||
| NativeModuleLoader = Callable[[], PixelPerfectNativeModule] | ||
|
|
||
|
|
||
| def _load_installed_module() -> PixelPerfectNativeModule: | ||
| return import_module("windup_pixel_perfect_native") | ||
|
|
||
|
|
||
| def _load_native(loader: NativeModuleLoader) -> PixelPerfectNativeModule: | ||
| try: | ||
| return loader() | ||
| except (ImportError, OSError) as error: | ||
| raise PixelPerfectUnavailableError( | ||
| "本地像素原生扩展未安装或无法加载" | ||
| ) from error | ||
|
|
||
|
|
||
| class NativeGridDetector: | ||
| def __init__( | ||
| self, module_loader: NativeModuleLoader = _load_installed_module | ||
| ) -> None: | ||
| self._module_loader = module_loader | ||
|
|
||
| def detect(self, source: bytes) -> GridDetection: | ||
| native = _load_native(self._module_loader) | ||
| try: | ||
| payload = native.detect(source, "full") | ||
| except ValueError as error: | ||
| raise PixelPerfectInputError( | ||
| str(error) or "原生检测器拒绝了输入" | ||
| ) from error | ||
| except Exception as error: | ||
| raise PixelPerfectUnavailableError("原生检测器调用失败") from error | ||
|
|
||
| expected = { | ||
| "cols", | ||
| "rows", | ||
| "step_x", | ||
| "step_y", | ||
| "consensus", | ||
| "confidence", | ||
| } | ||
| if not isinstance(payload, dict) or set(payload) != expected: | ||
| raise PixelPerfectUnavailableError("检测器返回字段不符合约定") | ||
| try: | ||
| result = GridDetection(**payload) | ||
| except TypeError as error: | ||
| raise PixelPerfectUnavailableError("检测器返回类型不符合约定") from error | ||
| if ( | ||
| not isinstance(result.cols, int) | ||
| or isinstance(result.cols, bool) | ||
| or not isinstance(result.rows, int) | ||
| or isinstance(result.rows, bool) | ||
| or result.cols < 1 | ||
| or result.rows < 1 | ||
| or not isinstance(result.step_x, (int, float)) | ||
| or isinstance(result.step_x, bool) | ||
| or not isinstance(result.step_y, (int, float)) | ||
| or isinstance(result.step_y, bool) | ||
| or not math.isfinite(result.step_x) | ||
| or not math.isfinite(result.step_y) | ||
| or result.step_x <= 0 | ||
| or result.step_y <= 0 | ||
| or not isinstance(result.consensus, str) | ||
| or result.confidence not in {"high", "medium", "low"} | ||
| ): | ||
| raise PixelPerfectUnavailableError("检测器返回值超出约定") | ||
| return result | ||
|
|
||
|
|
||
| class NativeGridReconstructor: | ||
| def __init__( | ||
| self, module_loader: NativeModuleLoader = _load_installed_module | ||
| ) -> None: | ||
| self._module_loader = module_loader | ||
|
|
||
| def reconstruct(self, source: bytes, *, cols: int, rows: int, colors: int) -> bytes: | ||
| native = _load_native(self._module_loader) | ||
| try: | ||
| output = native.reconstruct(source, cols, rows, colors) | ||
| except ValueError as error: | ||
| raise PixelPerfectInputError( | ||
| str(error) or "原生重建器拒绝了输入" | ||
| ) from error | ||
| except Exception as error: | ||
| raise PixelPerfectUnavailableError("原生重建器调用失败") from error | ||
| if not isinstance(output, bytes): | ||
| raise PixelPerfectUnavailableError("重建器返回类型不符合约定") | ||
| if len(output) > 32 * 1024 * 1024: | ||
| raise PixelPerfectUnavailableError("重建器返回数据过大") | ||
| return output |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里新定义的异常应该收缩到common层级下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huyanxius