-
Notifications
You must be signed in to change notification settings - Fork 5
Implement workflow & activity registry #14
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
+392
−4
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b518f61
Implement workflow & activity registry
timl3136 52af499
linter
timl3136 c9e5720
Implement workflow & activity registry
timl3136 9f0daee
linter
timl3136 7afec2e
Merge branch 'registry' of github.com:timl3136/cadence-python-client …
timl3136 5e2d963
Respond to comments
timl3136 ee7e90d
Merge branch 'main' into registry
timl3136 6e22fb0
modify test
timl3136 f73f888
Merge branch 'registry' of github.com:timl3136/cadence-python-client …
timl3136 5018631
comments
timl3136 026ad58
unpack
timl3136 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
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,14 @@ | ||
| """ | ||
| Cadence Python Client | ||
|
|
||
| A Python framework for authoring workflows and activities for Cadence. | ||
| """ | ||
|
|
||
| # Import main client functionality | ||
| from .client import Client | ||
|
|
||
| __version__ = "0.1.0" | ||
|
|
||
| __all__ = [ | ||
| "Client", | ||
| ] |
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,174 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Workflow and Activity Registry for Cadence Python Client. | ||
|
|
||
| This module provides a registry system for managing workflows and activities, | ||
| similar to the Go client's registry.go implementation. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Callable, Dict, Optional, Unpack, TypedDict | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class RegisterWorkflowOptions(TypedDict, total=False): | ||
| """Options for registering a workflow.""" | ||
| name: Optional[str] | ||
| alias: Optional[str] | ||
|
|
||
|
|
||
| class RegisterActivityOptions(TypedDict, total=False): | ||
| """Options for registering an activity.""" | ||
| name: Optional[str] | ||
| alias: Optional[str] | ||
|
|
||
|
|
||
| class Registry: | ||
| """ | ||
| Registry for managing workflows and activities. | ||
|
|
||
| This class provides functionality to register, retrieve, and manage | ||
| workflows and activities in a Cadence application. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| """Initialize the registry.""" | ||
| self._workflows: Dict[str, Callable] = {} | ||
| self._activities: Dict[str, Callable] = {} | ||
| self._workflow_aliases: Dict[str, str] = {} # alias -> name mapping | ||
| self._activity_aliases: Dict[str, str] = {} # alias -> name mapping | ||
|
|
||
| def workflow( | ||
| self, | ||
| func: Optional[Callable] = None, | ||
| **kwargs: Unpack[RegisterWorkflowOptions] | ||
| ) -> Callable: | ||
| """ | ||
| Register a workflow function. | ||
|
|
||
| This method can be used as a decorator or called directly. | ||
|
|
||
| Args: | ||
| func: The workflow function to register | ||
| **kwargs: Options for registration (name, alias) | ||
|
|
||
| Returns: | ||
| The decorated function or the function itself | ||
|
|
||
| Raises: | ||
| KeyError: If workflow name already exists | ||
| """ | ||
| options = RegisterWorkflowOptions(**kwargs) | ||
|
|
||
| def decorator(f: Callable) -> Callable: | ||
| workflow_name = options.get('name') or f.__name__ | ||
|
|
||
| if workflow_name in self._workflows: | ||
| raise KeyError(f"Workflow '{workflow_name}' is already registered") | ||
|
|
||
| self._workflows[workflow_name] = f | ||
|
|
||
| # Register alias if provided | ||
| alias = options.get('alias') | ||
| if alias: | ||
| if alias in self._workflow_aliases: | ||
| raise KeyError(f"Workflow alias '{alias}' is already registered") | ||
| self._workflow_aliases[alias] = workflow_name | ||
|
|
||
| logger.info(f"Registered workflow '{workflow_name}'") | ||
| return f | ||
|
|
||
| if func is None: | ||
| return decorator | ||
| return decorator(func) | ||
|
|
||
| def activity( | ||
| self, | ||
| func: Optional[Callable] = None, | ||
| **kwargs: Unpack[RegisterActivityOptions] | ||
| ) -> Callable: | ||
| """ | ||
| Register an activity function. | ||
|
|
||
| This method can be used as a decorator or called directly. | ||
|
|
||
| Args: | ||
| func: The activity function to register | ||
| **kwargs: Options for registration (name, alias) | ||
|
|
||
| Returns: | ||
| The decorated function or the function itself | ||
|
|
||
| Raises: | ||
| KeyError: If activity name already exists | ||
| """ | ||
| options = RegisterActivityOptions(**kwargs) | ||
|
|
||
| def decorator(f: Callable) -> Callable: | ||
| activity_name = options.get('name') or f.__name__ | ||
|
|
||
| if activity_name in self._activities: | ||
| raise KeyError(f"Activity '{activity_name}' is already registered") | ||
|
|
||
| self._activities[activity_name] = f | ||
|
|
||
| # Register alias if provided | ||
| alias = options.get('alias') | ||
| if alias: | ||
| if alias in self._activity_aliases: | ||
| raise KeyError(f"Activity alias '{alias}' is already registered") | ||
| self._activity_aliases[alias] = activity_name | ||
|
|
||
| logger.info(f"Registered activity '{activity_name}'") | ||
| return f | ||
|
|
||
| if func is None: | ||
| return decorator | ||
| return decorator(func) | ||
|
|
||
| def get_workflow(self, name: str) -> Callable: | ||
| """ | ||
| Get a registered workflow by name. | ||
|
|
||
| Args: | ||
| name: Name or alias of the workflow | ||
|
|
||
| Returns: | ||
| The workflow function | ||
|
|
||
| Raises: | ||
| KeyError: If workflow is not found | ||
| """ | ||
| # Check if it's an alias | ||
| actual_name = self._workflow_aliases.get(name, name) | ||
|
|
||
| if actual_name not in self._workflows: | ||
| raise KeyError(f"Workflow '{name}' not found in registry") | ||
|
|
||
| return self._workflows[actual_name] | ||
|
|
||
| def get_activity(self, name: str) -> Callable: | ||
| """ | ||
| Get a registered activity by name. | ||
|
|
||
| Args: | ||
| name: Name or alias of the activity | ||
|
|
||
| Returns: | ||
| The activity function | ||
|
|
||
| Raises: | ||
| KeyError: If activity is not found | ||
| """ | ||
| # Check if it's an alias | ||
| actual_name = self._activity_aliases.get(name, name) | ||
|
|
||
| if actual_name not in self._activities: | ||
| raise KeyError(f"Activity '{name}' not found in registry") | ||
|
|
||
| return self._activities[actual_name] | ||
|
|
||
|
|
||
|
|
||
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.