--- nfo/projects/netfraggle/bin/fraggleCtlPreferences.py 2004/08/25 13:53:48 1.1.1.1 +++ nfo/projects/netfraggle/bin/fraggleCtlPreferences.py 2004/08/30 12:50:47 1.6 @@ -1,18 +1,115 @@ +import os + import fraggleParserXML +from fraggleEngine import * +def create(parent): + return fraggleCtlPreferences(parent) + class fraggleCtlPreferences: - def __init_fraggle_xml__(): - self.fraggleXML = fraggleParserXML.create() + def __init_fraggle_xml__(self): + self.fraggleXML = fraggleParserXML.create(self) - def __init__(self,parent): + self.configList = {} + self.prefsfile = os.path.join(self.getDefaultDir(), 'prefs.xml') self.__init_fraggle_xml__() - + self.restoreProfiles() #def savePrefs(): - def setConfig(username,password,url,retrieval): - configList = [username,password,url,retrieval) + def setConfig(self,username,password,url,retrieval): + self.configList = {'username': username,'password': password,'url': url,'retrieval': retrieval} + + def getConfig(self): + if not self.configList: + self.loadConfig(self.prefsfile) + return self.configList + + def loadConfig(self,inputLocation): + self.configList = self.fraggleXML.unmarshalXML(inputLocation) + + def saveConfig(self): + #self.fraggleXML.marshalXML(self.profileDictionary, self.prefsfile) + self.fraggleXML.marshalXML(self.configList, self.prefsfile) + + def saveProfiles(self): + profilesTemp = os.path.join(self.getDefaultDir(), 'profiles.xml') + self.fraggleXML.marshalXML(self.profileDictionary, profilesTemp) + #print self.profileDictionary + + def restoreProfiles(self): + profiles = os.path.join(self.getDefaultDir(), 'profiles.xml') + self.profileDictionary = self.fraggleXML.unmarshalXML(profiles) + #print self.profileDictionary + + def appendProfile(self,profileKey,configList): + #print "Appending: " + #print self.profileDictionary + list = self.getProfileList() + try: + temp = list.index(profileKey) + #print "Index Collision" + return 1 + except ValueError: + try: + self.profileDictionary.append(profileKey) + self.profileDictionary.append(configList) + except AttributeError: + self.profileDictionary = [profileKey,configList] + return 0 + #print self.profileDictionary + + + def loadProfile(self,profileKey): + try: + key = self.profileDictionary.index(profileKey) + self.configList = self.profileDictionary[key+1] + finally: + pass + + def getProfileList(self): + #print self.profileDictionary + try: + profileBuffer = self.profileDictionary + x = len(profileBuffer) -1 + tempList = [] + while x >= 0: + tempList.append(profileBuffer[x-1]) + x = x-2 + return tempList + except TypeError: + return [] + except IndexError: + return [] + + def deleteProfile(self,profileId): + try: + self.profileDictionary.pop(self.profileDictionary.index(profileId)+1) + self.profileDictionary.pop(self.profileDictionary.index(profileId)) + except IndexError: + pass + + # from Pears: http://project5.freezope.org/pears/ + def getDefaultDir(self): + """Gets location of default dir and creates it + if necessary. ($HOME/.pears/)""" + try: + import pearsdebug + savedir = pearsdebug.savedir + except: + dir = '.netfraggle' + savedir = os.path.expanduser(os.path.join('~', dir)) + if len(savedir)<=len("c:\\/" + dir): + # problem that might occur on Win2k (no $HOME environment variable) + temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir) + if temp > len("c:\\/" + dir): + savedir = temp + # create dir if it doesn't exist + if not os.path.exists(savedir): + os.makedirs(savedir) + return savedir + \ No newline at end of file