Path: blob/main/tools/neteditTestFunctions/buildinternaltestdata.py
169673 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file buildinternaltestdata.py14# @author Pablo Alvarez Lopez15# @date May 20251617"""18This script generates thre header file with all data needed for internal tests19"""20from __future__ import absolute_import21from __future__ import print_function2223import ast24import os252627def flattenClassTree(node, scope=None, result=None, local_vars=None):28if scope is None:29scope = []30if result is None:31result = {}32if local_vars is None:33local_vars = {}3435for item in node.body:36if isinstance(item, ast.ClassDef):37scope.append(item.name)38flattenClassTree(item, scope, result, local_vars)39scope.pop()4041elif isinstance(item, ast.Assign):42for target in item.targets:43if isinstance(target, ast.Name):44key = '.'.join(scope + [target.id])45value_node = item.value46try:47# evaluate if os possible48value = eval_ast(value_node, local_vars)49result[key] = value50local_vars[target.id] = value51except Exception:52result[key] = None53return result545556def eval_ast(node, local_vars):57if isinstance(node, ast.Constant):58return node.value59elif isinstance(node, ast.Name):60return local_vars.get(node.id)61elif isinstance(node, ast.BinOp):62left = eval_ast(node.left, local_vars)63right = eval_ast(node.right, local_vars)64return eval_binop(node.op, left, right)65elif isinstance(node, ast.UnaryOp):66operand = eval_ast(node.operand, local_vars)67return eval_unaryop(node.op, operand)68else:69raise ValueError("Unsupported AST node: %s" % ast.dump(node))707172def eval_binop(op, left, right):73if isinstance(op, ast.Add):74return left + right75elif isinstance(op, ast.Sub):76return left - right77elif isinstance(op, ast.Mult):78return left * right79elif isinstance(op, ast.Div):80return left / right81elif isinstance(op, ast.FloorDiv):82return left // right83elif isinstance(op, ast.Mod):84return left % right85elif isinstance(op, ast.Pow):86return left ** right87else:88raise ValueError("Unsupported binary operator: %s" % op)899091def eval_unaryop(op, operand):92if isinstance(op, ast.UAdd):93return +operand94elif isinstance(op, ast.USub):95return -operand96else:97raise ValueError("Unsupported unary operator: %s" % op)9899100def parseIntFile(outputFolder, file):101# read python file102pythonFile = "./enums/" + file + ".py"103with open(pythonFile, "r", encoding="utf-8") as f:104tree = ast.parse(f.read(), filename=pythonFile)105# flatten data106data = flattenClassTree(tree)107# write header file108outputFile = outputFolder + "/" + file + ".txt"109with open(outputFile, "w", encoding="utf-8") as f:110for key, value in sorted(data.items()):111f.write('netedit.%s %s\n' % (key, value))112113114def parsePositionFile(outputFolder, file):115# read python file116pythonFile = "./enums/" + file + ".py"117with open(pythonFile, "r", encoding="utf-8") as f:118tree = ast.parse(f.read(), filename=pythonFile)119# flatten data120data = flattenClassTree(tree)121# write header file122outputFile = outputFolder + "/" + file + ".txt"123with open(outputFile, "w", encoding="utf-8") as f:124for key in sorted(data.keys()):125if key.endswith(".x"):126keyShorted = key[:-2]127x = data[keyShorted + ".x"]128y = data[keyShorted + ".y"]129f.write('netedit.%s %s %s\n' % (keyShorted, x, y))130131132def parseRouteFile(outputFolder, file):133# read python file134pythonFile = "./enums/" + file + ".py"135with open(pythonFile, "r", encoding="utf-8") as f:136tree = ast.parse(f.read(), filename=pythonFile)137# flatten data138data = flattenClassTree(tree)139# write header file140outputFile = outputFolder + "/" + file + ".txt"141with open(outputFile, "w", encoding="utf-8") as f:142for key in sorted(data.keys()):143if key.endswith(".up"):144keyShorted = key[:-3]145up = data[keyShorted + ".up"]146down = data[keyShorted + ".down"]147left = data[keyShorted + ".left"]148right = data[keyShorted + ".right"]149f.write('netedit.%s %s %s %s %s\n' % (keyShorted, up, down, left, right))150151152# main153if __name__ == "__main__":154outputFolder = "./../../data/tests"155# check if create folder156if not os.path.exists(outputFolder):157os.mkdir(outputFolder)158# calculate files for data folder159parseIntFile(outputFolder, "attributesEnum")160parseIntFile(outputFolder, "contextualMenuOperations")161parsePositionFile(outputFolder, "viewPositions")162parseRouteFile(outputFolder, "movements")163164165