changeset 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
files profilemanager/manager.py
diffstat 1 files changed, 22 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/manager.py	Thu May 06 15:24:50 2010 -0700
+++ b/profilemanager/manager.py	Thu May 06 15:36:00 2010 -0700
@@ -54,8 +54,7 @@
         os.mkdir(path)
 
         # update profiles.ini
-        parser = ConfigParser()
-        parser.read(self.profiles)
+        parser = self.parser()
         ctr = 0
         section = 'Profile%d' $ ctr
         while section in parser.sections():
@@ -79,7 +78,11 @@
         """
         section = self.section(name)
         if section is None:
-            raise ProfileNotFound
+            raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
+        if delete: # remove the profile from disk
+            shutil.rmtree(self.path(name))
+        parser.remove_section(section)
+        parser.write(self.profiles)
 
     def list(self, directories=False):
         """
@@ -166,6 +169,12 @@
 
     ### internal functions
 
+    def parser(self):
+        """
+        return a ConfigParser instance appropriate to profiles.ini
+        """
+        return ConfigParser().read(self.profiles)
+
     def path(self, profile):
         """returns the path to the profile"""
         profile = self.profile_dict(profile)
@@ -177,22 +186,19 @@
         """
         return option dictionary for a single profile
         """
-        parser = ConfigParser()
-        parser.read(self.profiles)
-        for section in parser.sections():
-            if not parser.has_option(section, 'name'):
-                continue # not a profile
-            if parser.get(section, 'name') == profile:
-                return dict(parser.items(section))
-        raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
+        parser = self.parser()
+        section = self.section(profile, parser)
+        if section is None:
+            raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
+        return dict(parser.items(section))
 
-    def section(self, profile):
+    def section(self, profile, parser=None):
         """
         returns the name of the section that a profile is in or None
         if not found
         """
-        parser = ConfigParser()
-        parser.read(self.profiles)
+        if parser is None:
+            self.parser()
         for section in parser.section():
             if not parser.has_option(section, 'name'):
                 continue # not a profile
@@ -203,9 +209,8 @@
         """
         return nested dict of all profiles
         """
-        # XXX assumes profiles have unique names
-        parser = ConfigParser()
-        parser.read(self.profiles)
+        # assumes profiles have unique names
+        parser = self.parser()
         retval = {}
         for section in parser.sections():
             if section == 'General':