diff --git a/gnpy/tools/cli_examples.py b/gnpy/tools/cli_examples.py index f220120d..0ed75800 100644 --- a/gnpy/tools/cli_examples.py +++ b/gnpy/tools/cli_examples.py @@ -29,7 +29,7 @@ from gnpy.topology.request import (ResultElement, jsontocsv, compute_path_dsjctn deduplicate_disjunctions, compute_path_with_disjunction, PathRequest, compute_constrained_path, propagate2) from gnpy.topology.spectrum_assignment import build_oms_list, pth_assign_spectrum -from gnpy.tools.json_io import load_equipment, load_network, load_json, load_requests, save_network, requests_from_json, disjunctions_from_json +from gnpy.tools.json_io import load_equipment, load_network, load_json, load_requests, save_network, requests_from_json, disjunctions_from_json from gnpy.tools.plots import plot_baseline, plot_results _logger = logging.getLogger(__name__) @@ -98,6 +98,7 @@ def transmission_main_example(args=None): default=_examples_dir / 'edfa_example_network.json') parser.add_argument('source', nargs='?', help='source node') parser.add_argument('destination', nargs='?', help='destination node') + parser.add_argument('--save-network', type=Path, help='Save the network as a JSON file') args = parser.parse_args(args if args is not None else sys.argv[1:]) _setup_logging(args) @@ -255,7 +256,8 @@ def transmission_main_example(args=None): }) write_csv(result_dicts, 'simulation_result.csv') - save_network(args.filename, network) + if args.save_network is not None: + save_network(network, args.save_network) if args.show_channels: print('\nThe total SNR per channel at the end of the line is:') @@ -317,6 +319,7 @@ def path_requests_run(args=None): parser.add_argument('-v', '--verbose', action='count', default=0, help='increases verbosity for each occurence') parser.add_argument('-o', '--output', type=Path) + parser.add_argument('--save-network', type=Path, help='Save the network as a JSON file') args = parser.parse_args(args if args is not None else sys.argv[1:]) _setup_logging(args) @@ -336,7 +339,8 @@ def path_requests_run(args=None): p_total_db = p_db + lin2db(automatic_nch(equipment['SI']['default'].f_min, equipment['SI']['default'].f_max, equipment['SI']['default'].spacing)) build_network(network, equipment, p_db, p_total_db) - save_network(args.network_filename, network) + if args.save_network is not None: + save_network(network, args.save_network) oms_list = build_oms_list(network, equipment) try: diff --git a/gnpy/tools/json_io.py b/gnpy/tools/json_io.py index 2f97d006..648cfdf7 100644 --- a/gnpy/tools/json_io.py +++ b/gnpy/tools/json_io.py @@ -316,10 +316,13 @@ def load_network(filename, equipment, name_matching=False): return network_from_json(json_data, equipment) -def save_network(filename, network): - filename_output = path.splitext(filename)[0] + '_auto_design.json' - json_data = network_to_json(network) - save_json(json_data, filename_output) +def save_network(network: DiGraph, filename: str): + '''Dump the network into a JSON file + + :param network: network to work on + :param filename: file to write to + ''' + save_json(network_to_json(network), filename) def _cls_for(equipment_type): diff --git a/tests/test_parser.py b/tests/test_parser.py index ad3903b9..1479101b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -73,7 +73,7 @@ def test_excel_json_generation(xls_input, expected_json_output): DATA_DIR / 'testTopology.xls': DATA_DIR / 'testTopology_auto_design_expected.json', }.items()) -def test_auto_design_generation_fromxlsgainmode(xls_input, expected_json_output): +def test_auto_design_generation_fromxlsgainmode(tmpdir, xls_input, expected_json_output): """ tests generation of topology json test that the build network gives correct results in gain mode """ @@ -88,9 +88,8 @@ def test_auto_design_generation_fromxlsgainmode(xls_input, expected_json_output) p_total_db = p_db + lin2db(automatic_nch(equipment['SI']['default'].f_min, equipment['SI']['default'].f_max, equipment['SI']['default'].spacing)) build_network(network, equipment, p_db, p_total_db) - save_network(xls_input, network) - - actual_json_output = xls_input.with_name(xls_input.stem + '_auto_design').with_suffix('.json') + actual_json_output = tmpdir / xls_input.with_name(xls_input.stem + '_auto_design').with_suffix('.json').name + save_network(network, actual_json_output) with open(actual_json_output, encoding='utf-8') as f: actual = load(f) @@ -116,7 +115,7 @@ def test_auto_design_generation_fromxlsgainmode(xls_input, expected_json_output) DATA_DIR / 'testTopology_auto_design_expected.json': DATA_DIR / 'testTopology_auto_design_expected.json', }.items()) -def test_auto_design_generation_fromjson(json_input, expected_json_output): +def test_auto_design_generation_fromjson(tmpdir, json_input, expected_json_output): """test that autodesign creates same file as an input file already autodesigned """ equipment = load_equipment(eqpt_filename) @@ -130,9 +129,8 @@ def test_auto_design_generation_fromjson(json_input, expected_json_output): p_total_db = p_db + lin2db(automatic_nch(equipment['SI']['default'].f_min, equipment['SI']['default'].f_max, equipment['SI']['default'].spacing)) build_network(network, equipment, p_db, p_total_db) - save_network(json_input, network) - - actual_json_output = json_input.with_name(json_input.stem + '_auto_design').with_suffix('.json') + actual_json_output = tmpdir / json_input.with_name(json_input.stem + '_auto_design').with_suffix('.json').name + save_network(network, actual_json_output) with open(actual_json_output, encoding='utf-8') as f: actual = load(f)