Added locking and moved work around / reduced repeat work at startup. Should clear up segfaults and improve startup time

This commit is contained in:
Anna 2010-05-04 11:03:32 -04:00
parent 7cce83c0f3
commit 6044fa27d2
3 changed files with 61 additions and 39 deletions

View file

@ -3,7 +3,6 @@
# Custom twitter client... mostly for learning Python
import sys, ConfigParser, os, re, optparse, shelve
import twitter
import gtk, gtk.glade, gobject
from urllib2 import HTTPError
from twitterwidgets import TweetPane
@ -47,7 +46,7 @@ class MyTwitter():
for item in config.sections():
if (re.match(r'account', item)):
username = config.get(item, 'username')
self.accounts[username] = twitter.Api(username=username, password=config.get(item, 'password'))
self.accounts[username] = apithreads.SafeApi(username=username, password=config.get(item, 'password'))
self.username = self.accounts.keys()[0]
self.api = self.accounts[self.username]
@ -106,8 +105,9 @@ class MyTwitter():
# Add the tabs from last session to the notebook
page_num = self.db['active_page']
for tab, single_tweet in self.db['open_tabs']:
self.add_to_notebook(tab, single_tweet)
self.add_to_notebook(tab, single_tweet, update=False)
self.tweet_notebook.set_current_page(page_num)
self.update_windows()
# Put Home, @user, Direct Messages, and lists in the View menu for
# each user
@ -188,7 +188,8 @@ class MyTwitter():
def update_status(self):
text = self.update_entry.get_text()
self.update_entry.set_text("")
self.api.PostUpdate(text, in_reply_to_status_id=self.reply_id)
with self.api.lock:
self.api.PostUpdate(text, in_reply_to_status_id=self.reply_id)
self.reply_id = None
self.update_status_bar('Tweet Posted')
@ -224,7 +225,8 @@ class MyTwitter():
def on_retweet(self, widget, data):
self.api.PostRetweet(data['id'])
with self.api.lock:
self.api.PostRetweet(data['id'])
def on_reply_to(self, widget, data):
@ -262,7 +264,7 @@ class MyTwitter():
self.remove_view(name, single_tweet)
def add_to_notebook(self, name, single_tweet=None):
def add_to_notebook(self, name, single_tweet=None, update=True):
# If it already exists, don't add it, just switch to it
for i in range(self.tweet_notebook.get_n_pages()):
pane = self.tweet_notebook.get_nth_page(i)
@ -308,8 +310,9 @@ class MyTwitter():
pane=new_pane,
single_tweet=single_tweet).start()
self.update_windows()
self.tweet_notebook.set_current_page(-1) # switch to the new pane
if update:
self.update_windows()
self.tweet_notebook.set_current_page(-1) # switch to the new pane
def on_tab_change(self, event, page, page_num):
@ -356,10 +359,12 @@ class MyTwitter():
current_pane = self.tweet_notebook.get_nth_page(self.tweet_notebook.get_current_page())
user_name = re.sub('^user: ', '', current_pane.get_list_name())
if current_pane.get_following():
self.api.DestroyFriendship(user_name)
with self.api.lock:
self.api.DestroyFriendship(user_name)
current_pane.set_following(False)
else:
self.api.CreateFriendship(user_name)
with self.api.lock:
self.api.CreateFriendship(user_name)
current_pane.set_following(True)
self.update_follow_button(current_pane)