This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from rossumai/refactor/extract-arguments
Refactor/extract arguments + remove the necessity to specify file type of loaded schema
- Loading branch information
Showing
13 changed files
with
348 additions
and
260 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
import functools | ||
from typing import Callable, IO, Optional | ||
|
||
import click | ||
|
||
from elisctl.schema.xlsx import SchemaContent, XlsxToSchema | ||
|
||
|
||
def schema_content_factory(file_decorator): | ||
def schema_content(command: Optional[Callable] = None, **file_decorator_kwargs): | ||
def _load_func(schema_content_file_: Optional[IO[bytes]]) -> Optional[SchemaContent]: | ||
if schema_content_file_ is None: | ||
return None | ||
|
||
errors = [] | ||
for load_func in (json.load, XlsxToSchema().convert): | ||
try: | ||
return load_func(schema_content_file_) # type: ignore | ||
except Exception as e: | ||
errors.append(str(e)) | ||
schema_content_file_.seek(0) | ||
|
||
raise NotImplementedError(", ".join(errors)) | ||
|
||
def decorator(command_: Callable): | ||
@file_decorator(**file_decorator_kwargs) | ||
@functools.wraps(command_) | ||
def wrapped(*args, schema_content_file_: Optional[IO[bytes]], **kwargs): | ||
try: | ||
schema_content_ = _load_func(schema_content_file_) | ||
except Exception as e: | ||
raise click.ClickException( | ||
f"File {schema_content_file_} could not be loaded. Because of {e}" | ||
) from e | ||
|
||
return command_(*args, schema_content=schema_content_, **kwargs) | ||
|
||
return wrapped | ||
|
||
if command is None: | ||
return decorator | ||
return decorator(command) | ||
|
||
return schema_content |
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 |
---|---|---|
@@ -1,45 +0,0 @@ | ||
import json | ||
from typing import IO, Optional | ||
|
||
import click | ||
|
||
from elisctl import option | ||
from elisctl.lib.api_client import APIClient, get_json | ||
from elisctl.schema.xlsx import SchemaToXlsx | ||
from . import transform, upload | ||
|
||
|
||
@click.group("schema") | ||
def cli() -> None: | ||
pass | ||
|
||
|
||
cli.add_command(transform.cli) | ||
cli.add_command(upload.upload_command) | ||
|
||
|
||
@cli.command(name="get", help="Download schema from ELIS.") | ||
@click.pass_context | ||
@click.argument("id_", metavar="ID", type=str) | ||
@click.option("--indent", default=2, type=int) | ||
@click.option("--ensure-ascii", is_flag=True, type=bool) | ||
@click.option("--format", "format_", default="json", type=click.Choice(["json", "xlsx"])) | ||
@option.output_file | ||
def download_command( | ||
ctx: click.Context, | ||
id_: str, | ||
indent: int, | ||
ensure_ascii: bool, | ||
format_: str, | ||
output_file: Optional[IO[str]], | ||
): | ||
with APIClient(context=ctx.obj) as api_client: | ||
schema_dict = get_json(api_client.get(f"schemas/{id_}")) | ||
if format_ == "json": | ||
schema_file = json.dumps( | ||
schema_dict["content"], indent=indent, ensure_ascii=ensure_ascii, sort_keys=True | ||
).encode("utf-8") | ||
else: | ||
schema_file = SchemaToXlsx().convert(schema_dict["content"]) | ||
|
||
click.echo(schema_file, file=output_file, nl=False) | ||
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,47 @@ | ||
import json | ||
|
||
from typing import Optional, IO | ||
|
||
import click | ||
|
||
from elisctl import option | ||
from elisctl.lib.api_client import APIClient, get_json | ||
from . import upload | ||
from .xlsx import SchemaToXlsx | ||
from .transform import commands as transform | ||
|
||
|
||
@click.group("schema") | ||
def cli() -> None: | ||
pass | ||
|
||
|
||
cli.add_command(transform.cli) | ||
cli.add_command(upload.upload_command) | ||
|
||
|
||
@cli.command(name="get", help="Download schema from ELIS.") | ||
@click.pass_context | ||
@click.argument("id_", metavar="ID", type=str) | ||
@click.option("--indent", default=2, type=int) | ||
@click.option("--ensure-ascii", is_flag=True, type=bool) | ||
@click.option("--format", "format_", default="json", type=click.Choice(["json", "xlsx"])) | ||
@option.output_file | ||
def download_command( | ||
ctx: click.Context, | ||
id_: str, | ||
indent: int, | ||
ensure_ascii: bool, | ||
format_: str, | ||
output_file: Optional[IO[str]], | ||
): | ||
with APIClient(context=ctx.obj) as api_client: | ||
schema_dict = get_json(api_client.get(f"schemas/{id_}")) | ||
if format_ == "json": | ||
schema_file = json.dumps( | ||
schema_dict["content"], indent=indent, ensure_ascii=ensure_ascii, sort_keys=True | ||
).encode("utf-8") | ||
else: | ||
schema_file = SchemaToXlsx().convert(schema_dict["content"]) | ||
|
||
click.echo(schema_file, file=output_file, nl=False) |
Oops, something went wrong.