changeset 9:b1274abd1206

rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 14 Apr 2010 16:07:20 -0700
parents 7205cb6f5530
children c77e9bef78d6
files profilemanager/manager.py
diffstat 1 files changed, 40 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/manager.py	Tue Apr 13 19:24:52 2010 -0700
+++ b/profilemanager/manager.py	Wed Apr 14 16:07:20 2010 -0700
@@ -4,6 +4,7 @@
 
 import os
 import shutil
+import ConfigParser
 from utils import format_tabular
 from ConfigParser import SafeConfigParser as ConfigParser
 
@@ -24,18 +25,21 @@
 
     ### (public) API
 
+    def new(self, name):
+        """
+        generate a new clean profile
+        - name: name of the profile to generate
+        """
+        raise NotImplementedError
+
     def list(self, directories=False):
         """
         lists the profiles available in the config file
         - directories : display the directories
         """
-        parser = ConfigParser()
-        parser.read(self.profiles)
+        profiles = self.profiles_dict()
         retval = []
-        for section in parser.sections():
-            if section == 'General':
-                continue
-            name = parser.get(section, 'name')
+        for name in sorted(profiles.keys()):
             values = [name]
             if directories:
                 values.append(self.path(name))
@@ -45,8 +49,13 @@
     def backups(self, profile=None):
         """
         list backups for a given profile, or all profiles if the
-        profile is not given
+        profile is not given;  returns a list of backups if
+        profile is given or a dictionary of lists otherwise
         """
+        if profile is None:
+            # all profiles
+            retval = {}
+            return retval
         # TODO
 
     def clone(self, source, dest, hash=True):
@@ -111,6 +120,9 @@
         return profile['path']
 
     def profile_dict(self, profile):
+        """
+        return option dictionary for a single profile
+        """
         parser = ConfigParser()
         parser.read(self.profiles)
         for section in parser.sections():
@@ -120,9 +132,30 @@
                 return dict(parser.items(section))
         raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
 
+    def profiles_dict(self):
+        """
+        return nested dict of all profiles
+        """
+        # XXX assumes profiles have unique names
+        parser = ConfigParser()
+        parser.read(self.profiles)
+        retval = {}
+        for section in parser.sections():
+            if section == 'General':
+                continue
+            try:
+                name = parser.get(section, 'name')
+            except ConfigParser.NoOptionError:
+                continue
+            retval[name] = self.profile_dict(name)
+        return retval
+
+
     def hash(self):
         """
         generate a random hash for a new profile
         """
         # XXX right now this is completely fake
         return 'FOO'
+
+