]> git.mxchange.org Git - flightgear.git/blob - scripts/python/FlightGear.py
Updated to document the new 3d positional tags that are available for
[flightgear.git] / scripts / python / FlightGear.py
1 from telnetlib import Telnet
2 import sys
3 import socket
4 import re
5 from string import split, join
6 import time
7
8 __all__ = ["FlightGear"]
9
10 CRLF = '\r\n'
11
12 class FGTelnet(Telnet):
13     def __init__(self,host,port):
14         Telnet.__init__(self,host,port)
15         self.prompt = []
16         self.prompt.append( re.compile('/[^>]*> ') )
17         self.timeout = 5
18         #Telnet.set_debuglevel(self,2)
19
20     def help(self):
21         return
22
23     def ls(self,dir=None):
24         """
25         Returns a list of properties.
26         """
27         if dir == None:
28             self._putcmd('ls')
29         else:
30             self._putcmd('ls %s' % dir )
31         return self._getresp()
32
33     def dump(self):
34         """Dump current state as XML."""
35         self._putcmd('dump')
36         return self._getresp()
37
38     def cd(self, dir):
39         """Change directory."""
40         self._putcmd('cd ' + dir)
41         self._getresp()
42         return
43
44     def pwd(self):
45         """Display current path."""
46         self._putcmd('pwd')
47         return self._getresp()
48
49     def get(self,var):
50         """Retrieve the value of a parameter."""
51         self._putcmd('get %s' % var )
52         return self._getresp()
53
54     def set(self,var,value):
55         """Set variable to a new value"""
56         self._putcmd('set %s %s' % (var,value))
57         self._getresp() # Discard response
58
59     def quit(self):
60         """Terminate connection"""
61         self._putcmd('quit')
62         self.close()
63         return
64
65     # Internal: send one command to FlightGear
66     def _putcmd(self,cmd):
67         cmd = cmd + CRLF;
68         Telnet.write(self, cmd)
69         return
70
71     # Internal: get a response from FlightGear
72     def _getresp(self):
73         (i,match,resp) = Telnet.expect(self, self.prompt, self.timeout)
74         # Remove the terminating prompt.
75         # Everything preceding it is the response.
76         return split(resp, '\n')[:-1]
77
78 class FlightGear:
79     """FlightGear interface class.
80
81     An instance of this class represents a connection to a FlightGear telnet
82     server.
83
84     Properties are accessed using a dictionary style interface:
85     For example:
86
87     # Connect to flightgear telnet server.
88     fg = FlightGear('myhost', 5500)
89     # parking brake on
90     fg['/controls/gear/brake-parking'] = 1
91     # Get current heading
92     heading = fg['/orientation/heading-deg']
93
94     Other non-property related methods
95     """
96
97     def __init__( self, host = 'localhost', port = 5500 ):
98         try:
99             self.telnet = FGTelnet(host,port)
100         except socket.error, msg:
101             self.telnet = None
102             raise socket.error, msg
103         
104     def __del__(self):
105         # Ensure telnet connection is closed cleanly.
106         self.quit();
107
108     def __getitem__(self,key):
109         """Get a FlightGear property value.
110         Where possible the value is converted to the equivalent Python type.
111         """
112         s = self.telnet.get(key)[0]
113         match = re.compile( '[^=]*=\s*\'([^\']*)\'\s*([^\r]*)\r').match( s )
114         if not match:
115             return None
116         value,type = match.groups()
117         #value = match.group(1)
118         #type = match.group(2)
119         if value == '':
120             return None
121
122         if type == '(double)':
123             return float(value)
124         elif type == '(int)':
125             return int(value)
126         elif type == '(bool)':
127             if value == 'true':
128                 return 1
129             else:
130                 return 0
131         else:
132             return value
133         
134     def __setitem__(self, key, value):
135         """Set a FlightGear property value."""
136         self.telnet.set( key, value )
137
138     def quit(self):
139         """Close the telnet connection to FlightGear."""
140         if self.telnet:
141             self.telnet.quit()
142             self.telnet = None