comparison profilemanager/manager.py @ 47:420becb84df7

* move exception string to the exception itself * further stubbing on backup * stub on temporary profile function
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 09:06:16 -0700
parents df1b2e48dddb
children 09a2666999fa
comparison
equal deleted inserted replaced
46:df1b2e48dddb 47:420becb84df7
17 class ProfileNotFound(Exception): 17 class ProfileNotFound(Exception):
18 """ 18 """
19 exception when a profile is specified but is not present in a given 19 exception when a profile is specified but is not present in a given
20 .ini file 20 .ini file
21 """ 21 """
22 def __init__(self, profile, config):
23 self.profile = profile
24 self.config = config
25 Exception.__init__('Profile %s not found in %s' % (profile, config))
22 26
23 class ProfileManager(object): 27 class ProfileManager(object):
28
29 backups_dir = 'backups' # directory for backups relative to profile_dir
24 30
25 def __init__(self, profiles): 31 def __init__(self, profiles):
26 """ 32 """
27 - profiles: filesystem path to profiles.ini file 33 - profiles: filesystem path to profiles.ini file
28 """ 34 """
65 - delete: delete the profile directory as well 71 - delete: delete the profile directory as well
66 """ 72 """
67 parser = self.parser() 73 parser = self.parser()
68 section = self.section(name, parser) 74 section = self.section(name, parser)
69 if section is None: 75 if section is None:
70 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles)) 76 raise ProfileNotFound(profile, self.profiles)
71 if delete: # remove the profile from disk 77 if delete: # remove the profile from disk
72 shutil.rmtree(self.path(name)) 78 shutil.rmtree(self.path(name))
73 parser.remove_section(section) 79 parser.remove_section(section)
74 parser.write(file(self.profiles, 'w')) 80 parser.write(file(self.profiles, 'w'))
75 81
119 """ 125 """
120 backup the profile 126 backup the profile
121 - profile: name of the profile to be backed up 127 - profile: name of the profile to be backed up
122 - dest: name of the destination (optional) 128 - dest: name of the destination (optional)
123 """ 129 """
130
131 # get the profile section
132 parser = self.parser()
133 section = self.section(profile)
134 if profile is None:
135 raise ProfileNotFound(profile, self.profiles)
136
137 # determine destination directory
124 if dest is None: 138 if dest is None:
125 dest = '%s.%d.bak' % (profile, int(time.time())) 139 dest = '%s.%d.bak' % (profile, int(time.time()))
140 name = dest
141 backups_dir = os.path.join(self.profile_dir, self.backups_dir)
142 if not os.path.exists(backups_dir):
143 os.mkdir(backups_dir)
144 dest = os.path.join(backups_dir, dest)
145 else:
146 if not os.path.isabs(dest):
147 dest = os.path.abspath(dest)
148 name = dest
149
150 # copy the files
126 self.clone(profile, dest, add=False) 151 self.clone(profile, dest, add=False)
127 # TODO: add something like 152
153
154 # add backup entry to profiles.ini:
128 # `Backup=$(profile)s.$(datestamp)s.bak` 155 # `Backup=$(profile)s.$(datestamp)s.bak`
129 # to self.profiles 156 raise NotImplementedError
157
130 158
131 def backups(self, profile=None): 159 def backups(self, profile=None):
132 """ 160 """
133 list backups for a given profile, or all profiles if the 161 list backups for a given profile, or all profiles if the
134 profile is not given; returns a list of backups if 162 profile is not given; returns a list of backups if
137 if profile is None: 165 if profile is None:
138 # all profiles 166 # all profiles
139 retval = {} 167 retval = {}
140 return retval 168 return retval
141 # TODO 169 # TODO
170 raise NotImplementedError
142 171
143 def restore(self, profile, date=None, delete=False): 172 def restore(self, profile, date=None, delete=False):
144 """ 173 """
145 restore the profile from a backup 174 restore the profile from a backup
146 the most recent backup is used unless `date` is given 175 the most recent backup is used unless `date` is given
157 if delete: # delete the backup 186 if delete: # delete the backup
158 # delete the directory 187 # delete the directory
159 # delete the entry from ``profiles.ini`` 188 # delete the entry from ``profiles.ini``
160 # if there are no backups, delete the ``backups`` line 189 # if there are no backups, delete the ``backups`` line
161 pass #TODO 190 pass #TODO
162 191
192 def temporary(self):
193 """make a temporary profile"""
194 raise NotImplementedError
163 195
164 def merge(self, output, *profiles): 196 def merge(self, output, *profiles):
165 """merge a set of profiles (not trivial!)""" 197 """merge a set of profiles (not trivial!)"""
166 raise NotImplementedError 198 raise NotImplementedError
167 199
229 return option dictionary for a single profile 261 return option dictionary for a single profile
230 """ 262 """
231 parser = self.parser() 263 parser = self.parser()
232 section = self.section(profile, parser) 264 section = self.section(profile, parser)
233 if section is None: 265 if section is None:
234 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles)) 266 raise ProfileNotFound(profile, self.profiles)
235 return dict(parser.items(section)) 267 return dict(parser.items(section))
236 268
237 def profiles_dict(self): 269 def profiles_dict(self):
238 """ 270 """
239 return nested dict of all profiles 271 return nested dict of all profiles