mirror of
https://github.com/codex-storage/bittorrent-benchmarks.git
synced 2025-01-10 11:25:42 +00:00
remove typer and use argparse for the CLI
This commit is contained in:
parent
3e1eee7870
commit
55487c5a75
@ -1,26 +1,48 @@
|
|||||||
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
import typer
|
import typer
|
||||||
from pydantic_core import ValidationError
|
from pydantic_core import ValidationError
|
||||||
|
|
||||||
from benchmarks.core.config import ConfigParser
|
from benchmarks.core.config import ConfigParser, ExperimentBuilder
|
||||||
|
from benchmarks.core.experiments.experiments import Experiment
|
||||||
from benchmarks.deluge.config import DelugeExperimentConfig
|
from benchmarks.deluge.config import DelugeExperimentConfig
|
||||||
|
|
||||||
parser = ConfigParser()
|
config_parser = ConfigParser()
|
||||||
parser.register(DelugeExperimentConfig)
|
config_parser.register(DelugeExperimentConfig)
|
||||||
|
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
|
|
||||||
|
|
||||||
def _parse_config(config: Path):
|
def cmd_list(experiments: Dict[str, ExperimentBuilder[Experiment]], _):
|
||||||
|
"""
|
||||||
|
Lists the experiments available in CONFIG.
|
||||||
|
"""
|
||||||
|
print(f'Available experiments are:')
|
||||||
|
for experiment in experiments.keys():
|
||||||
|
print(f' - {experiment}')
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_run(experiments: Dict[str, ExperimentBuilder[Experiment]], args):
|
||||||
|
"""
|
||||||
|
Runs the experiment with name EXPERIMENT.
|
||||||
|
"""
|
||||||
|
if args.experiment not in experiments:
|
||||||
|
print(f'Experiment {args.experiment} not found.')
|
||||||
|
sys.exit(-1)
|
||||||
|
experiments[args.experiment].build().run()
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_config(config: Path) -> Dict[str, ExperimentBuilder[Experiment]]:
|
||||||
if not config.exists():
|
if not config.exists():
|
||||||
print(f'Config file {config} does not exist.')
|
print(f'Config file {config} does not exist.')
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
with config.open(encoding='utf-8') as infile:
|
with config.open(encoding='utf-8') as infile:
|
||||||
try:
|
try:
|
||||||
return parser.parse(infile)
|
return config_parser.parse(infile)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
print(f'There were errors parsing the config file.')
|
print(f'There were errors parsing the config file.')
|
||||||
for error in e.errors():
|
for error in e.errors():
|
||||||
@ -28,29 +50,6 @@ def _parse_config(config: Path):
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
|
||||||
def list(ctx: typer.Context):
|
|
||||||
"""
|
|
||||||
Lists the experiments available in CONFIG.
|
|
||||||
"""
|
|
||||||
experiments = ctx.obj
|
|
||||||
print(f'Available experiments are:')
|
|
||||||
for experiment in experiments.keys():
|
|
||||||
print(f' - {experiment}')
|
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
|
||||||
def run(ctx: typer.Context, experiment: str):
|
|
||||||
"""
|
|
||||||
Runs the experiment with name EXPERIMENT.
|
|
||||||
"""
|
|
||||||
experiments = ctx.obj
|
|
||||||
if experiment not in experiments:
|
|
||||||
print(f'Experiment {experiment} not found.')
|
|
||||||
sys.exit(-1)
|
|
||||||
experiments[experiment].build().run()
|
|
||||||
|
|
||||||
|
|
||||||
def _init_logging():
|
def _init_logging():
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -60,14 +59,24 @@ def _init_logging():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.callback()
|
def main():
|
||||||
def main(ctx: typer.Context, config: Path):
|
parser = argparse.ArgumentParser()
|
||||||
if ctx.resilient_parsing:
|
parser.add_argument('config', type=Path, help="Path to the experiment configuration file.")
|
||||||
return
|
|
||||||
|
commands = parser.add_subparsers(required=True)
|
||||||
|
list_cmd = commands.add_parser('list', help='Lists available experiments.')
|
||||||
|
list_cmd.set_defaults(func=cmd_list)
|
||||||
|
|
||||||
|
run_cmd = commands.add_parser('run')
|
||||||
|
run_cmd.add_argument('experiment', type=str, help='Name of the experiment to run.')
|
||||||
|
run_cmd.set_defaults(func=cmd_run)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
ctx.obj = _parse_config(config)
|
|
||||||
_init_logging()
|
_init_logging()
|
||||||
|
|
||||||
|
args.func(_parse_config(args.config), args)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app()
|
main()
|
||||||
|
111
poetry.lock
generated
111
poetry.lock
generated
@ -11,20 +11,6 @@ files = [
|
|||||||
{file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"},
|
{file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "click"
|
|
||||||
version = "8.1.7"
|
|
||||||
description = "Composable command line interface toolkit"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.7"
|
|
||||||
files = [
|
|
||||||
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
|
|
||||||
{file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colorama"
|
name = "colorama"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
@ -58,41 +44,6 @@ files = [
|
|||||||
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
|
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "markdown-it-py"
|
|
||||||
version = "3.0.0"
|
|
||||||
description = "Python port of markdown-it. Markdown parsing, done right!"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.8"
|
|
||||||
files = [
|
|
||||||
{file = "markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb"},
|
|
||||||
{file = "markdown_it_py-3.0.0-py3-none-any.whl", hash = "sha256:355216845c60bd96232cd8d8c40e8f9765cc86f46880e43a8fd22dc1a1a8cab1"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
mdurl = ">=0.1,<1.0"
|
|
||||||
|
|
||||||
[package.extras]
|
|
||||||
benchmarking = ["psutil", "pytest", "pytest-benchmark"]
|
|
||||||
code-style = ["pre-commit (>=3.0,<4.0)"]
|
|
||||||
compare = ["commonmark (>=0.9,<1.0)", "markdown (>=3.4,<4.0)", "mistletoe (>=1.0,<2.0)", "mistune (>=2.0,<3.0)", "panflute (>=2.3,<3.0)"]
|
|
||||||
linkify = ["linkify-it-py (>=1,<3)"]
|
|
||||||
plugins = ["mdit-py-plugins"]
|
|
||||||
profiling = ["gprof2dot"]
|
|
||||||
rtd = ["jupyter_sphinx", "mdit-py-plugins", "myst-parser", "pyyaml", "sphinx", "sphinx-copybutton", "sphinx-design", "sphinx_book_theme"]
|
|
||||||
testing = ["coverage", "pytest", "pytest-cov", "pytest-regressions"]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "mdurl"
|
|
||||||
version = "0.1.2"
|
|
||||||
description = "Markdown URL utilities"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.7"
|
|
||||||
files = [
|
|
||||||
{file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"},
|
|
||||||
{file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mypy"
|
name = "mypy"
|
||||||
version = "1.13.0"
|
version = "1.13.0"
|
||||||
@ -330,20 +281,6 @@ files = [
|
|||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
|
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pygments"
|
|
||||||
version = "2.18.0"
|
|
||||||
description = "Pygments is a syntax highlighting package written in Python."
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.8"
|
|
||||||
files = [
|
|
||||||
{file = "pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a"},
|
|
||||||
{file = "pygments-2.18.0.tar.gz", hash = "sha256:786ff802f32e91311bff3889f6e9a86e81505fe99f2735bb6d60ae0c5004f199"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.extras]
|
|
||||||
windows-terminal = ["colorama (>=0.4.6)"]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pytest"
|
name = "pytest"
|
||||||
version = "8.3.3"
|
version = "8.3.3"
|
||||||
@ -426,35 +363,6 @@ files = [
|
|||||||
{file = "pyyaml-6.0.2.tar.gz", hash = "sha256:d584d9ec91ad65861cc08d42e834324ef890a082e591037abe114850ff7bbc3e"},
|
{file = "pyyaml-6.0.2.tar.gz", hash = "sha256:d584d9ec91ad65861cc08d42e834324ef890a082e591037abe114850ff7bbc3e"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rich"
|
|
||||||
version = "13.9.4"
|
|
||||||
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.8.0"
|
|
||||||
files = [
|
|
||||||
{file = "rich-13.9.4-py3-none-any.whl", hash = "sha256:6049d5e6ec054bf2779ab3358186963bac2ea89175919d699e378b99738c2a90"},
|
|
||||||
{file = "rich-13.9.4.tar.gz", hash = "sha256:439594978a49a09530cff7ebc4b5c7103ef57baf48d5ea3184f21d9a2befa098"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
markdown-it-py = ">=2.2.0"
|
|
||||||
pygments = ">=2.13.0,<3.0.0"
|
|
||||||
|
|
||||||
[package.extras]
|
|
||||||
jupyter = ["ipywidgets (>=7.5.1,<9)"]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "shellingham"
|
|
||||||
version = "1.5.4"
|
|
||||||
description = "Tool to Detect Surrounding Shell"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.7"
|
|
||||||
files = [
|
|
||||||
{file = "shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686"},
|
|
||||||
{file = "shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "torrentool"
|
name = "torrentool"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@ -469,23 +377,6 @@ files = [
|
|||||||
[package.extras]
|
[package.extras]
|
||||||
cli = ["click"]
|
cli = ["click"]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "typer"
|
|
||||||
version = "0.13.1"
|
|
||||||
description = "Typer, build great CLIs. Easy to code. Based on Python type hints."
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.7"
|
|
||||||
files = [
|
|
||||||
{file = "typer-0.13.1-py3-none-any.whl", hash = "sha256:5b59580fd925e89463a29d363e0a43245ec02765bde9fb77d39e5d0f29dd7157"},
|
|
||||||
{file = "typer-0.13.1.tar.gz", hash = "sha256:9d444cb96cc268ce6f8b94e13b4335084cef4c079998a9f4851a90229a3bd25c"},
|
|
||||||
]
|
|
||||||
|
|
||||||
[package.dependencies]
|
|
||||||
click = ">=8.0.0"
|
|
||||||
rich = ">=10.11.0"
|
|
||||||
shellingham = ">=1.3.0"
|
|
||||||
typing-extensions = ">=3.7.4.3"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "types-pyyaml"
|
name = "types-pyyaml"
|
||||||
version = "6.0.12.20240917"
|
version = "6.0.12.20240917"
|
||||||
@ -511,4 +402,4 @@ files = [
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.12"
|
python-versions = "^3.12"
|
||||||
content-hash = "f3c2c0480921a5d481f0e8de78329998a8fddae7ccecc6cb9cb9a01e3f6137c5"
|
content-hash = "c10ab6006a3097ae8fcbac02448e98cf18f61146ab311979e1e9d5e735e2369d"
|
||||||
|
@ -13,7 +13,6 @@ pathvalidate = "^3.2.1"
|
|||||||
torrentool = "^1.2.0"
|
torrentool = "^1.2.0"
|
||||||
pydantic = "^2.10.2"
|
pydantic = "^2.10.2"
|
||||||
pyyaml = "^6.0.2"
|
pyyaml = "^6.0.2"
|
||||||
typer = "^0.13.1"
|
|
||||||
|
|
||||||
[tool.poetry.group.test.dependencies]
|
[tool.poetry.group.test.dependencies]
|
||||||
pytest = "^8.3.3"
|
pytest = "^8.3.3"
|
||||||
@ -33,4 +32,4 @@ requires = ["poetry-core"]
|
|||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
bittorrent-benchmarks = "benchmarks.cli:app"
|
bittorrent-benchmarks = "benchmarks.cli:main"
|
Loading…
x
Reference in New Issue
Block a user