From 36dbddedaa1e06d342fe6e2f421d58282523515f Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 29 Jun 2010 15:14:26 -0400 Subject: [PATCH] More cleanup from the refactor --- config.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ hrafn.py | 65 +++++++++---------------------------------------------- 2 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..4e29964 --- /dev/null +++ b/config.py @@ -0,0 +1,58 @@ +# 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 diff --git a/hrafn.py b/hrafn.py index fe1eb3f..1158be5 100755 --- a/hrafn.py +++ b/hrafn.py @@ -2,28 +2,27 @@ # # Custom twitter client... mostly for learning Python -import sys, ConfigParser, os, re, optparse, shelve +import os, re, shelve import gtk, gtk.glade, gobject from urllib2 import HTTPError,URLError from twitterwidgets import TweetPane from threading import enumerate,Condition import apithreads +import config class Hrafn(): """ Display Tweets, post to twitter """ def __init__(self, resize): - global config - self.resize = resize self.lists = {} self.lists_cond = Condition() - if config.get('global', 'trayicon') == '1': + if config.config.get('global', 'trayicon') == '1': self.use_trayicon = True - if config.get('global', 'taskbar_when_minimized') == '1': + if config.config.get('global', 'taskbar_when_minimized') == '1': self.taskbar_min = True else: self.taskbar_min = False @@ -37,7 +36,7 @@ class Hrafn(): self.first_account_item = None # And init the DB stuff here - db_file = os.path.expanduser(config.get('global', 'dbfile')) + db_file = os.path.expanduser(config.config.get('global', 'dbfile')) self.db = shelve.open(db_file) if not self.db.has_key('tokens'): @@ -46,8 +45,8 @@ class Hrafn(): if not self.db.has_key('active_page'): self.db['active_page'] = 0 - self.num_entries = int(config.get('global', 'entries')) - self.refresh_time = int(config.get('global', 'refreshtime')) + self.num_entries = int(config.config.get('global', 'entries')) + self.refresh_time = int(config.config.get('global', 'refreshtime')) if not self.db.has_key('active_user'): self.db['active_user'] = None @@ -107,7 +106,7 @@ class Hrafn(): self.context_id = self.status_bar.get_context_id('message') # Add debug options to help menu - if debug: + if config.debug: menu_item = gtk.MenuItem('debug: Show Threads') menu_item.connect('activate', self.debug_show_threads) self.help_menu.append(menu_item) @@ -650,59 +649,15 @@ class Hrafn(): ### end class Hrafn -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() - # main if __name__ == "__main__": - 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 + config.init() base_icon = gtk.gdk.pixbuf_new_from_file('ui/icon.svg') icon = base_icon.scale_simple(128, 128, gtk.gdk.INTERP_BILINEAR) gtk.window_set_default_icon(icon) - my_twitter = Hrafn(options.resize) + my_twitter = Hrafn(config.options.resize) gtk.gdk.threads_init() gtk.gdk.threads_enter()