Added unit tests, renamed to follow absolute importing guidelines, and made minor...
[invirt/packages/python-jsonrpclib.git] / jsonrpclib / history.py
1
2 class History(object):
3     """
4     This holds all the response and request objects for a
5     session. A server using this should call "clear" after
6     each request cycle in order to keep it from clogging 
7     memory.
8     """
9     requests = []
10     responses = []
11     _instance = None
12     
13     @classmethod
14     def instance(cls):
15         if not cls._instance:
16             cls._instance = cls()
17         return cls._instance
18
19     def add_response(self, response_obj):
20         self.responses.append(response_obj)
21     
22     def add_request(self, request_obj):
23         self.requests.append(request_obj)
24
25     @property
26     def request(self):
27         if len(self.requests) == 0:
28             return None
29         else:
30             return self.requests[-1]
31
32     @property
33     def response(self):
34         if len(self.responses) == 0:
35             return None
36         else:
37             return self.responses[-1]
38
39     def clear(self):
40         del self.requests[:]
41         del self.responses[:]