From f50a11e3e84ecf9bd97515f5f08576a4f37dca1c Mon Sep 17 00:00:00 2001 From: Thibaud Date: Thu, 13 Dec 2018 22:02:54 +0100 Subject: [PATCH] Refactor code --- configurator/configurator.py | 37 +++++++++++++++------------- configurator/templates/simul.polycfg | 1 - 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/configurator/configurator.py b/configurator/configurator.py index eb646b9..5b2fb3d 100644 --- a/configurator/configurator.py +++ b/configurator/configurator.py @@ -26,34 +26,34 @@ vehicle_classes = { def load_netconvert_template(osm_input, out_name): - tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg')) - root = tree.getroot() + netconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg')) + root = netconfig.getroot() root.find('input/osm-files').set('value', osm_input) root.find('output/output-file').set('value', f'{out_name}.net.xml') root.find('report/log').set('value', f'{out_name}.netconvert.log') - return tree + return netconfig def load_polyconvert_template(osm_file, type_file, scenario_name): - tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg')) - root = tree.getroot() + polyconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg')) + root = polyconfig.getroot() root.find('input/osm-files').set('value', osm_file) root.find('input/net-file').set('value', f'{scenario_name}.net.xml') root.find('input/type-file').set('value', type_file) root.find('output/output-file').set('value', f'{scenario_name}.poly.xml') root.find('report/log').set('value', f'{scenario_name}.polyconvert.log') - return tree + return polyconfig -def load_sumoconfig_template(simulation_name, routefiles=None): +def load_sumoconfig_template(simulation_name, routefiles=()): routefiles = routefiles or (f'{simulation_name}.rou.xml',) - tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg')) - root = tree.getroot() + sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg')) + root = sumoconfig.getroot() root.find('input/net-file').set('value', f'{simulation_name}.net.xml') - root.find('input/route-files').set('value', ','.join(f for f in routefiles)) + root.find('input/route-files').set('value', ','.join(routefiles)) root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml') root.find('report/log').set('value', f'{simulation_name}.log') - return tree + return sumoconfig def generate_scenario(osm_file, out_path, scenario_name): @@ -61,12 +61,14 @@ def generate_scenario(osm_file, out_path, scenario_name): poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name) with tempfile.TemporaryDirectory() as tmpdirname: + # Generate POLYCONVERT and NETCONVERT configuration netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg') polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg') net_template.write(netconfig) poly_template.write(polyconfig) # Copy typemaps to tempdir shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap')) + # Call POLYCONVERT and NETCONVERT polyconvert_cmd = ['polyconvert', '-c', polyconfig] netconvertcmd = ['netconvert', '-c', netconfig] subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -76,19 +78,20 @@ def generate_scenario(osm_file, out_path, scenario_name): shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns) -def generate_mobility(path, name): - netfile = os.path.join(path, f'{name}.net.xml') - output = os.path.join(path, f'{name}.trips.xml') +def generate_mobility(out_path, name): + netfile = f'{name}.net.xml' + netpath = os.path.join(out_path, netfile) + output = os.path.join(out_path, f'{name}.trips.xml') routefiles = [] end_time = 200 classes = ('passenger', 'bus') for veh_class in classes: # simname.bus.rou.xml, simname.passenger.rou.xml, ... routefile = f'{name}.{veh_class}.rou.xml' - routepath = os.path.join(path, routefile) + routepath = os.path.join(out_path, routefile) routefiles.append(routefile) options = { - '--net-file': netfile, + '--net-file': netpath, '--output-trip-file': output, '--route-file': routepath, '-e': end_time @@ -104,7 +107,7 @@ def generate_random_trips(flags, options): def generate_sumo_configuration(routefiles, path, scenario_name): - sumo_template = load_sumoconfig_template(routefiles, scenario_name) + sumo_template = load_sumoconfig_template(scenario_name, routefiles=routefiles) sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg')) diff --git a/configurator/templates/simul.polycfg b/configurator/templates/simul.polycfg index 909c415..639ca2f 100644 --- a/configurator/templates/simul.polycfg +++ b/configurator/templates/simul.polycfg @@ -5,7 +5,6 @@ -