This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
hrafn/config.py

59 lines
2.1 KiB
Python
Raw Normal View History

2010-06-29 19:14:26 +00:00
# Configuration module that handles both
# command-line options and config file.
# Call init() once at the beginning of the application
# to read everything in.
import ConfigParser, optparse, os
def _check_config(config, config_file):
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', '5')
new_data = True
if not config.has_option('global', 'trayicon'):
config.set('global', 'trayicon', '1')
new_data = True
if not config.has_option('global', 'taskbar_when_minimized'):
config.set('global', 'taskbar_when_minimized', '0')
new_data = True
if not config.has_option('global', 'debug'):
config.set('global', 'debug', '0')
new_data = True
if not config.has_option('global', 'dbfile'):
config.set('global', 'dbfile', '~/.hrafn.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()
def init():
global options
global config
global debug
parser = optparse.OptionParser()
parser.add_option('-c' ,'--config', dest="filename", default="~/.hrafn.conf", help="read configuration from FILENAME instead of the default ~/.hrafn.conf")
parser.add_option('-n' ,'--no-resize', dest="resize", action='store_false', default=True, help="use the default window size instead of the size from the last session")
(options, args) = parser.parse_args()
# Read in config, and set config options to defaults,
# if they are not present
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser(options.filename))
_check_config(config, options.filename)
# Set the convenient global debug flag
debug = False
if config.get('global', 'debug') == 1:
debug = True