-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored component hierarchies and initialization (#121)
* Constrained `add_component()` to only work in the initializer * Removed ContainerComponent * Added the `prepare()` method to the `Component` class * Refactored `run_component()` to (only) accept either a dict or a `Component` subclass
- Loading branch information
Showing
22 changed files
with
567 additions
and
297 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
|
@@ -15,14 +15,13 @@ | |
|
||
|
||
class ApplicationComponent(CLIApplicationComponent): | ||
async def start(self) -> None: | ||
def __init__(self) -> None: | ||
self.add_component( | ||
"mailer", | ||
backend="smtp", | ||
host="your.smtp.server.here", | ||
message_defaults={"sender": "[email protected]", "to": "[email protected]"}, | ||
) | ||
await super().start() | ||
|
||
@inject | ||
async def run(self, *, mailer: Mailer = resource()) -> None: | ||
|
@@ -52,4 +51,4 @@ async def run(self, *, mailer: Mailer = resource()) -> None: | |
|
||
|
||
if __name__ == "__main__": | ||
run_application(ApplicationComponent(), logging=logging.DEBUG) | ||
run_application(ApplicationComponent, logging=logging.DEBUG) |
This file contains 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 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 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 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,49 @@ | ||
from asphalt.core import ( | ||
Component, | ||
add_resource, | ||
get_resource, | ||
get_resource_nowait, | ||
run_application, | ||
) | ||
|
||
|
||
class ParentComponent(Component): | ||
def __init__(self) -> None: | ||
self.add_component("child1", ChildComponent, name="child1") | ||
self.add_component("child2", ChildComponent, name="child2") | ||
|
||
async def prepare(self) -> None: | ||
print("ParentComponent.prepare()") | ||
add_resource("Hello") # adds a `str` type resource by the name `default` | ||
|
||
async def start(self) -> None: | ||
print("ParentComponent.start()") | ||
print(get_resource_nowait(str, "child1_resource")) | ||
print(get_resource_nowait(str, "child2_resource")) | ||
|
||
|
||
class ChildComponent(Component): | ||
parent_resource: str | ||
sibling_resource: str | ||
|
||
def __init__(self, name: str) -> None: | ||
self.name = name | ||
|
||
async def prepare(self) -> None: | ||
self.parent_resource = get_resource_nowait(str) | ||
print(f"ChildComponent.prepare() [{self.name}]") | ||
|
||
async def start(self) -> None: | ||
print(f"ChildComponent.start() [{self.name}]") | ||
|
||
# Add a `str` type resource, with a name like `childX_resource` | ||
add_resource( | ||
f"{self.parent_resource}, world from {self.name}!", f"{self.name}_resource" | ||
) | ||
|
||
# Do this only after adding our own resource, or we end up in a deadlock | ||
resource = "child1_resource" if self.name == "child2" else "child1_resource" | ||
await get_resource(str, resource) | ||
|
||
|
||
run_application(ParentComponent) |
This file contains 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 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 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 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 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.