-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
73 lines (70 loc) · 2.37 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pytest
import time
import sys
import json
import ast
import requests
import xml.etree.ElementTree as ET
@pytest.fixture(scope="module")
def device(request):
global d
n = getattr(request.module, "dev_name")
print(f'Setup: for {n}')
c = getattr(sys.modules[f"alpaca.{n.lower()}"], n) # Creates a device class by string name :-)
d = c('localhost:32323', 0) # Created an instance of the class
# d = c('[fe80::9927:65fc:e9e8:f33a%eth0]:32323', 0) # RPi 4 Ethernet to Windows OmniSim IPv6
#d.Connected = True
d.Connect()
while d.Connecting:
time.sleep(0.5)
print(f"Setup: Connected to OmniSim {n} OK")
return d
#
# Grabs the settings for the device from the OmniSim settings data *once*.
#
@pytest.fixture(scope="module")
def settings(request):
n = getattr(request.module, "dev_name").lower()
resp = requests.get(f'http://localhost:32323/simulator/v1/{n}/0/xmlprofile?ClientID=0&ClientTransactionID=0')
text = eval(resp.content)["Value"]
root = ET.ElementTree(ET.fromstring(text)).getroot()
s = {}
for i in root.iter("SettingsPair"):
k = i.find('Key').text
v = i.find('Value').text
try:
s[k] = ast.literal_eval(v) # Numerics
except:
try:
s[k] = json.loads(v) # Boolean strings from XML
except:
s[k] = v # Punt ... string
print(f"Setup: {n} OminSim Settings retrieved")
return s
@pytest.fixture(scope="module")
def disconn(request):
global d
yield
#d.Connected = False
d.Disconnect()
n = getattr(request.module, "dev_name")
print(f"Teardown: {n} Disconnected")
#
# Common function to get settings for @pytest.mark.skipif() decorators
#
def get_settings(device: str):
resp = requests.get(f'http://localhost:32323/simulator/v1/{device}/0/xmlprofile?ClientID=0&ClientTransactionID=0')
text = eval(resp.content)["Value"]
root = ET.ElementTree(ET.fromstring(text)).getroot()
s = {}
for i in root.iter("SettingsPair"):
k = i.find('Key').text
v = i.find('Value').text
try:
s[k] = ast.literal_eval(v) # Numerics
except:
try:
s[k] = json.loads(v) # Boolean strings from XML
except:
s[k] = v # Punt ... string
return s