# $Id$ ############################################################################# # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################## CONFIGFILE_NAME = 'config.ini' template = { 'AWS_ACCESS_KEY_ID': '', 'AWS_SECRET_ACCESS_KEY': '', 'SYNC_BASEDIR': '', } import os, sys import ConfigParser class Config(object): def __init__(self): self.s3_access_key = '' self.s3_secret_key = '' self.sync_base_dir = '' self.bucket_name = '' def filepath(): ''' ''' if sys.platform == 'win32': home = os.environ.get('HOMEPATH', os.getcwd()) apppath = os.path.join(home, 'Application Data', 's3sync') else: home = os.environ.get('HOME', os.getcwd()) apppath = os.path.join(home, '.s3sync') if not os.path.exists(apppath): os.makedirs(apppath) filename = os.path.join(apppath, CONFIGFILE_NAME) if not os.path.exists(filename): saveSettings([]) return filename def loadSettings(): ''' ''' cp = ConfigParser.SafeConfigParser(template) cp.read(filepath()) confs = [] for sect in cp.sections(): conf = Config() conf.s3_access_key = cp.get(sect, 'AWS_ACCESS_KEY_ID') conf.s3_secret_key = cp.get(sect, 'AWS_SECRET_ACCESS_KEY') conf.sync_base_dir = cp.get(sect, 'SYNC_BASEDIR') conf.bucket_name = sect confs.append(conf) return confs def saveSettings(confs): ''' ''' import ConfigParser cp = ConfigParser.SafeConfigParser(template) for conf in confs: sect = conf.bucket_name #cp.set(sect, 'AWS_ACCESS_KEY_ID', conf.s3_access_key) #cp.set(sect, 'AWS_SECRET_ACCESS_KEY', conf.s3_secret_key) cp.set(sect, 'SYNC_BASEDIR', conf.sync_base_dir) try: import os fileobj = file(filepath(), 'w') cp.write(fileobj) except: pass