More cleanup from the refactor

This commit is contained in:
Anna 2010-06-29 15:14:26 -04:00
parent 14741c0fd0
commit 36dbddedaa
2 changed files with 68 additions and 55 deletions

58
config.py Normal file
View File

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

View File

@ -2,28 +2,27 @@
# #
# Custom twitter client... mostly for learning Python # Custom twitter client... mostly for learning Python
import sys, ConfigParser, os, re, optparse, shelve import os, re, shelve
import gtk, gtk.glade, gobject import gtk, gtk.glade, gobject
from urllib2 import HTTPError,URLError from urllib2 import HTTPError,URLError
from twitterwidgets import TweetPane from twitterwidgets import TweetPane
from threading import enumerate,Condition from threading import enumerate,Condition
import apithreads import apithreads
import config
class Hrafn(): class Hrafn():
""" Display Tweets, post to twitter """ """ Display Tweets, post to twitter """
def __init__(self, resize): def __init__(self, resize):
global config
self.resize = resize self.resize = resize
self.lists = {} self.lists = {}
self.lists_cond = Condition() self.lists_cond = Condition()
if config.get('global', 'trayicon') == '1': if config.config.get('global', 'trayicon') == '1':
self.use_trayicon = True 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 self.taskbar_min = True
else: else:
self.taskbar_min = False self.taskbar_min = False
@ -37,7 +36,7 @@ class Hrafn():
self.first_account_item = None self.first_account_item = None
# And init the DB stuff here # 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) self.db = shelve.open(db_file)
if not self.db.has_key('tokens'): if not self.db.has_key('tokens'):
@ -46,8 +45,8 @@ class Hrafn():
if not self.db.has_key('active_page'): if not self.db.has_key('active_page'):
self.db['active_page'] = 0 self.db['active_page'] = 0
self.num_entries = int(config.get('global', 'entries')) self.num_entries = int(config.config.get('global', 'entries'))
self.refresh_time = int(config.get('global', 'refreshtime')) self.refresh_time = int(config.config.get('global', 'refreshtime'))
if not self.db.has_key('active_user'): if not self.db.has_key('active_user'):
self.db['active_user'] = None self.db['active_user'] = None
@ -107,7 +106,7 @@ class Hrafn():
self.context_id = self.status_bar.get_context_id('message') self.context_id = self.status_bar.get_context_id('message')
# Add debug options to help menu # Add debug options to help menu
if debug: if config.debug:
menu_item = gtk.MenuItem('debug: Show Threads') menu_item = gtk.MenuItem('debug: Show Threads')
menu_item.connect('activate', self.debug_show_threads) menu_item.connect('activate', self.debug_show_threads)
self.help_menu.append(menu_item) self.help_menu.append(menu_item)
@ -650,59 +649,15 @@ class Hrafn():
### end 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 # main
if __name__ == "__main__": if __name__ == "__main__":
parser = optparse.OptionParser() config.init()
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
base_icon = gtk.gdk.pixbuf_new_from_file('ui/icon.svg') base_icon = gtk.gdk.pixbuf_new_from_file('ui/icon.svg')
icon = base_icon.scale_simple(128, 128, gtk.gdk.INTERP_BILINEAR) icon = base_icon.scale_simple(128, 128, gtk.gdk.INTERP_BILINEAR)
gtk.window_set_default_icon(icon) gtk.window_set_default_icon(icon)
my_twitter = Hrafn(options.resize) my_twitter = Hrafn(config.options.resize)
gtk.gdk.threads_init() gtk.gdk.threads_init()
gtk.gdk.threads_enter() gtk.gdk.threads_enter()