public
Description: A desktop widget that display informations about CPU speed, temperature and so on through OSD
Homepage:
Clone URL: git://github.com/kratorius/osdwidget.git
kratorius (author)
Mon Apr 06 06:29:43 -0700 2009
commit  c9007744fbf3da3e77fae30cb137ec31a9700d12
tree    c599f20804aedac6b56c0ca8677f2e57d2a65720
parent  3fcfc5870c6215190d56d9ad8e638293d3ddb904
osdwidget / gmail.py
100644 94 lines (75 sloc) 2.824 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import time
import threading
import libgmail
import urllib2
 
import settings
from base import BaseWidget
 
class GMailWidget(BaseWidget):
    def __init__(self):
        super(GMailWidget, self).__init__()
        self.set_horizontal_offset(350)
 
        self.username = settings.GMAIL_USER
        self.password = settings.GMAIL_PASS
 
        self.check_thread = GMailCheck(self.username, self.password)
        self.check_thread.start()
 
    def get_msg(self):
        msg_count = self.check_thread.get_msg_count()
 
        error_messages = {
            GMailCheck.CONNECTING: ("Connecting...", "yellow"),
            GMailCheck.CONNECTION_FAILURE: ("Can't connect to GMail!", "red"),
            GMailCheck.LOGIN_FAILURE: ("Can't login in GMail", "red")
        }
 
        if msg_count in error_messages.keys():
            return error_messages[msg_count]
 
        color = "green"
        if msg_count == 0:
            msg = "Empty inbox!"
        elif msg_count == 1:
            msg = "Got one unread message"
        else:
            msg = "Got %d unread messages" % msg_count
            if 10 < msg_count <= 25: color = "yellow"
            elif msg_count > 20: color = "red"
 
        return (msg, color)
 
class GMailCheck(threading.Thread):
    # various constants
    CONNECTING = -1
    LOGIN_FAILURE = -2
    CONNECTION_FAILURE = -3
 
    def __init__(self, username, password, *args, **kwargs):
        self.msg_count = self.CONNECTING
        self.username = username
        self.password = password
        self.logged_in = False
 
        super(GMailCheck, self).__init__(*args, **kwargs)
 
    def get_msg_count(self):
        return self.msg_count
 
    def gmail_login(self):
        """
Log into GMail
"""
        self.ga = libgmail.GmailAccount(self.username, self.password)
        try:
            self.ga.login()
        except (libgmail.GmailLoginFailure, urllib2.URLError, urllib2.httplib.BadStatusLine):
            return False
 
        self.logged_in = True
        return True
 
    def run(self):
        """
Executes the actual email check
"""
        while True:
            if not self.logged_in:
                if not self.gmail_login():
                    self.msg_count = self.LOGIN_FAILURE
 
            # if we logged in correctly or were already logged in
            if self.logged_in:
                try:
                    self.msg_count = self.ga.getUnreadMsgCount()
                except (urllib2.URLError, urllib2.httplib.BadStatusLine):
                    # we may lose the connection, so try to login once
                    # again next time
                    self.logged_in = False
                    self.msg_count = self.CONNECTION_FAILURE
 
            time.sleep(settings.GMAIL_CHECK_INTERVAL * 60)