cleanups and refactoring to make it more readable.
parent
c80493ed4d
commit
92e7f88a30
|
@ -60,19 +60,10 @@ secondlife client lives on your box. to generate that file, simply run
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def ParseOptions():
|
||||||
|
'''Parse the command line options and setup options.
|
||||||
|
'''
|
||||||
|
|
||||||
reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+):// # scheme
|
|
||||||
((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)? # avatar name and password (optional)
|
|
||||||
(?P<host>[^:/]+)(:(?P<port>\d+))? # host, port (optional)
|
|
||||||
(?P<path>/.*) # path
|
|
||||||
$''', re.IGNORECASE | re.VERBOSE)
|
|
||||||
reLOC = re.compile(r'''^/(?P<region>[^/]+)/ # region name
|
|
||||||
(?P<x>\d+)/ # X position
|
|
||||||
(?P<y>\d+)/ # Y position
|
|
||||||
(?P<z>\d+) # Z position
|
|
||||||
''', re.IGNORECASE | re.VERBOSE)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG')
|
parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG')
|
||||||
parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT')
|
parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT')
|
||||||
|
@ -84,13 +75,20 @@ if __name__ == '__main__':
|
||||||
longHelp()
|
longHelp()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
return options
|
||||||
|
|
||||||
|
def ParseConfig(options):
|
||||||
|
'''Ensure configuration exists and parse it.
|
||||||
|
'''
|
||||||
#
|
#
|
||||||
# we are using ~/.matrixcfg to store the location of the secondlife client
|
# we are using ~/.matrixcfg to store the location of the
|
||||||
|
# secondlife client, os.path.normpath and os.path.expanduser
|
||||||
|
# should make sure that we are fine cross-platform
|
||||||
#
|
#
|
||||||
if not options.config:
|
if not options.config:
|
||||||
options.config = '~/.matrixcfg'
|
options.config = '~/.matrixcfg'
|
||||||
|
|
||||||
cfgPath = os.path.expanduser(options.config)
|
cfgPath = os.path.normpath(os.path.expanduser(options.config))
|
||||||
|
|
||||||
#
|
#
|
||||||
# if ~/.matrixcfg does not exist we are in trouble...
|
# if ~/.matrixcfg does not exist we are in trouble...
|
||||||
|
@ -118,20 +116,31 @@ if __name__ == '__main__':
|
||||||
config.write(cfg)
|
config.write(cfg)
|
||||||
cfg.close()
|
cfg.close()
|
||||||
|
|
||||||
client = config.get('secondlife', 'client')
|
return config.get('secondlife', 'client')
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# sanity check: URI supplied?
|
# regex: parse a URI
|
||||||
#
|
#
|
||||||
if not sys.argv:
|
reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+):// # scheme
|
||||||
print 'missing opensim/matrix URI'
|
((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)? # avatar name and password (optional)
|
||||||
sys.exit(1)
|
(?P<host>[^:/]+)(:(?P<port>\d+))? # host, port (optional)
|
||||||
|
(?P<path>/.*) # path
|
||||||
|
$''', re.IGNORECASE | re.VERBOSE)
|
||||||
|
|
||||||
|
#
|
||||||
|
# regex: parse path as location
|
||||||
|
#
|
||||||
|
reLOC = re.compile(r'''^/(?P<region>[^/]+)/ # region name
|
||||||
|
(?P<x>\d+)/ # X position
|
||||||
|
(?P<y>\d+)/ # Y position
|
||||||
|
(?P<z>\d+) # Z position
|
||||||
|
''', re.IGNORECASE | re.VERBOSE)
|
||||||
|
|
||||||
|
def ParseUri(uri):
|
||||||
|
'''Parse a URI and return its constituent parts.
|
||||||
|
'''
|
||||||
|
|
||||||
#
|
|
||||||
# parse URI and extract scheme. host, port?, avatar?, password?
|
|
||||||
#
|
|
||||||
uri = sys.argv.pop()
|
|
||||||
match = reURI.match(uri)
|
match = reURI.match(uri)
|
||||||
if not match or not match.group('scheme') or not match.group('host'):
|
if not match or not match.group('scheme') or not match.group('host'):
|
||||||
print 'hmm... cannot parse URI %s, giving up' % uri
|
print 'hmm... cannot parse URI %s, giving up' % uri
|
||||||
|
@ -144,15 +153,26 @@ if __name__ == '__main__':
|
||||||
password = match.group('password')
|
password = match.group('password')
|
||||||
path = match.group('path')
|
path = match.group('path')
|
||||||
|
|
||||||
#
|
return (scheme, host, port, avatar, password, path)
|
||||||
# sanity check: matrix: or opensim: scheme?
|
|
||||||
#
|
def ParsePath(path):
|
||||||
if scheme != 'matrix' and scheme != 'opensim':
|
'''Try and parse path as /region/X/Y/Z.
|
||||||
print 'hmm...unknown scheme %s, calling it a day' % scheme
|
'''
|
||||||
|
|
||||||
|
loc = None
|
||||||
|
match = reLOC.match(path)
|
||||||
|
if match:
|
||||||
|
loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'),
|
||||||
|
int(match.group('x')),
|
||||||
|
int(match.group('y')),
|
||||||
|
int(match.group('z')))
|
||||||
|
return loc
|
||||||
|
|
||||||
|
|
||||||
|
def GetGridInfo(host, port):
|
||||||
|
'''Invoke /get_grid_info on target grid and obtain additional parameters
|
||||||
|
'''
|
||||||
|
|
||||||
#
|
|
||||||
# get grid info from OpenSim server
|
|
||||||
#
|
|
||||||
gridname = None
|
gridname = None
|
||||||
gridnick = None
|
gridnick = None
|
||||||
login = None
|
login = None
|
||||||
|
@ -178,19 +198,16 @@ if __name__ == '__main__':
|
||||||
login = gridInfoXml.findtext('/login')
|
login = gridInfoXml.findtext('/login')
|
||||||
welcome = gridInfoXml.findtext('/welcome')
|
welcome = gridInfoXml.findtext('/welcome')
|
||||||
economy = gridInfoXml.findtext('/economy')
|
economy = gridInfoXml.findtext('/economy')
|
||||||
|
authenticator = gridInfoXml.findtext('/authenticator')
|
||||||
|
|
||||||
except urllib2.URLError:
|
except urllib2.URLError:
|
||||||
print 'oops, failed to retrieve grid info, proceeding with guestimates...'
|
print 'oops, failed to retrieve grid info, proceeding with guestimates...'
|
||||||
|
|
||||||
#
|
return (gridname, gridnick, login, welcome, economy)
|
||||||
# fallback: use supplied uri in case GridInfo drew a blank
|
|
||||||
#
|
|
||||||
if not login: login = uri
|
|
||||||
|
|
||||||
#
|
|
||||||
# ok, got everything, now construct the command line
|
def StartClient(client, nick, login, welcome, economy, avatar, password, location):
|
||||||
#
|
clientArgs = [ client ]
|
||||||
clientArgs = ['matrix: %s' % gridnick]
|
|
||||||
clientArgs += ['-loginuri', login]
|
clientArgs += ['-loginuri', login]
|
||||||
|
|
||||||
if welcome: clientArgs += ['-loginpage', welcome]
|
if welcome: clientArgs += ['-loginpage', welcome]
|
||||||
|
@ -201,19 +218,55 @@ if __name__ == '__main__':
|
||||||
clientArgs += urllib.unquote(avatar).split()
|
clientArgs += urllib.unquote(avatar).split()
|
||||||
clientArgs += [password]
|
clientArgs += [password]
|
||||||
|
|
||||||
#
|
if location:
|
||||||
# take a closer look at path: if it's a /region/X/Y/Z pattern, use
|
clientArgs += [location]
|
||||||
# it as the "SLURL
|
|
||||||
#
|
|
||||||
match = reLOC.match(path)
|
|
||||||
if match:
|
|
||||||
loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'),
|
|
||||||
int(match.group('x')),
|
|
||||||
int(match.group('y')),
|
|
||||||
int(match.group('z')))
|
|
||||||
clientArgs += [loc]
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# all systems go
|
# all systems go
|
||||||
#
|
#
|
||||||
os.execv(client, clientArgs)
|
os.execv(client, clientArgs)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
#
|
||||||
|
# parse command line options and deal with help requests
|
||||||
|
#
|
||||||
|
options = ParseOptions()
|
||||||
|
client = ParseConfig(options)
|
||||||
|
|
||||||
|
#
|
||||||
|
# sanity check: URI supplied?
|
||||||
|
#
|
||||||
|
if not sys.argv:
|
||||||
|
print 'missing opensim/matrix URI'
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
#
|
||||||
|
# parse URI and extract scheme. host, port?, avatar?, password?
|
||||||
|
#
|
||||||
|
uri = sys.argv.pop()
|
||||||
|
(scheme, host, port, avatar, password, path) = ParseUri(uri)
|
||||||
|
|
||||||
|
#
|
||||||
|
# sanity check: matrix: or opensim: scheme?
|
||||||
|
#
|
||||||
|
if scheme != 'matrix' and scheme != 'opensim':
|
||||||
|
print 'hmm...unknown scheme %s, calling it a day' % scheme
|
||||||
|
|
||||||
|
#
|
||||||
|
# get grid info from OpenSim server
|
||||||
|
#
|
||||||
|
(gridname, gridnick, login, welcome, economy) = GetGridInfo(host, port)
|
||||||
|
|
||||||
|
#
|
||||||
|
# fallback: use supplied uri in case GridInfo drew a blank
|
||||||
|
#
|
||||||
|
if not login: login = uri
|
||||||
|
|
||||||
|
#
|
||||||
|
# take a closer look at path: if it's a /region/X/Y/Z pattern, use
|
||||||
|
# it as the "SLURL
|
||||||
|
#
|
||||||
|
location = ParsePath(path)
|
||||||
|
StartClient(client, gridnick, login, welcome, economy, avatar, password, location)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue