changeset 5:ca57920aa223

adding better formatting for list
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 06 Apr 2010 08:04:24 -0700
parents 35dc297efa25
children 2a3f5cdfd60c
files profilemanager/command.py profilemanager/manager.py profilemanager/utils.py
diffstat 3 files changed, 40 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/command.py	Mon Apr 05 13:53:22 2010 -0700
+++ b/profilemanager/command.py	Tue Apr 06 08:04:24 2010 -0700
@@ -161,5 +161,10 @@
             commandparser.error("Too many arguments given")
 
         # invoke the command
-        pprint(getattr(_object, name)(*command_args, **command_options.__dict__))
+        retval = getattr(_object, name)(*command_args, **command_options.__dict__)
+        if isinstance(retval, basestring):
+            print retval
+        else:
+            pprint(retval)
+        return retval
 
--- a/profilemanager/manager.py	Mon Apr 05 13:53:22 2010 -0700
+++ b/profilemanager/manager.py	Tue Apr 06 08:04:24 2010 -0700
@@ -4,6 +4,8 @@
 
 import os
 import shutil
+from utils import format_tabular
+from ConfigParser import SafeConfigParser as ConfigParser
 
 class ProfileNotFound(Exception):
     """
@@ -26,15 +28,14 @@
         """
         lists the profiles available in the config file
         """
-        from ConfigParser import SafeConfigParser as ConfigParser
         parser = ConfigParser()
         parser.read(self.profiles)
         names = []
         for section in parser.sections():
             if section == 'General':
                 continue
-            names.append(parser.get(section, 'name'))
-        return '\n'.join(names)
+            names.append((parser.get(section, 'name'),))
+        return format_tabular(names)
 
     def clone(self, source, dest):
         """
@@ -70,7 +71,7 @@
             # delete the backup
             pass
 
-    def merge(self, *profiles):
+    def merge(self, output, *profiles):
         """merge a set of profiles (not trivial!)"""
         raise NotImplementedError
 
@@ -79,3 +80,8 @@
     def path(self, profile):
         """returns the path to the profile"""
 
+    def profile_dict(self, profile):
+        parser = ConfigParser()
+        parser.read(self.profiles)
+        for section in parser.sections():
+            raise NotImplementedError
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/profilemanager/utils.py	Tue Apr 06 08:04:24 2010 -0700
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+def format_tabular(table, spacer=' '):
+    columns = [0 for i in range(max([len(row) for row in table]))]
+    assert (len(columns) == min([len(row) for row in table]))
+    for row in table:
+        for index, column in enumerate(row):
+            if len(column) > columns[index]:
+                columns[index] = len(column)
+    format_string = spacer.join(['%s%s' for i in range(len(columns))])
+    retval = []
+    for row in table:
+        values = []
+        for index, column in enumerate(row):
+            values.append(column)
+            values.append(' ' * (columns[index] - len(column)))
+        retval.append(format_string % tuple(values))
+    return '\n'.join(retval)
+    
+if __name__ == '__main__':
+    test_data = [ ['fox', 'i am a fox!', 'Foxkeh'],
+                  ['cat', 'meow', 'Lilly'],
+                  ['elephant', 'elephants shower with their trunks', 'Elephino'] ]
+    print format_tabular(test_data)