aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/share/python
diff options
context:
space:
mode:
authorDr Scofield2008-07-30 08:09:45 +0000
committerDr Scofield2008-07-30 08:09:45 +0000
commit92e7f88a30e0fb99dabd56a6775251193b62cfa6 (patch)
treef448bf5e0d702add764cc000db7710602630b4ca /share/python
parentPrevent acks from being appended to viewer effect packets, since (diff)
downloadopensim-SC_OLD-92e7f88a30e0fb99dabd56a6775251193b62cfa6.zip
opensim-SC_OLD-92e7f88a30e0fb99dabd56a6775251193b62cfa6.tar.gz
opensim-SC_OLD-92e7f88a30e0fb99dabd56a6775251193b62cfa6.tar.bz2
opensim-SC_OLD-92e7f88a30e0fb99dabd56a6775251193b62cfa6.tar.xz
cleanups and refactoring to make it more readable.
Diffstat (limited to 'share/python')
-rw-r--r--share/python/matrix/matrix.py159
1 files changed, 106 insertions, 53 deletions
diff --git a/share/python/matrix/matrix.py b/share/python/matrix/matrix.py
index ea36cc9..be7ec25 100644
--- a/share/python/matrix/matrix.py
+++ b/share/python/matrix/matrix.py
@@ -60,19 +60,10 @@ secondlife client lives on your box. to generate that file, simply run
60 60
61 ''' 61 '''
62 62
63def ParseOptions():
64 '''Parse the command line options and setup options.
65 '''
63 66
64reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+):// # scheme
65 ((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)? # avatar name and password (optional)
66 (?P<host>[^:/]+)(:(?P<port>\d+))? # host, port (optional)
67 (?P<path>/.*) # path
68 $''', re.IGNORECASE | re.VERBOSE)
69reLOC = re.compile(r'''^/(?P<region>[^/]+)/ # region name
70 (?P<x>\d+)/ # X position
71 (?P<y>\d+)/ # Y position
72 (?P<z>\d+) # Z position
73 ''', re.IGNORECASE | re.VERBOSE)
74
75if __name__ == '__main__':
76 parser = optparse.OptionParser() 67 parser = optparse.OptionParser()
77 parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG') 68 parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG')
78 parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT') 69 parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT')
@@ -84,13 +75,20 @@ if __name__ == '__main__':
84 longHelp() 75 longHelp()
85 sys.exit(0) 76 sys.exit(0)
86 77
78 return options
79
80def ParseConfig(options):
81 '''Ensure configuration exists and parse it.
82 '''
87 # 83 #
88 # we are using ~/.matrixcfg to store the location of the secondlife client 84 # we are using ~/.matrixcfg to store the location of the
85 # secondlife client, os.path.normpath and os.path.expanduser
86 # should make sure that we are fine cross-platform
89 # 87 #
90 if not options.config: 88 if not options.config:
91 options.config = '~/.matrixcfg' 89 options.config = '~/.matrixcfg'
92 90
93 cfgPath = os.path.expanduser(options.config) 91 cfgPath = os.path.normpath(os.path.expanduser(options.config))
94 92
95 # 93 #
96 # if ~/.matrixcfg does not exist we are in trouble... 94 # if ~/.matrixcfg does not exist we are in trouble...
@@ -118,20 +116,31 @@ if __name__ == '__main__':
118 config.write(cfg) 116 config.write(cfg)
119 cfg.close() 117 cfg.close()
120 118
121 client = config.get('secondlife', 'client') 119 return config.get('secondlife', 'client')
122 120
123 121
124 # 122#
125 # sanity check: URI supplied? 123# regex: parse a URI
126 # 124#
127 if not sys.argv: 125reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+):// # scheme
128 print 'missing opensim/matrix URI' 126 ((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)? # avatar name and password (optional)
129 sys.exit(1) 127 (?P<host>[^:/]+)(:(?P<port>\d+))? # host, port (optional)
128 (?P<path>/.*) # path
129 $''', re.IGNORECASE | re.VERBOSE)
130
131#
132# regex: parse path as location
133#
134reLOC = re.compile(r'''^/(?P<region>[^/]+)/ # region name
135 (?P<x>\d+)/ # X position
136 (?P<y>\d+)/ # Y position
137 (?P<z>\d+) # Z position
138 ''', re.IGNORECASE | re.VERBOSE)
139
140def ParseUri(uri):
141 '''Parse a URI and return its constituent parts.
142 '''
130 143
131 #
132 # parse URI and extract scheme. host, port?, avatar?, password?
133 #
134 uri = sys.argv.pop()
135 match = reURI.match(uri) 144 match = reURI.match(uri)
136 if not match or not match.group('scheme') or not match.group('host'): 145 if not match or not match.group('scheme') or not match.group('host'):
137 print 'hmm... cannot parse URI %s, giving up' % uri 146 print 'hmm... cannot parse URI %s, giving up' % uri
@@ -144,21 +153,32 @@ if __name__ == '__main__':
144 password = match.group('password') 153 password = match.group('password')
145 path = match.group('path') 154 path = match.group('path')
146 155
147 # 156 return (scheme, host, port, avatar, password, path)
148 # sanity check: matrix: or opensim: scheme? 157
149 # 158def ParsePath(path):
150 if scheme != 'matrix' and scheme != 'opensim': 159 '''Try and parse path as /region/X/Y/Z.
151 print 'hmm...unknown scheme %s, calling it a day' % scheme 160 '''
161
162 loc = None
163 match = reLOC.match(path)
164 if match:
165 loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'),
166 int(match.group('x')),
167 int(match.group('y')),
168 int(match.group('z')))
169 return loc
170
171
172def GetGridInfo(host, port):
173 '''Invoke /get_grid_info on target grid and obtain additional parameters
174 '''
152 175
153 #
154 # get grid info from OpenSim server
155 #
156 gridname = None 176 gridname = None
157 gridnick = None 177 gridnick = None
158 login = None 178 login = None
159 welcome = None 179 welcome = None
160 economy = None 180 economy = None
161 181
162 # 182 #
163 # construct GridInfo URL 183 # construct GridInfo URL
164 # 184 #
@@ -178,19 +198,16 @@ if __name__ == '__main__':
178 login = gridInfoXml.findtext('/login') 198 login = gridInfoXml.findtext('/login')
179 welcome = gridInfoXml.findtext('/welcome') 199 welcome = gridInfoXml.findtext('/welcome')
180 economy = gridInfoXml.findtext('/economy') 200 economy = gridInfoXml.findtext('/economy')
201 authenticator = gridInfoXml.findtext('/authenticator')
181 202
182 except urllib2.URLError: 203 except urllib2.URLError:
183 print 'oops, failed to retrieve grid info, proceeding with guestimates...' 204 print 'oops, failed to retrieve grid info, proceeding with guestimates...'
184 205
185 # 206 return (gridname, gridnick, login, welcome, economy)
186 # fallback: use supplied uri in case GridInfo drew a blank
187 #
188 if not login: login = uri
189 207
190 # 208
191 # ok, got everything, now construct the command line 209def StartClient(client, nick, login, welcome, economy, avatar, password, location):
192 # 210 clientArgs = [ client ]
193 clientArgs = ['matrix: %s' % gridnick]
194 clientArgs += ['-loginuri', login] 211 clientArgs += ['-loginuri', login]
195 212
196 if welcome: clientArgs += ['-loginpage', welcome] 213 if welcome: clientArgs += ['-loginpage', welcome]
@@ -201,19 +218,55 @@ if __name__ == '__main__':
201 clientArgs += urllib.unquote(avatar).split() 218 clientArgs += urllib.unquote(avatar).split()
202 clientArgs += [password] 219 clientArgs += [password]
203 220
204 # 221 if location:
205 # take a closer look at path: if it's a /region/X/Y/Z pattern, use 222 clientArgs += [location]
206 # it as the "SLURL
207 #
208 match = reLOC.match(path)
209 if match:
210 loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'),
211 int(match.group('x')),
212 int(match.group('y')),
213 int(match.group('z')))
214 clientArgs += [loc]
215 223
216 # 224 #
217 # all systems go 225 # all systems go
218 # 226 #
219 os.execv(client, clientArgs) 227 os.execv(client, clientArgs)
228
229
230if __name__ == '__main__':
231 #
232 # parse command line options and deal with help requests
233 #
234 options = ParseOptions()
235 client = ParseConfig(options)
236
237 #
238 # sanity check: URI supplied?
239 #
240 if not sys.argv:
241 print 'missing opensim/matrix URI'
242 sys.exit(1)
243
244 #
245 # parse URI and extract scheme. host, port?, avatar?, password?
246 #
247 uri = sys.argv.pop()
248 (scheme, host, port, avatar, password, path) = ParseUri(uri)
249
250 #
251 # sanity check: matrix: or opensim: scheme?
252 #
253 if scheme != 'matrix' and scheme != 'opensim':
254 print 'hmm...unknown scheme %s, calling it a day' % scheme
255
256 #
257 # get grid info from OpenSim server
258 #
259 (gridname, gridnick, login, welcome, economy) = GetGridInfo(host, port)
260
261 #
262 # fallback: use supplied uri in case GridInfo drew a blank
263 #
264 if not login: login = uri
265
266 #
267 # take a closer look at path: if it's a /region/X/Y/Z pattern, use
268 # it as the "SLURL
269 #
270 location = ParsePath(path)
271 StartClient(client, gridnick, login, welcome, economy, avatar, password, location)
272