forked from joanpc/XplaneNoaaWeather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyDref.py
99 lines (84 loc) · 3.04 KB
/
EasyDref.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from XPLMDataAccess import *
class EasyDref:
'''
Easy Dataref access
Copyright (C) 2011 Joan Perez i Cauhe
'''
def __init__(self, dataref, type = "float"):
# Clear dataref
dataref = dataref.strip()
self.isarray, dref = False, False
if ('"' in dataref):
dref = dataref.split('"')[1]
dataref = dataref[dataref.rfind('"')+1:]
if ('(' in dataref):
# Detect embedded type, and strip it from dataref
type = dataref[dataref.find('(')+1:dataref.find(')')]
dataref = dataref[:dataref.find('(')] + dataref[dataref.find(')')+1:]
if ('[' in dataref):
# We have an array
self.isarray = True
range = dataref[dataref.find('[')+1:dataref.find(']')].split(':')
dataref = dataref[:dataref.find('[')]
if (len(range) < 2):
range.append(range[0])
self.initArrayDref(range[0], range[1], type)
elif (type == "int"):
self.dr_get = XPLMGetDatai
self.dr_set = XPLMSetDatai
self.cast = int
elif (type == "float"):
self.dr_get = XPLMGetDataf
self.dr_set = XPLMSetDataf
self.cast = float
elif (type == "double"):
self.dr_get = XPLMGetDatad
self.dr_set = XPLMSetDatad
self.cast = float
else:
print "ERROR: invalid DataRef type", type
if dref: dataref = dref
self.DataRef = XPLMFindDataRef(dataref)
if self.DataRef == False:
print "Can't find " + dataref + " DataRef"
def initArrayDref(self, first, last, type):
self.index = int(first)
self.count = int(last) - int(first) +1
self.last = int(last)
if (type == "int"):
self.rget = XPLMGetDatavi
self.rset = XPLMSetDatavi
self.cast = int
elif (type == "float"):
self.rget = XPLMGetDatavf
self.rset = XPLMSetDatavf
self.cast = float
elif (type == "bit"):
self.rget = XPLMGetDatab
self.rset = XPLMSetDatab
self.cast = float
else:
print "ERROR: invalid DataRef type", type
pass
def set(self, value):
if (self.isarray):
self.rset(self.DataRef, value, self.index, len(value))
else:
self.dr_set(self.DataRef, self.cast(value))
def get(self):
if (self.isarray):
list = []
self.rget(self.DataRef, list, self.index, self.count)
return list
else:
return self.dr_get(self.DataRef)
def __getattr__(self, name):
if name == 'value':
return self.get()
else:
raise AttributeError
def __setattr__(self, name, value):
if name == 'value':
self.set(value)
else:
self.__dict__[name] = value