Refactor Gennet cli so now is more elegant and easy to maintain.

This commit is contained in:
Alberto Soutullo 2023-01-21 19:21:45 +01:00
parent f9b9e82ff9
commit d91f524f98
2 changed files with 27 additions and 26 deletions

View File

@ -5,7 +5,7 @@
"same_toml_configuration": false "same_toml_configuration": false
}, },
"gennet": { "gennet": {
"num_nodes": 3, "num_nodes": 5,
"num_topics": 1, "num_topics": 1,
"node_type": "desktop", "node_type": "desktop",
"network_type": "scalefree", "network_type": "scalefree",

View File

@ -222,41 +222,43 @@ def update_file_from_cli(dirname, num_nodes, num_topics,
config_obj["gennet"]["num_subnets"] = num_subnets config_obj["gennet"]["num_subnets"] = num_subnets
def conf_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):
if value:
typer.echo(f"Loading config file: {value}")
try:
with open(value, 'r') as f: # Load config file
conf = json.load(f)
conf = conf["gennet"]
ctx.default_map = ctx.default_map or {} # Initialize the default map
ctx.default_map.update(conf) # Merge the config dict into default_map
except Exception as ex:
raise typer.BadParameter(str(ex))
return value
### the main ########################################################################## ### the main ##########################################################################
def main(dirname: str = "WakuNetwork", num_nodes: int = 4, num_topics: int = 1, def main(dirname: str = "WakuNetwork",
num_nodes: int = 4,
num_topics: int = 1,
network_type: networkType = networkType.NEWMANWATTSSTROGATZ.value, network_type: networkType = networkType.NEWMANWATTSSTROGATZ.value,
node_type: nodeType = nodeType.DESKTOP.value, node_type: nodeType = nodeType.DESKTOP.value,
num_subnets: int = -1, num_subnets: int = -1,
num_partitions: int = 1, num_partitions: int = 1,
config_file: str = './config/config.json'): config_file: str = typer.Option("", callback=conf_callback, is_eager=True)):
""" Load config file """
try:
with open(config_file, 'r') as file:
config_obj = json.load(file)
except Exception as e:
print(e)
sys.exit(1)
print(config_obj)
update_file_from_cli(dirname, num_nodes, num_topics, network_type, node_type, num_subnets,
num_partitions, config_obj)
# Sanity checks # Sanity checks
if config_obj['general']['num_partitions'] > 1: if num_partitions > 1:
raise ValueError( raise ValueError(
f"--num-partitions {config_obj['general']['num_partitions']}, Sorry, we do not yet support partitions") f"--num-partitions {num_partitions}, Sorry, we do not yet support partitions")
if config_obj['general']['num_subnets'] > config_obj['general']['num_nodes']: if num_subnets > num_nodes:
raise ValueError( raise ValueError(
f"num_subnets must be <= num_nodes: num_subnets={config_obj['general']['num_subnets']}, num_nodes={config_obj['general']['num_nodes']}") f"num_subnets must be <= num_nodes: num_subnets={num_subnets}, num_nodes={num_nodes}")
if config_obj['general']['num_subnets'] == -1: if num_subnets == -1:
config_obj['general']['num_subnets'] = config_obj['general']['num_nodes'] num_subnets = num_nodes
# Generate the network # Generate the network
G = generate_network(config_obj['general']['num_nodes'], G = generate_network(num_nodes, networkType(network_type))
networkType(config_obj['general']['network_type']))
# # Refuse to overwrite non-empty dirs # # Refuse to overwrite non-empty dirs
# if exists_or_nonempty(config_obj['general']['topology_path']): # if exists_or_nonempty(config_obj['general']['topology_path']):
@ -264,8 +266,7 @@ def main(dirname: str = "WakuNetwork", num_nodes: int = 4, num_topics: int = 1,
os.makedirs('./config/topology_generated/', exist_ok=True) os.makedirs('./config/topology_generated/', exist_ok=True)
# Generate file format specific data structs and write the files; optionally, draw the network # Generate file format specific data structs and write the files; optionally, draw the network
generate_and_write_files('./config/topology_generated/', config_obj['general']['num_topics'], generate_and_write_files('./config/topology_generated/', num_topics, num_subnets, G)
config_obj['general']['num_subnets'], G)
# draw(dirname, G) # draw(dirname, G)