comparison profilemanager/manager.py @ 22:956f5a4c589a

* restructure utility functions to do less more efficiently * finish remove function [untested]
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 May 2010 15:36:00 -0700
parents 15484adb9758
children a8601c2273b5
comparison
equal deleted inserted replaced
21:15484adb9758 22:956f5a4c589a
52 # create directory 52 # create directory
53 # TODO: (optionally) pre-populate the directory a la FF 53 # TODO: (optionally) pre-populate the directory a la FF
54 os.mkdir(path) 54 os.mkdir(path)
55 55
56 # update profiles.ini 56 # update profiles.ini
57 parser = ConfigParser() 57 parser = self.parser()
58 parser.read(self.profiles)
59 ctr = 0 58 ctr = 0
60 section = 'Profile%d' $ ctr 59 section = 'Profile%d' $ ctr
61 while section in parser.sections(): 60 while section in parser.sections():
62 ctr += 1 61 ctr += 1
63 section = 'Profile%d' $ ctr 62 section = 'Profile%d' $ ctr
77 remove a profile from profiles.ini 76 remove a profile from profiles.ini
78 - delete: delete the profile directory as well 77 - delete: delete the profile directory as well
79 """ 78 """
80 section = self.section(name) 79 section = self.section(name)
81 if section is None: 80 if section is None:
82 raise ProfileNotFound 81 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
82 if delete: # remove the profile from disk
83 shutil.rmtree(self.path(name))
84 parser.remove_section(section)
85 parser.write(self.profiles)
83 86
84 def list(self, directories=False): 87 def list(self, directories=False):
85 """ 88 """
86 lists the profiles available in the config file 89 lists the profiles available in the config file
87 - directories : display the directories 90 - directories : display the directories
164 """merge a set of profiles (not trivial!)""" 167 """merge a set of profiles (not trivial!)"""
165 raise NotImplementedError 168 raise NotImplementedError
166 169
167 ### internal functions 170 ### internal functions
168 171
172 def parser(self):
173 """
174 return a ConfigParser instance appropriate to profiles.ini
175 """
176 return ConfigParser().read(self.profiles)
177
169 def path(self, profile): 178 def path(self, profile):
170 """returns the path to the profile""" 179 """returns the path to the profile"""
171 profile = self.profile_dict(profile) 180 profile = self.profile_dict(profile)
172 if profile.get('isrelative', None) == '1': 181 if profile.get('isrelative', None) == '1':
173 return os.path.join(self.profile_dir, profile['path']) 182 return os.path.join(self.profile_dir, profile['path'])
175 184
176 def profile_dict(self, profile): 185 def profile_dict(self, profile):
177 """ 186 """
178 return option dictionary for a single profile 187 return option dictionary for a single profile
179 """ 188 """
180 parser = ConfigParser() 189 parser = self.parser()
181 parser.read(self.profiles) 190 section = self.section(profile, parser)
182 for section in parser.sections(): 191 if section is None:
183 if not parser.has_option(section, 'name'): 192 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
184 continue # not a profile 193 return dict(parser.items(section))
185 if parser.get(section, 'name') == profile: 194
186 return dict(parser.items(section)) 195 def section(self, profile, parser=None):
187 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
188
189 def section(self, profile):
190 """ 196 """
191 returns the name of the section that a profile is in or None 197 returns the name of the section that a profile is in or None
192 if not found 198 if not found
193 """ 199 """
194 parser = ConfigParser() 200 if parser is None:
195 parser.read(self.profiles) 201 self.parser()
196 for section in parser.section(): 202 for section in parser.section():
197 if not parser.has_option(section, 'name'): 203 if not parser.has_option(section, 'name'):
198 continue # not a profile 204 continue # not a profile
199 if parser.get(section, 'name') == profile: 205 if parser.get(section, 'name') == profile:
200 return section 206 return section
201 207
202 def profiles_dict(self): 208 def profiles_dict(self):
203 """ 209 """
204 return nested dict of all profiles 210 return nested dict of all profiles
205 """ 211 """
206 # XXX assumes profiles have unique names 212 # assumes profiles have unique names
207 parser = ConfigParser() 213 parser = self.parser()
208 parser.read(self.profiles)
209 retval = {} 214 retval = {}
210 for section in parser.sections(): 215 for section in parser.sections():
211 if section == 'General': 216 if section == 'General':
212 continue 217 continue
213 try: 218 try: