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.

This commit is contained in:
Anna 2010-04-21 14:30:36 -04:00
parent e056cd76b4
commit a260d25fd6

View File

@ -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)):