From a260d25fd6eef2dc4f2a8579f8effab399e418c8 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 21 Apr 2010 14:30:36 -0400 Subject: [PATCH] Added logic to create and put some defaults into a config file, if one is not found. And an error message if the account section is missing. --- mytwitter.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mytwitter.py b/mytwitter.py index 6c97f1f..99c2491 100755 --- a/mytwitter.py +++ b/mytwitter.py @@ -17,6 +17,31 @@ class MyTwitter(): config = ConfigParser.ConfigParser() config.read(os.path.expanduser(config_file)) + # Set config options to defaults, if they are not present + new_data = False + if not config.has_section('global'): + config.add_section('global') + new_data = True + if not config.has_option('global', 'entries'): + config.set('global', 'entries', '20') + new_data = True + if not config.has_option('global', 'refreshtime'): + config.set('global', 'refreshtime', '30') + new_data = True + if not config.has_option('global', 'dbfile'): + config.set('global', 'dbfile', '~/.mytwitter.db') + new_data = True + + # Write out new config data, if needed + if new_data: + config_filehandle = open(os.path.expanduser(config_file), 'wb') + config.write(config_filehandle) + config_filehandle.close() + + if len(config.sections()) < 2: + print "Error: You must define at least one [account] section in " + config_file + sys.exit(1) + self.accounts = {} for item in config.sections(): if (re.match(r'account', item)):