-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add Connector Registration and Dynamic Dispatch #1375
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
43e8d0f
Add dynamic connector dispatcher
7468aa6
drop unused ditribute CLIs
damonmcc d4b24db
drop validation logic in `lifecycle.ditribute`
damonmcc 1545c21
generalize distribution to use connector dispatcher
damonmcc 2a4b090
generalize distribution away from socrata-specific
332c39b
Rewrite package_and_distribute script
f242fe0
Update GHA
305f3f8
move and skip SFTP test
damonmcc 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Lifecycle artifacts | ||
.library/ | ||
.publishing/ | ||
.package/ | ||
.lifecycle/ | ||
output/ | ||
.output/ | ||
.data | ||
|
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,6 @@ | ||
class FTPConnector: | ||
def push(self, dest_path: str, ftp_profile: str): | ||
raise Exception("Push not implemented for FTP") | ||
|
||
def pull(self, **kwargs): | ||
raise Exception("Pull not implemented for FTP") | ||
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from pathlib import Path | ||
|
||
BASE_PATH = Path(".lifecycle") | ||
|
||
|
||
class WORKING_DIRECTORIES: | ||
packaging = Path(".package") | ||
packaging = BASE_PATH / Path("package") |
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,44 @@ | ||
from typing import Unpack | ||
|
||
from dcpy.lifecycle.distribute.connectors import ( | ||
DistributionSFTPConnector, | ||
SocrataPublishConnector, | ||
) | ||
from dcpy.models.lifecycle.distribute import ( | ||
DatasetDestinationPushArgs, | ||
DistributeResult, | ||
) | ||
from dcpy.models.connectors import ConnectorDispatcher | ||
|
||
|
||
# Register all default connectors for `lifecycle.distribute`. | ||
# Third parties can similarly register their own connectors, | ||
# so long as the connector implements a ConnectorDispatcher protocol. | ||
dispatcher = ConnectorDispatcher[DatasetDestinationPushArgs, dict]() | ||
|
||
dispatcher.register(conn_type="socrata", connector=SocrataPublishConnector()) | ||
dispatcher.register(conn_type="sftp", connector=DistributionSFTPConnector()) | ||
|
||
|
||
def to_dataset_destination( | ||
**push_kwargs: Unpack[DatasetDestinationPushArgs], | ||
) -> DistributeResult: | ||
"""Distribute a dataset and specific dataset_destination_id. | ||
|
||
Requires fully rendered template, ie there should be no template variables in the metadata | ||
""" | ||
ds_md = push_kwargs["metadata"] | ||
dest = ds_md.get_destination(push_kwargs["dataset_destination_id"]) | ||
dest_type = dest.type | ||
|
||
try: | ||
result = dispatcher.push(dest_type, push_kwargs) | ||
return DistributeResult.from_push_kwargs( | ||
result=result, success=True, push_args=push_kwargs | ||
) | ||
except Exception as e: | ||
return DistributeResult.from_push_kwargs( | ||
result=f"Error pushing {push_kwargs['metadata'].attributes.display_name} to dest_type: {dest_type}, destination: {dest.id}: {str(e)}", | ||
success=False, | ||
push_args=push_kwargs, | ||
) |
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 |
---|---|---|
@@ -1,7 +1,41 @@ | ||
import typer | ||
from pathlib import Path | ||
|
||
from dcpy.lifecycle.distribute import socrata | ||
from dcpy.lifecycle import distribute | ||
import dcpy.models.product.dataset.metadata as m | ||
|
||
app = typer.Typer() | ||
|
||
app.add_typer(socrata.socrata_app, name="socrata") | ||
|
||
@app.command("from_local") | ||
def _dist_from_local( | ||
package_path: Path = typer.Argument(), | ||
dataset_destination_id: str = typer.Argument(), | ||
metadata_path: Path = typer.Option( | ||
None, | ||
"-m", | ||
"--metadata-path", | ||
help="(Optional) Metadata Path Override", | ||
), | ||
publish: bool = typer.Option( | ||
False, | ||
"-p", | ||
"--publish", | ||
help="Publish the Revision? Or leave it open.", | ||
), | ||
metadata_only: bool = typer.Option( | ||
False, | ||
"-z", | ||
"--metadata-only", | ||
help="Only push metadata (including attachments).", | ||
), | ||
): | ||
md = m.Metadata.from_path(metadata_path or (package_path / "metadata.yml")) | ||
result = distribute.to_dataset_destination( | ||
metadata=md, | ||
dataset_destination_id=dataset_destination_id, | ||
publish=publish, | ||
dataset_package_path=package_path, | ||
metadata_only=metadata_only, | ||
) | ||
print(result) |
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,42 @@ | ||
from typing import Any | ||
|
||
from dcpy.connectors.ftp import FTPConnector | ||
from dcpy.connectors.socrata import publish as socrata_pub | ||
from dcpy.models.lifecycle.distribute import DatasetDestinationPushArgs | ||
|
||
# Sadly, can't use Unpack on kwarg generics yet. | ||
# https://github.com/python/typing/issues/1399 | ||
|
||
|
||
# Wrap the connectors to bind them to the `PublisherPushKwargs` | ||
# so that we can register and delegate calls. | ||
# This is the recommended way for third parties to add custom Distribution Connectors. | ||
class DistributionSFTPConnector: | ||
conn_type: str | ||
|
||
def __init__(self): | ||
self.conn_type = "sftp" | ||
self._base_connector = FTPConnector() | ||
|
||
def push(self, arg: DatasetDestinationPushArgs) -> Any: | ||
md = arg["metadata"] | ||
dest = md.get_destination(arg["dataset_destination_id"]) | ||
dest_path = dest.custom["destination_path"] | ||
user_id = dest.custom["user_id"] | ||
self._base_connector.push(dest_path=dest_path, ftp_profile=user_id) | ||
|
||
def pull(self, _: dict) -> Any: | ||
raise Exception("Pull is not defined for any Distribution Connectors.") | ||
|
||
|
||
class SocrataPublishConnector: | ||
conn_type = "socrata" | ||
|
||
def push( | ||
self, | ||
arg: DatasetDestinationPushArgs, | ||
) -> Any: | ||
return socrata_pub.push_dataset(**arg) | ||
|
||
def pull(self, _: dict): | ||
raise Exception("Pull not implemented for Socrata Connector") | ||
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.
looks like this disables the use of
rich
for error messages. are "pretty" tracebacks in github action logs hard to read when distributing?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.
Yeah, the context variables from typer are humongous in this case, so it's a real pain to track back in the logs. Honestly, I really dislike having the full context all the time. It might be nice to tie this to the to log-level. (ie enable rich for debug level logs, but not for info)