Random bug fixes, advancing the channels, unit tests, seperating contacts not just...
[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                         return False
40                 elif self._backend.is_authed():
41                         return True
42                 else:
43                         try:
44                                 loggedIn = self._backend.login(self._username, self._password)
45                         except RuntimeError:
46                                 loggedIn = False
47                         if loggedIn:
48                                 return True
49                         else:
50                                 self.logout()
51                                 return False
52
53         @property
54         def backend(self):
55                 """
56                 Login enforcing backend
57                 """
58                 assert self.is_logged_in(), "User not logged in"
59                 return self._backend
60
61         @property
62         def addressbook(self):
63                 """
64                 Delay initialized addressbook
65                 """
66                 if self._addressbook is None:
67                         _moduleLogger.info("Initializing addressbook")
68                         self._addressbook = addressbook.Addressbook(self.backend)
69                 return self._addressbook
70
71         @property
72         def conversations(self):
73                 """
74                 Delay initialized addressbook
75                 """
76                 if self._conversations is None:
77                         _moduleLogger.info("Initializing conversations")
78                         self._conversations = conversations.Conversationst(self.backend)
79                 return self._conversations