comparison src/tools/memory.py @ 420:acd908528ef7

core: profile creation/deletion through database
author Goffi <goffi@goffi.org>
date Wed, 02 Nov 2011 22:49:23 +0100
parents 199cf4ebcc74
children 6c20c76abdcc
comparison
equal deleted inserted replaced
419:6c167a2e04b8 420:acd908528ef7
37 SAVEFILE_HISTORY="/history" 37 SAVEFILE_HISTORY="/history"
38 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) 38 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins)
39 SAVEFILE_DATABASE="/sat.db" 39 SAVEFILE_DATABASE="/sat.db"
40 40
41 class ProfileNotInCacheError(Exception): 41 class ProfileNotInCacheError(Exception):
42 pass
43
44 class ConnectedProfileError(Exception):
42 pass 45 pass
43 46
44 class Param(): 47 class Param():
45 """This class manage parameters with xml""" 48 """This class manage parameters with xml"""
46 ### TODO: add desciption in params 49 ### TODO: add desciption in params
112 self.params = {} 115 self.params = {}
113 self.params_gen = {} 116 self.params_gen = {}
114 host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML) 117 host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML)
115 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) 118 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
116 119
117 def createProfile(self, name): 120 def createProfile(self, profile):
118 """Create a new profile 121 """Create a new profile
119 @param name: Name of the profile""" 122 @param profile: profile of the profile"""
120 if self.storage.hasProfile(name): 123 #FIXME: must be asynchronous and call the callback once the profile actually exists
124 if self.storage.hasProfile(profile):
125 info (_('The profile profile already exists'))
126 return True
127 if not self.host.trigger.point("ProfileCreation", profile):
128 return False
129 self.storage.createProfile(profile)
130 return False
131
132 def asyncCreateProfile(self, profile, callback, errback):
133 """Create a new profile
134 @param profile: name of the profile
135 @param callback: called when the profile actually exists in database and memory
136 @param errback: called with a string constant as parameter:
137 - CONFLICT: the profile already exists
138 - CANCELED: profile creation canceled
139 - DATABASE: profile creation in database failed"""
140 #FIXME: must be asynchronous and call the callback once the profile actually exists
141 if self.storage.hasProfile(profile):
121 info (_('The profile name already exists')) 142 info (_('The profile name already exists'))
143 errback("CONFLICT")
144 return
145 if not self.host.trigger.point("ProfileCreation", profile):
146 errback("CANCELED")
147 return
148 d = self.storage.createProfile(profile)
149 d.addCallback(lambda ignore: callback())
150 d.addErrback(lambda ignore: errback("DATABASE"))
151
152
153 def deleteProfile(self, profile):
154 """Delete an existing profile
155 @param profile: name of the profile"""
156 #TODO: async equivalent, like for createProfile
157 if not self.storage.hasProfile(profile):
158 error(_('Trying to delete an unknown profile'))
122 return True 159 return True
123 if not self.host.trigger.point("ProfileCreation", name): 160 if self.host.isConnected(profile):
124 return False 161 error(_("Trying to delete a connected profile"))
125 self.params[name]={} 162 raise ConnectedProfileError
126 return False 163 self.storage.deleteProfile(profile)
127
128 def deleteProfile(self, name):
129 """Delete an existing profile
130 @param name: Name of the profile"""
131 if not self.storage.hasProfile(name):
132 error (_('Trying to delete an unknown profile'))
133 return True
134 del self.params[name]
135 return False 164 return False
136 165
137 def getProfileName(self, profile_key): 166 def getProfileName(self, profile_key):
138 """return profile according to profile_key 167 """return profile according to profile_key
139 @param profile_key: profile name or key which can be 168 @param profile_key: profile name or key which can be
594 """Create a new profile 623 """Create a new profile
595 @param name: Profile name 624 @param name: Profile name
596 """ 625 """
597 return self.params.createProfile(name) 626 return self.params.createProfile(name)
598 627
628 def asyncCreateProfile(self, name, callback, errback):
629 """Create a new profile
630 @param name: Profile name
631 """
632 return self.params.asyncCreateProfile(name, callback, errback)
633
599 def deleteProfile(self, name): 634 def deleteProfile(self, name):
600 """Delete an existing profile 635 """Delete an existing profile
601 @param name: Name of the profile""" 636 @param name: Name of the profile"""
602 return self.params.deleteProfile(name) 637 return self.params.deleteProfile(name)
603 638