comparison profilemanager/manager.py @ 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
comparison
equal deleted inserted replaced
8:7205cb6f5530 9:b1274abd1206
2 manage Mozilla/Firefox profiles 2 manage Mozilla/Firefox profiles
3 """ 3 """
4 4
5 import os 5 import os
6 import shutil 6 import shutil
7 import ConfigParser
7 from utils import format_tabular 8 from utils import format_tabular
8 from ConfigParser import SafeConfigParser as ConfigParser 9 from ConfigParser import SafeConfigParser as ConfigParser
9 10
10 class ProfileNotFound(Exception): 11 class ProfileNotFound(Exception):
11 """ 12 """
22 self.profiles = profiles 23 self.profiles = profiles
23 self.profile_dir = os.path.abspath(os.path.dirname(profiles)) 24 self.profile_dir = os.path.abspath(os.path.dirname(profiles))
24 25
25 ### (public) API 26 ### (public) API
26 27
28 def new(self, name):
29 """
30 generate a new clean profile
31 - name: name of the profile to generate
32 """
33 raise NotImplementedError
34
27 def list(self, directories=False): 35 def list(self, directories=False):
28 """ 36 """
29 lists the profiles available in the config file 37 lists the profiles available in the config file
30 - directories : display the directories 38 - directories : display the directories
31 """ 39 """
32 parser = ConfigParser() 40 profiles = self.profiles_dict()
33 parser.read(self.profiles)
34 retval = [] 41 retval = []
35 for section in parser.sections(): 42 for name in sorted(profiles.keys()):
36 if section == 'General':
37 continue
38 name = parser.get(section, 'name')
39 values = [name] 43 values = [name]
40 if directories: 44 if directories:
41 values.append(self.path(name)) 45 values.append(self.path(name))
42 retval.append(values) 46 retval.append(values)
43 return format_tabular(retval) 47 return format_tabular(retval)
44 48
45 def backups(self, profile=None): 49 def backups(self, profile=None):
46 """ 50 """
47 list backups for a given profile, or all profiles if the 51 list backups for a given profile, or all profiles if the
48 profile is not given 52 profile is not given; returns a list of backups if
53 profile is given or a dictionary of lists otherwise
49 """ 54 """
55 if profile is None:
56 # all profiles
57 retval = {}
58 return retval
50 # TODO 59 # TODO
51 60
52 def clone(self, source, dest, hash=True): 61 def clone(self, source, dest, hash=True):
53 """ 62 """
54 clones the profile `source` and output to `dest` 63 clones the profile `source` and output to `dest`
109 if profile.get('isrelative', None) == '1': 118 if profile.get('isrelative', None) == '1':
110 return os.path.join(self.profile_dir, profile['path']) 119 return os.path.join(self.profile_dir, profile['path'])
111 return profile['path'] 120 return profile['path']
112 121
113 def profile_dict(self, profile): 122 def profile_dict(self, profile):
123 """
124 return option dictionary for a single profile
125 """
114 parser = ConfigParser() 126 parser = ConfigParser()
115 parser.read(self.profiles) 127 parser.read(self.profiles)
116 for section in parser.sections(): 128 for section in parser.sections():
117 if not parser.has_option(section, 'name'): 129 if not parser.has_option(section, 'name'):
118 continue # not a profile 130 continue # not a profile
119 if parser.get(section, 'name') == profile: 131 if parser.get(section, 'name') == profile:
120 return dict(parser.items(section)) 132 return dict(parser.items(section))
121 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles)) 133 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
122 134
135 def profiles_dict(self):
136 """
137 return nested dict of all profiles
138 """
139 # XXX assumes profiles have unique names
140 parser = ConfigParser()
141 parser.read(self.profiles)
142 retval = {}
143 for section in parser.sections():
144 if section == 'General':
145 continue
146 try:
147 name = parser.get(section, 'name')
148 except ConfigParser.NoOptionError:
149 continue
150 retval[name] = self.profile_dict(name)
151 return retval
152
153
123 def hash(self): 154 def hash(self):
124 """ 155 """
125 generate a random hash for a new profile 156 generate a random hash for a new profile
126 """ 157 """
127 # XXX right now this is completely fake 158 # XXX right now this is completely fake
128 return 'FOO' 159 return 'FOO'
160
161