-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: consolidate linters and formatter into Ruff for improved perfo…
…rmance and simplicity (#216)
- Loading branch information
1 parent
8d91d92
commit 15d2809
Showing
55 changed files
with
349 additions
and
268 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
import json | ||
import tarfile | ||
import tempfile | ||
from collections.abc import Iterable | ||
from pathlib import Path | ||
from typing import Any, List, Optional, cast | ||
from typing import Any, cast | ||
from urllib.parse import urljoin | ||
|
||
import requests | ||
|
@@ -53,14 +54,15 @@ def _build_package(package: Path) -> Path: | |
def _get_name_from_metadata(host: str, token: str, package: Path) -> str | None: | ||
"""Extract metadata from a provided package and return the package name if it exists in the metadata.""" | ||
try: | ||
metadata_response = requests.post( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
plugin_url(host, "extract-metadata"), | ||
headers={"Authorization": f"Bearer {token}"}, | ||
files={"package": open(package, "rb")}, | ||
) | ||
with open(package) as package_file: | ||
This comment has been minimized.
Sorry, something went wrong.
beaugunderson
Member
|
||
metadata_response = requests.post( | ||
plugin_url(host, "extract-metadata"), | ||
headers={"Authorization": f"Bearer {token}"}, | ||
files={"package": package_file}, | ||
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if metadata_response.status_code != requests.codes.ok: | ||
print(f"Status code {metadata_response.status_code}: {metadata_response.text}") | ||
|
@@ -116,8 +118,8 @@ def _get_meta_properties(protocol_path: Path, classname: str) -> dict[str, str]: | |
|
||
|
||
def _get_protocols_with_new_cqm_properties( | ||
protocol_classes: List[dict[str, Any]], plugin: Path | ||
) -> List[dict[str, Any]] | None: | ||
protocol_classes: Iterable[dict[str, Any]], plugin: Path | ||
) -> Iterable[dict[str, Any]] | None: | ||
"""Extract the meta properties of any ClinicalQualityMeasure Protocols included in the plugin if they have changed.""" | ||
has_updates = False | ||
protocol_props = [] | ||
|
@@ -146,14 +148,14 @@ def init() -> None: | |
try: | ||
project_dir = cookiecutter(str(template)) | ||
except OutputDirExistsException: | ||
raise typer.BadParameter(f"The supplied directory already exists") | ||
raise typer.BadParameter("The supplied directory already exists") from None | ||
|
||
print(f"Project created in {project_dir}") | ||
|
||
|
||
def install( | ||
plugin_name: Path = typer.Argument(..., help="Path to plugin to install"), | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
|
@@ -181,15 +183,16 @@ def install( | |
print(f"Posting {built_package_path.absolute()} to {url}") | ||
|
||
try: | ||
r = requests.post( | ||
url, | ||
data={"is_enabled": True}, | ||
files={"package": open(built_package_path, "rb")}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
with open(built_package_path, "rb") as package: | ||
r = requests.post( | ||
url, | ||
data={"is_enabled": True}, | ||
files={"package": package}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.status_code == requests.codes.created: | ||
print(f"Plugin {plugin_name} successfully installed!") | ||
|
@@ -207,7 +210,7 @@ def install( | |
|
||
def uninstall( | ||
name: str = typer.Argument(..., help="Plugin name to uninstall"), | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
|
@@ -232,7 +235,7 @@ def uninstall( | |
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.status_code == requests.codes.no_content: | ||
print(f"Plugin {name} successfully uninstalled!") | ||
|
@@ -243,7 +246,7 @@ def uninstall( | |
|
||
def enable( | ||
name: str = typer.Argument(..., help="Plugin name to enable"), | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
|
@@ -269,7 +272,7 @@ def enable( | |
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.ok: | ||
print(f"Plugin {name} successfully enabled!") | ||
|
@@ -280,7 +283,7 @@ def enable( | |
|
||
def disable( | ||
name: str = typer.Argument(..., help="Plugin name to disable"), | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
|
@@ -306,7 +309,7 @@ def disable( | |
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.ok: | ||
print(f"Plugin {name} successfully disabled!") | ||
|
@@ -316,11 +319,11 @@ def disable( | |
|
||
|
||
def list( | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
) | ||
), | ||
) -> None: | ||
"""List all plugins from a Canvas instance.""" | ||
if not host: | ||
|
@@ -337,7 +340,7 @@ def list( | |
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.status_code == requests.codes.ok: | ||
plugins = r.json().get("results", []) | ||
|
@@ -382,7 +385,7 @@ def validate_manifest( | |
|
||
except json.JSONDecodeError: | ||
print("There was a problem loading the manifest file, please ensure it's valid JSON") | ||
raise typer.Abort() | ||
raise typer.Abort() from None | ||
|
||
validate_manifest_file(manifest_json) | ||
|
||
|
@@ -391,14 +394,14 @@ def validate_manifest( | |
|
||
def update( | ||
name: str = typer.Argument(..., help="Plugin name to update"), | ||
package: Optional[Path] = typer.Option( | ||
package_path: Path | None = typer.Option( | ||
help="Path to a wheel or sdist file containing the python package to install", | ||
default=None, | ||
), | ||
is_enabled: Optional[bool] = typer.Option( | ||
is_enabled: bool | None = typer.Option( | ||
None, "--enable/--disable", show_default=False, help="Enable/disable the plugin" | ||
), | ||
host: Optional[str] = typer.Option( | ||
host: str | None = typer.Option( | ||
callback=get_default_host, | ||
help="Canvas instance to connect to", | ||
default=None, | ||
|
@@ -408,27 +411,36 @@ def update( | |
if not host: | ||
raise typer.BadParameter("Please specify a host or set a default via the `auth` command") | ||
|
||
if package: | ||
validate_package(package) | ||
if package_path: | ||
validate_package(package_path) | ||
|
||
token = get_or_request_api_token(host) | ||
|
||
print(f"Updating plugin {name} from {host} with {is_enabled=}, {package=}") | ||
|
||
binary_package = {"package": open(package, "rb")} if package else None | ||
print(f"Updating plugin {name} from {host} with {is_enabled=}, {package_path=}") | ||
|
||
url = plugin_url(host, name) | ||
|
||
try: | ||
r = requests.patch( | ||
url, | ||
data={"is_enabled": is_enabled} if is_enabled is not None else {}, | ||
files=binary_package, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
data = {"is_enabled": is_enabled} if is_enabled is not None else {} | ||
headers = {"Authorization": f"Bearer {token}"} | ||
|
||
if package_path: | ||
with open(package_path, "rb") as package: | ||
r = requests.patch( | ||
url, | ||
data=data, | ||
headers=headers, | ||
files={"package": package}, | ||
) | ||
else: | ||
r = requests.patch( | ||
url, | ||
data=data, | ||
headers=headers, | ||
) | ||
except requests.exceptions.RequestException: | ||
print(f"Failed to connect to {host}") | ||
raise typer.Exit(1) | ||
raise typer.Exit(1) from None | ||
|
||
if r.status_code == requests.codes.ok: | ||
print("Plugin successfully updated!") | ||
|
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.
🐛