User:Jbkraft/osgridstart

osgrid_start.py
I use this so I don't have to hunt through the map to find an open grid location. It will not keep your sims together at the moment if you have more then one.

""" osgrid_start.py
 * 1) !/usr/bin/env python
 * 2) encoding: utf-8

Created by JB Kraft on 2008-07-06. Licence: Whatevah """

import sys import getopt import re import urllib2 import glob import xml.dom.minidom try: from BeautifulSoup import BeautifulSoup except: print 'This requires BeautifulSoup' print 'http://www.crummy.com/software/BeautifulSoup/' sys.exit( 1 )

OPTS = { 'verbose': False, 'url' : 'http://osgrid.org/index.php?page=regions&AStart=0&ALimit=10000' }

help_message = ''' This util finds open areas on osgrid and rewrites your region xml files so they don't bonk anyone.

NOTES: - this is not smart enough yet to keep your regions together if you have more then one. I just run one sim so it works for me. Saves me having to check the map everytime I launch for someone at my last grid location.

- this is meant to be run from the opensim/bin directory and expects that your region files are in ./Regions

- it grabs the region list from osgrid.org and parses that html to make the region list. If the formatting of that pages changes it will break this. '''

class Usage(Exception): def __init__(self, msg): self.msg = msg

def get_region_list( url ): '''grab the region list as html from osgrid and parse it into a dict of Sim -> [x,y]...''' regions = {} html = urllib2.urlopen( url ).read soup = BeautifulSoup(html) trs = soup.findAll("tr", {'style' : re.compile('BACKGROUND-COLOR')}) for tr in trs: tds = tr.findAll('td') try: coords = [int(tds[2].find(text=True)),int(tds[3].find(text=True))] regions[tds[1].find(text=True)] = coords except: pass return regions def map_regions( regions ): '''looks in local subdir ./Regions/*.xml to find any xml files and locate an empty place for each. NOTE: this will not keep regions together at the moment.''' xmls = glob.glob( 'Regions/*.xml') for xmlfile in xmls: doc = xml.dom.minidom.parse( xmlfile ) config = doc.getElementsByTagName('Config') sim_name = config[0].getAttribute('sim_name') coord = [int(config[0].getAttribute('sim_location_x')), int(config[0].getAttribute('sim_location_y'))] print '     Sim: ' + sim_name print ' Current: ' + str(coord) if is_occupied( regions, coord ): print ' Occupied: True' nextopen = find_next_open(regions) print 'Next open: ' + str(nextopen) # store this so we don't re-use it           regions[sim_name] = nextopen config[0].setAttribute('sim_location_x', str(nextopen[0]) ) config[0].setAttribute('sim_location_y', str(nextopen[1]) ) # write out the new file open( xmlfile, 'w').write(doc.childNodes[0].toxml) else: print ' Occupied: False' print def find_next_open( regions, level = 0 ): '''Recurse outward in concentric circles from center to find the next available open grid location from the region dict''' center = [10000,10000] current = [center[0] + level, center[1] + level ] # down y side while current[1] > center[1] - level: if not is_occupied( regions, current ): return current current[1] = current[1] - 1 # back x side while current[0] > center[0] - level: if not is_occupied( regions, current ): return current current[0] = current[0] - 1 # up y side while current[1] < center[1] + level: if not is_occupied( regions, current ): return current current[1] = current[1] + 1 # back to start while current[0] < center[0] + level: if not is_occupied( regions, current ): return current current[0] = current[0] + 1 return find_next_open( regions, level + 1) def is_occupied( regions, coord ): hunt through the region dict to see if this space is occupied for name, loc in regions.items: if loc[0] == coord[0] and loc[1] == coord[1]: return True return False

def main(argv=None): global OPTS if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "hu:v", ["help", "url="]) except getopt.error, msg: raise Usage(msg) # option processing for option, value in opts: if option == "-v": OPTS['verbose'] = True if option in ("-h", "--help"): raise Usage(help_message) if option in ("-u", "--url"): OPTS['url'] = value regions = get_region_list( OPTS['url'] ) map_regions( regions )
 * 1) M A I N
 * 1) M A I N

except Usage, err: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) print >> sys.stderr, "\t for help use --help" return 2

if __name__ == "__main__": sys.exit(main)