1111
1212from simulaqron .network import Network
1313from simulaqron .settings import LOCAL_SIMULAQRON_SETTINGS , LOCAL_NETWORK_SETTINGS , HOME_NETWORK_SETTINGS
14- from simulaqron .settings import simulaqron_settings , get_default_network_config_file , network_config
15- from simulaqron .settings .network_config import NodeConfig , DEFAULT_SIMULAQRON_NETWORK_FILENAME
14+ from simulaqron .settings import simulaqron_settings , network_config
15+ from simulaqron .settings .network_config import (NodeConfig , DEFAULT_SIMULAQRON_NETWORK_FILENAME ,
16+ get_default_network_config_file )
1617from simulaqron .settings .simulaqron_config import SimBackend
1718
1819CONTEXT_SETTINGS = dict (help_option_names = ["-h" , "--help" ])
@@ -137,7 +138,10 @@ def _create_local_networks_if_needed_and_load():
137138 _load_local_network_or_default ()
138139
139140
140- @click .group (context_settings = CONTEXT_SETTINGS , epilog = "Run 'simulaqron COMMAND --help' for more information on a command." )
141+ @click .group (
142+ context_settings = CONTEXT_SETTINGS ,
143+ epilog = "Run 'simulaqron COMMAND --help' for more information on a command."
144+ )
141145def cli_entry_point ():
142146 """Command line interface for interacting with SimulaQron."""
143147 pass
@@ -162,10 +166,10 @@ def version():
162166@cli_entry_point .command ()
163167@click .option (
164168 "--network-config-file" ,
165- help = f"Path to network config file. If not specified, uses "
166- f"./ { DEFAULT_SIMULAQRON_NETWORK_FILENAME } or ~/.simulaqron/{ DEFAULT_SIMULAQRON_NETWORK_FILENAME } " ,
169+ help = f"Path to network config file. If not specified, uses ./ { DEFAULT_SIMULAQRON_NETWORK_FILENAME } " # noqa: E131
170+ f"or ~/.simulaqron/{ DEFAULT_SIMULAQRON_NETWORK_FILENAME } " , # noqa: E131
167171 type = click .Path (exists = True , dir_okay = False , resolve_path = True , path_type = Path ),
168- default = LOCAL_NETWORK_SETTINGS
172+ default = get_default_network_config_file ()
169173)
170174@click .option (
171175 "--simulaqron-config-file" ,
@@ -189,51 +193,67 @@ def version():
189193)
190194def start (name : str , nodes : str , simulaqron_config_file : Path , network_config_file : Path ):
191195 """Starts a network with the given parameters or from config files."""
192- # if netconfig is None:
193- # network_config_file = get_default_network_config_file()
194196 # Checks the simulaqron config
195197 if not _path_exists (simulaqron_config_file ):
196- raise click .BadParameter (f"The given simulaqron config file '{ simulaqron_config_file } ' does not exist or it is a folder.\n "
197- "Please check the path given to the --simulaqron-config-file option." )
198+ raise click .BadOptionUsage (
199+ option_name = "simulaqron-config-file" ,
200+ message = f"The given simulaqron config file '{ simulaqron_config_file } ' does not exist or it "
201+ "is a folder.\n Please check the path given to the --simulaqron-config-file option." # noqa: E131
202+ )
198203 # Checks the network config
199204 if not _path_exists (network_config_file ):
200- raise click .BadParameter (f"The given network config file '{ network_config_file } ' does not exist or it is a folder.\n "
201- "Please check the path given to the --network-config-file option." )
205+ raise click .BadOptionUsage (
206+ option_name = "network-config-file" ,
207+ message = f"The given network config file '{ network_config_file } ' does not exist or it is a "
208+ "folder.\n Please check the path given to the --network-config-file option." # noqa: E131
209+ )
202210 # Load SimulaQron and network configs
203211 simulaqron_settings .read_from_file (simulaqron_config_file )
204212 network_config .read_from_file (network_config_file )
205213 # Check that the network name exists in the network configuration
206- if not name in network_config .networks :
207- raise click .BadParameter (f"The network '{ name } ' was not found in the network configuration file '{ network_config_file } '.\n "
208- f"Please check the name you passed in the --name option and try again." )
214+ if name not in network_config .networks :
215+ raise click .BadOptionUsage (
216+ option_name = "name" ,
217+ message = f"The network '{ name } ' was not found in the network configuration file " # noqa: E713
218+ f"'{ network_config_file } '.\n Please check the name you passed in the" # noqa: E131
219+ " --name option and try again." # noqa: E131
220+ )
209221 # Check that the nodes to start exist in the given network
210222 nodes = nodes .split ("," )
211223 if len (nodes ) <= 0 :
212- print ("The list of nodes to start is empty. Please check the list given in the --nodes argument." )
213- return
224+ raise click .BadOptionUsage (
225+ option_name = "nodes" ,
226+ message = "The list of nodes to start is empty. Please check the list given in "
227+ "the --nodes argument."
228+ )
214229 for node_to_start in nodes :
215- if not node_to_start in network_config .networks [name ]:
216- raise click .BadParameter (f"The node '{ node_to_start } ' was not found in the network named '{ name } 'specified in"
217- f" the configuration file '{ network_config_file } '.\n Please check the list of names you "
218- f"passed in the --nodes option and try again." )
230+ if node_to_start not in network_config .networks [name ]:
231+ raise click .BadOptionUsage (
232+ option_name = "nodes" ,
233+ message = f"The node '{ node_to_start } ' was not found in the network named " # noqa: E713
234+ f"'{ name } 'specified in the configuration file '{ network_config_file } '.\n " # noqa: E131
235+ "Please check the list of names you passed in the --nodes option and try again." # noqa: E131
236+ )
219237 # Check that there is no other network with the same name running
220238 pidfile = PID_FOLDER / f"simulaqron_network_{ name } .pid"
221239 if pidfile .exists ():
222- logging .warning ("Network with name %s is already running" , name )
223- logging .warning ("The pidfile for this network is located at %s" , pidfile )
224- return
240+ raise click .BadOptionUsage (
241+ option_name = "pidfile" ,
242+ message = f"Network with name { name } is already running.\n The pidfile for "
243+ f"this network is located at { pidfile } " # noqa: E131
244+ )
225245
226246 # Let's start the simulaqron daemon. We will pass the config file so it will be available
227247 # in the child process and load the same config
228- d = SimulaQronDaemon (pidfile = pidfile , name = name , nodes = nodes )
248+ d = SimulaQronDaemon (pidfile = pidfile , name = name , nodes = nodes , network_config_file = network_config_file )
229249 try :
230250 d .start ()
231251 except SystemExit as e :
232252 if e .code == exit .PIDFILE_INACCESSIBLE or \
233253 e .code == exit .DAEMONIZE_FAILED :
234254 logging .debug (f"Failed to launch Simulaqron Daemon. "
235255 f"Exit code reported by daemons: { e .code } " )
236- print ("Failed to launch SimulaQron Daemon. Aborted!" )
256+ raise click . BadParameter ("Failed to launch SimulaQron Daemon. Aborted!" )
237257
238258
239259###############
@@ -251,7 +271,7 @@ def stop(name: str):
251271 """Stops a network."""
252272 assert name is not None
253273 pidfile = PID_FOLDER / f"simulaqron_network_{ name } .pid"
254- logging .debug (f "Trying to open PIDfile" )
274+ logging .debug ("Trying to open PIDfile" )
255275 if not pidfile .exists ():
256276 logging .warning ("Network with name %s is not running" , name )
257277 return
@@ -289,7 +309,7 @@ def reset(force: bool):
289309 simulaqron_settings .default_settings ()
290310 simulaqron_settings .write_to_file (LOCAL_SIMULAQRON_SETTINGS )
291311 else :
292- print ("Aborting!" )
312+ raise click . ClickException ("Aborting!" )
293313
294314
295315###############
@@ -637,7 +657,7 @@ def get(network_name: str):
637657 try :
638658 nodes = network_config .get_node_names (network_name = network_name )
639659 except ValueError :
640- print (f"No network { network_name } " )
660+ raise click . BadParameter (f"No network { network_name } " )
641661 else :
642662 print (("{} " * len (nodes ))[:- 1 ].format (* nodes ))
643663
0 commit comments