added _sanity_check_

This commit is contained in:
0xFugue 2023-07-19 12:14:16 +05:30
parent 46317aafb0
commit 3509a780a8
1 changed files with 24 additions and 26 deletions

View File

@ -16,6 +16,8 @@ class networkType(Enum):
GENNET="gennet" GENNET="gennet"
GENLOAD="wls" GENLOAD="wls"
CONFIG="config"
# Util and format functions # Util and format functions
#----------------------------------------------------------- #-----------------------------------------------------------
@ -390,40 +392,36 @@ def _config_file_callback(ctx: typer.Context, param: typer.CallbackParam, cfile:
raise typer.BadParameter(str(ex)) raise typer.BadParameter(str(ex))
return cfile return cfile
@app.command() def _sanity_check(fname, ftype="json", keys):
def kurtosis(ctx: typer.Context, config_file: Path): if not fname.exists():
if not config_file.exists(): log.error(f'The file "{fname}" does not exist')
log.error(f'The config file "{config_file}" does not exist')
sys.exit(0) sys.exit(0)
try: try:
with open(config_file, 'r') as f: # Load config file with open(fname, 'r') as f: # Load config file
if ftype == "json": # Both batch and kurtosis use json
conf = json.load(f) conf = json.load(f)
if GENNET not in conf: for key in keys:
log.error(f'{GENNET} configuration not found in {config_file}') if key not in conf:
log.error(f'The json {key} not found in {fname}')
sys.exit(0) sys.exit(0)
elif GENLOAD not in conf: elif ftype == "yaml": # Shadow uses yaml
log.error(f'{GENLOAD} configuration not found in {config_file}') log.error(f'YAML is not yet supported : {fname}')
sys.exit(0) sys.exit(0)
except Exception as ex: except Exception as ex:
raise typer.BadParameter(str(ex))
@app.command()
def kurtosis(ctx: typer.Context, config_file: Path):
_sanity_check(fname, "json", [GENNET, GENLOAD])
@app.command() @app.command()
def batch(ctx: typer.Context, batch_file: Path): def batch(ctx: typer.Context, batch_file: Path):
if not batch_file.exists(): _sanity_check(fname, "json", [CONFIG])
log.error(f'The config file "{batch_file}" does not exist')
sys.exit(0)
try:
with open(config_file, 'r') as f: # Load config file
conf = json.load(f)
if GENNET not in conf:
log.error(f'{GENNET} configuration not found in {config_file}')
sys.exit(0)
elif GENLOAD not in conf:
log.error(f'{GENLOAD} configuration not found in {config_file}')
sys.exit(0)
except Exception as ex:
raise typer.BadParameter(str(ex))
@app.command()
def shadow(ctx: typer.Context, batch_file: Path):
_sanity_check(fname, "yaml", [])
@app.command() @app.command()
def cli(ctx: typer.Context, def cli(ctx: typer.Context,