""" rbos.py (C)2008 Don A. Hanlen I make no claims, and take no reponsibility, for use or abuse of this code! I require those who DO use it to cite its source! If this code is used for commercial purposes I expect a (negotiable) cut! These functions use the Python os module in a way that will work on both Posix and Windoze, hopefully elsewhere. """ import os import string import pickle def directory(): """ return list of files in current working directory """ return os.listdir(os.getcwd()) def remove(fName): """ return 'removed' | 'no such file' """ if fName in directory(): os.remove(fName) return 'removed' return 'no such file' def rename(oldName, newName): """ return newName | 'no such file' """ if oldName in directory(): remove(newName) os.rename(oldName, newName) return newName return "no such file" def copy(oldName, newName): """ return newName | 'no such file' """ if oldName in directory(): fn = open(oldName, 'r') t = fn.read() fn.close() fn = open(newName, 'w') fn.write(t) fn.close() return newName return "no such file" def backupOpen(name, extension): """ move name.extension to name.'bck', return new name.extension for write will return new file even nothing to back up! """ rename(name+'.'+extension, name+'.bck') return open(name+'.txt', 'w') def loadList(fName): """ like a pickle.load, but opens file on its own, returns dList[] """ f = open(fName, 'r') t = f.read() f.close() t = string.split(t, '\n') dList = [] for x in t[:-1]: dList.append(eval(x)) return dList def dumpList(dList, fName): """ like a pickle.dump() but opens file on its own """ f = open(fName, 'w') for x in dList: f.write(str(x)+'\n') f.close() def getsavedList(dList, hashH): """ backup current saved games list[], return last list[] could be same dictionary! """ base = 'RBsg' ## saved games ext = '.sdc' ## Saved Dictionary Current exL = '.sdl' ## Saved Dictionary Last dr = directory() current = '' last = '' for x in dr: if string.find(x, base)==0: if string.find(x[-len(ext):], ext)==0: current = x if string.find(x[-len(exL):], exL)==0: last = x if current: if current!=(base+hashH+ext): if last: remove(last) last = current[:string.find(current,'.')] + exL rename(current, last) dumpList(dList, base+hashH+ext) else: dumpList(dList, base+hashH+ext) return dList if last: pList = loadList(last) return pList return dList def convertPickleDict(dName, fName): """ a util to convert pickled dictionaries to lists[] (it appears pickle screws with attributes on Windoze) """ fd = open(dName, 'r') dd = pickle.load(fd) fd.close() ff = open(fName, 'w') for x in dd.keys(): ff.write(str(dd[x])+'\n') ff.close()