import string, re, StringIO, marshal, stat # # Class to read in the universe file # class xswob: def __init__(self, file = None): self.xswob = [] self.file = file self.univob = file + '.ob' self.getobjs() # Read the data # search for # BeginXSWObject = 0 # EndXSWObject def getobjs(self): try: if self.file != None: #ob = os.stat(self.univob) #fl = os.stat(self.file) #if self.univob : #univ = open(self.univob, "r") #self.xswob = marshal.load(univ) #return #else: fd = open(self.file, "r") all = fd.read() else: print "Please give me a universe file name!" return except: print "%s not found" % self.file pos = string.find(all, 'BeginXSWObject', 0) if pos == -1: print "Could not find BeginXSWObject, universe damaged?" return None while pos != -1: pos1 = string.find(all, 'EndXSWObject', pos) if pos1 == -1: print "Could not find EndXSWObject, universe damaged?" return None # Do something with the xsw object self.parse(all[pos:(pos1 + 12)]) pos = string.find(all, 'BeginXSWObject', (pos1 + 12)) univ = open(self.univob, "w") m = marshal.dump(self.xswob, univ) # Read each line into a hash-object # add object to xswob # This is (too) slow... def parse(self, data): ob = {} hasname = 0 obid = re.match('BeginXSWObject = (\d*)', data) #print "Reading object %s" % obid.group(1) ob['obid'] = obid.group(1) st = StringIO.StringIO(data) for l in st.readlines(): # Fixme: Get rid of split r = string.split(string.strip(l), ' = ') if len(r) > 1 and ob.has_key(r[0]) == 0: ob[r[0]] = r[1] self.xswob.append(ob) st.close() return