generated from softwareone-platform/swo-extension-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-16846: implement fake option #21
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
Merged
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from mpt_tool.managers.file_migration import FileMigrationManager | ||
| from mpt_tool.managers.file_state import FileStateManager | ||
|
|
||
| __all__ = ["FileMigrationManager", "FileStateManager"] |
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,15 @@ | ||
| import json | ||
| from typing import Any, override | ||
|
|
||
| from mpt_tool.models import Migration | ||
|
|
||
|
|
||
| class StateJSONEncoder(json.JSONEncoder): | ||
| """JSON encoder for migration states.""" | ||
|
|
||
| @override | ||
| def default(self, obj: object) -> Any: # noqa: WPS110 | ||
| if isinstance(obj, Migration): | ||
| return obj.to_dict() | ||
|
|
||
| return super().default(obj) |
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,25 @@ | ||
| from mpt_tool.errors import BaseError | ||
|
|
||
|
|
||
| class ManagerError(BaseError): | ||
| """Base error for all manager errors.""" | ||
|
|
||
|
|
||
| class CreateMigrationError(ManagerError): | ||
| """Error creating the migration file.""" | ||
|
|
||
|
|
||
| class InvalidStateError(ManagerError): | ||
| """Error loading invalid state.""" | ||
|
|
||
|
|
||
| class LoadMigrationError(ManagerError): | ||
| """Error loading migrations.""" | ||
|
|
||
|
|
||
| class MigrationFolderError(ManagerError): | ||
| """Error accessing migrations folder.""" | ||
|
|
||
|
|
||
| class StateNotFoundError(ManagerError): | ||
| """Error getting state from state file.""" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from mpt_tool.constants import MIGRATION_STATE_FILE | ||
| from mpt_tool.enums import MigrationTypeEnum | ||
| from mpt_tool.managers.encoders import StateJSONEncoder | ||
| from mpt_tool.managers.errors import InvalidStateError, StateNotFoundError | ||
| from mpt_tool.models import Migration | ||
|
|
||
|
|
||
| class FileStateManager: | ||
| """Manages migration states.""" | ||
|
|
||
| _state_path: Path = Path(MIGRATION_STATE_FILE) | ||
|
|
||
| @classmethod | ||
| def load(cls) -> dict[str, Migration]: | ||
| """Load migration states from the state file.""" | ||
| if not cls._state_path.exists(): | ||
| return {} | ||
|
|
||
| try: | ||
| state_data = json.loads(cls._state_path.read_text(encoding="utf-8")) | ||
| except json.JSONDecodeError as error: | ||
| raise InvalidStateError(f"Invalid state file: {error!s}") from error | ||
|
|
||
| return {key: Migration.from_dict(mig_data) for key, mig_data in state_data.items()} | ||
|
|
||
| @classmethod | ||
| def get_by_id(cls, migration_id: str) -> Migration: | ||
| """Get a migration state by its ID.""" | ||
| state_data = cls.load() | ||
| try: | ||
| state = state_data[migration_id] | ||
| except KeyError: | ||
| raise StateNotFoundError("State not found") from None | ||
|
|
||
| return state | ||
|
|
||
| @classmethod | ||
| def new(cls, migration_id: str, migration_type: MigrationTypeEnum, order_id: int) -> Migration: | ||
| """Create a new migration state.""" | ||
| state_data = cls.load() | ||
| new_state = Migration( | ||
| migration_id=migration_id, | ||
| order_id=order_id, | ||
| type=migration_type, | ||
| ) | ||
| state_data[migration_id] = new_state | ||
| cls.save(state_data) | ||
| return new_state | ||
|
|
||
| @classmethod | ||
| def save(cls, state_data: dict[str, Migration]) -> None: | ||
| """Save migration states to the state file.""" | ||
| cls._state_path.write_text( | ||
| json.dumps(state_data, indent=2, cls=StateJSONEncoder), encoding="utf-8" | ||
| ) | ||
|
|
||
| @classmethod | ||
| def save_state(cls, state: Migration) -> None: | ||
| """Save a migration state to the state file.""" | ||
| state_data = cls.load() | ||
| state_data[state.migration_id] = state | ||
| cls.save(state_data) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.