Adding testing of blank Text channel, fixing lots of little bugs along the way
[theonering] / src / gvoice / session.py
1 #!/usr/bin/env python
2
3 import logging
4
5 import backend
6 import addressbook
7 import conversations
8
9
10 _moduleLogger = logging.getLogger("gvoice.session")
11
12
13 class Session(object):
14
15         def __init__(self, cookiePath):
16                 self._cookiePath = cookiePath
17                 self._username = None
18                 self._password = None
19                 self._backend = None
20                 self._addressbook = None
21                 self._conversations = None
22
23         def login(self, username, password):
24                 self._username = username
25                 self._password = password
26                 self._backend = backend.GVoiceBackend(self._cookiePath)
27                 if not self._backend.is_authed():
28                         self._backend.login(self._username, self._password)
29
30         def logout(self):
31                 self._username = None
32                 self._password = None
33                 self._backend = None
34                 self._addressbook = None
35                 self._conversations = None
36
37         def is_logged_in(self):
38                 if self._backend is None:
39                         _moduleLogger.info("No Backend")
40                         return False
41                 elif self._backend.is_authed():
42                         return True
43                 else:
44                         try:
45                                 loggedIn = self._backend.login(self._username, self._password)
46                         except RuntimeError, e:
47                                 _moduleLogger.exception("Re-authenticating and erroring")
48                                 loggedIn = False
49                         if loggedIn:
50                                 return True
51                         else:
52                                 _moduleLogger.info("Login failed")
53                                 self.logout()
54                                 return False
55
56         @property
57         def backend(self):
58                 """
59                 Login enforcing backend
60                 """
61                 assert self.is_logged_in(), "User not logged in"
62                 return self._backend
63
64         @property
65         def addressbook(self):
66                 """
67                 Delay initialized addressbook
68                 """
69                 if self._addressbook is None:
70                         _moduleLogger.info("Initializing addressbook")
71                         self._addressbook = addressbook.Addressbook(self.backend)
72                 return self._addressbook
73
74         @property
75         def conversations(self):
76                 """
77                 Delay initialized addressbook
78                 """
79                 if self._conversations is None:
80                         _moduleLogger.info("Initializing conversations")
81                         self._conversations = conversations.Conversations(self.backend)
82                 return self._conversations