Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/neteditTestFunctions/setup.py
193674 views
1
# -*- coding: utf-8 -*-
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2026 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file setup.py
15
# @author Pablo Alvarez Lopez
16
# @date 28-05-25
17
18
# imports
19
import os
20
import sys
21
import time
22
import subprocess
23
import pyautogui
24
import pyperclip
25
26
from .input.keyboard import typeKey, keyRelease
27
from .constants import NETEDIT_APP, TEXTTEST_SANDBOX, REFERENCE_PNG
28
from .constants import DELAY_REFERENCE, DELAY_MOUSE_MOVE, DELAY_MOUSE_CLICK
29
30
31
def Popen(extraParameters):
32
"""
33
@brief open netedit
34
"""
35
# set the default parameters of Netedit
36
neteditCall = [NETEDIT_APP]
37
38
# check if a netedit config must be loaded
39
if os.path.exists(os.path.join(TEXTTEST_SANDBOX, "netedit_B.netecfg")):
40
neteditCall += ['-c netedit_B.netecfg']
41
elif os.path.exists(os.path.join(TEXTTEST_SANDBOX, "netedit_A.netecfg")):
42
neteditCall += ['-c netedit_A.netecfg']
43
elif os.path.exists(os.path.join(TEXTTEST_SANDBOX, "sumo_B.netecfg")):
44
neteditCall += ['-c sumo_B.netecfg']
45
elif os.path.exists(os.path.join(TEXTTEST_SANDBOX, "sumo_A.netecfg")):
46
neteditCall += ['-c sumo_A.netecfg']
47
48
# add extra parameters
49
neteditCall += extraParameters
50
51
# return a subprocess with Netedit
52
return subprocess.Popen(neteditCall, env=os.environ, stdout=sys.stdout, stderr=sys.stderr, cwd=TEXTTEST_SANDBOX)
53
54
55
def getReferenceMatch(neProcess, makeScrenshot):
56
"""
57
@brief obtain reference referencePosition (pink square)
58
"""
59
# show information
60
print("Finding reference")
61
# make a screenshot
62
errorScreenshot = pyautogui.screenshot()
63
try:
64
# wait for reference
65
time.sleep(DELAY_REFERENCE)
66
# capture screen and search reference
67
positionOnScreen = pyautogui.locateOnScreen(REFERENCE_PNG, minSearchTime=3, confidence=0.95)
68
except Exception as e:
69
# we cannot specify the exception here because some versions of pyautogui use one and some don't
70
print(e)
71
positionOnScreen = None
72
# make a screenshot
73
errorScreenshot = pyautogui.screenshot()
74
# check if pos was found
75
if positionOnScreen:
76
# adjust position to center
77
referencePosition = (positionOnScreen[0] + 16, positionOnScreen[1] + 16)
78
# break loop
79
print("TestFunctions: 'reference.png' found. Position: " +
80
str(referencePosition[0]) + " - " + str(referencePosition[1]))
81
# check that position is consistent (due scaling)
82
if referencePosition != (304, 168):
83
print("TestFunctions: Position of 'reference.png' isn't consistent")
84
# click over position
85
pyautogui.moveTo(referencePosition)
86
# wait
87
time.sleep(DELAY_MOUSE_MOVE)
88
# press i for inspect mode
89
typeKey("i")
90
# click over position (used to center view in window)
91
pyautogui.click(button='left')
92
# wait after every operation
93
time.sleep(DELAY_MOUSE_CLICK)
94
# return reference position
95
return referencePosition
96
# referente not found, then write screenshot
97
if (makeScrenshot):
98
errorScreenshot.saveExistent("errorScreenshot.png")
99
# kill netedit process
100
neProcess.kill()
101
# print debug information
102
sys.exit("TestFunctions: Killed Netedit process. 'reference.png' not found")
103
104
105
def setupAndStart(extraParameters=[], makeScrenshot=True):
106
"""
107
@brief setup and start netedit
108
"""
109
if os.name == "posix":
110
# to work around non working gtk clipboard
111
pyperclip.set_clipboard("xclip")
112
# Open Netedit
113
neteditProcess = Popen(extraParameters)
114
# atexit.register(quit, neteditProcess, False, False)
115
# print debug information
116
print("TestFunctions: Netedit opened successfully")
117
# Release all keys
118
keyRelease("shift")
119
keyRelease("control")
120
keyRelease("alt")
121
# Wait for Netedit reference
122
return neteditProcess, getReferenceMatch(neteditProcess, makeScrenshot)
123
124