generated from rochacbruno/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from CoderChen01/cjj_dev
Complete the config command and json data export
- Loading branch information
Showing
36 changed files
with
546 additions
and
126 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 |
---|---|---|
@@ -1,8 +1,107 @@ | ||
import typing as t # noqa: F401 | ||
import typing as t | ||
from dataclasses import fields | ||
|
||
import click | ||
from click.shell_completion import CompletionItem | ||
|
||
from opendigger_pycli.console import CONSOLE | ||
|
||
@click.command("config") | ||
def config(): | ||
pass | ||
from ..base import pass_environment | ||
from ..config import ALL_CONFIGS | ||
|
||
if t.TYPE_CHECKING: | ||
from click.core import Context, Parameter | ||
|
||
from opendigger_pycli.datatypes.config import BaseConfig | ||
|
||
from ..base import Environment | ||
|
||
|
||
def config_shell_completion( | ||
ctx: t.Optional["Context"], param: t.Optional["Parameter"], incomplete: str | ||
) -> t.List[CompletionItem]: | ||
incomplete_splited = incomplete.rsplit(".", 1)[0] | ||
if incomplete_splited == "": | ||
return [ | ||
CompletionItem(config_dataclass_key) for config_dataclass_key in ALL_CONFIGS | ||
] | ||
is_key = False | ||
last_config_dataclass: t.Optional[BaseConfig] = None | ||
for config_dataclass_key in ALL_CONFIGS: | ||
if incomplete_splited.startswith(config_dataclass_key): | ||
if incomplete_splited != config_dataclass_key: | ||
return [CompletionItem(config_dataclass_key + ".")] | ||
else: | ||
is_key = True | ||
last_config_dataclass = ALL_CONFIGS[config_dataclass_key] # type: ignore | ||
break | ||
else: | ||
continue | ||
|
||
if not is_key and last_config_dataclass is None: | ||
return [] | ||
|
||
if incomplete[:-1] == incomplete_splited: | ||
return [CompletionItem(f"{incomplete_splited}.{field.name}") for field in fields(last_config_dataclass)] # type: ignore | ||
|
||
for field in fields(last_config_dataclass): # type: ignore | ||
if incomplete in f"{incomplete_splited}.{field.name}": | ||
return [CompletionItem(f"{incomplete_splited}.{field.name}")] | ||
|
||
return [] | ||
|
||
|
||
def parse_config_key(key: str) -> t.Tuple[str, str]: | ||
section_name, config_key = key.rsplit(".", 1) | ||
return section_name, config_key | ||
|
||
|
||
def check_config_setting( | ||
ctx: "Context", param: "Parameter", values: t.List[t.Tuple[str, str]] | ||
) -> t.List[t.Tuple[str, str]]: | ||
for value in values: | ||
try: | ||
section_name, config_key = parse_config_key(value[0]) | ||
except Exception: | ||
raise click.BadParameter(f"{value[0]} is not a valid config key") | ||
else: | ||
if section_name not in ALL_CONFIGS or config_key not in [ | ||
field.name for field in fields(ALL_CONFIGS[section_name]) | ||
]: | ||
raise click.BadParameter(f"{value[0]} is not a valid config key") | ||
return values | ||
|
||
|
||
@click.command("config") # type: ignore | ||
@click.option( | ||
"--set", | ||
"-s", | ||
"config_settings", | ||
type=(str, str), | ||
multiple=True, | ||
callback=check_config_setting, | ||
shell_complete=config_shell_completion, | ||
help="Set config value", | ||
required=True, | ||
) | ||
@click.option( | ||
"--persist/--no-persist", | ||
"-p/-n", | ||
is_flag=True, | ||
default=True, | ||
help="Set config value persistently", | ||
) | ||
@pass_environment | ||
def config( | ||
env: "Environment", config_settings: t.List[t.Tuple[str, str]], persist: bool | ||
) -> None: | ||
"""Set config value""" | ||
for config_setting in config_settings: | ||
key, value = config_setting | ||
section_name, key = parse_config_key(key) | ||
env.dlog(f"set config: {section_name}.{key}={value}") | ||
env.set_config(section_name, key, value, is_persist=persist) | ||
env.dlog(f"finished to set config: {section_name}.{key}={value}") | ||
|
||
CONSOLE.print("[green]Config set successfully[/]") | ||
CONSOLE.print(env.cli_config) |
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,29 +1,53 @@ | ||
import typing as t | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
from opendigger_pycli.exporters import ( | ||
SURPPORTED_EXPORT_FORMAT_TYPE, | ||
SURPPORTED_EXPORT_FORMATS, | ||
) | ||
from opendigger_pycli.results.export import ExportResult | ||
from opendigger_pycli.utils.decorators import processor | ||
|
||
from ..base import pass_environment | ||
|
||
if t.TYPE_CHECKING: | ||
from opendigger_pycli.results.query import QueryResults | ||
|
||
from ..base import Environment | ||
|
||
|
||
@click.command("export", help="Export metrics") | ||
@click.option( | ||
"--format", | ||
"-f", | ||
"format_name", | ||
type=click.Choice(["csv", "json", "mhtml"]), | ||
type=click.Choice(SURPPORTED_EXPORT_FORMATS), | ||
required=True, | ||
help="Format to export", | ||
) | ||
@click.option( | ||
"--save-dir", | ||
"-s", | ||
"save_dir", | ||
type=click.Path(file_okay=False, resolve_path=True, path_type=Path), | ||
required=True, | ||
help="Directory to save indicators", | ||
) | ||
@click.option( | ||
"--split/--no-split", | ||
"is_split", | ||
default=True, | ||
help="Save indicators in separate files", | ||
) | ||
@click.option("--filename", "-o", "filename", type=str, required=True) | ||
@processor | ||
@pass_environment | ||
def export( | ||
env: "Environment", | ||
format_name: t.Literal["csv", "json", "mhtml"], | ||
filename: click.Path, | ||
results: "QueryResults", | ||
format: SURPPORTED_EXPORT_FORMAT_TYPE, | ||
save_dir: Path, | ||
is_split: bool, | ||
): | ||
pass | ||
ExportResult(results, format, save_dir, is_split).export() | ||
yield from results |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ader/tests/test_dataloader_proto_check.py → ...ders/tests/test_dataloader_proto_check.py
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
2 changes: 1 addition & 1 deletion
2
...er_pycli/dataloader/tests/test_indices.py → ...r_pycli/dataloaders/tests/test_indices.py
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
2 changes: 1 addition & 1 deletion
2
...er_pycli/dataloader/tests/test_metrics.py → ...r_pycli/dataloaders/tests/test_metrics.py
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
2 changes: 1 addition & 1 deletion
2
...r_pycli/dataloader/tests/test_networks.py → ..._pycli/dataloaders/tests/test_networks.py
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
File renamed without changes.
Oops, something went wrong.