Package sword2 :: Module transaction_history
[hide private]
[frames] | no frames]

Source Code for Module sword2.transaction_history

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  """ 
 5  Provides a class to hold the `sword2.Connection` transaction history and give simple means for export (JSON) and reporting. 
 6  """ 
 7   
 8  from sword2_logging import logging 
 9   
10  from datetime import datetime 
11   
12  th_l = logging.getLogger(__name__) 
13   
14 -class Transaction_History(list):
15 - def log(self, event_type, **kw):
16 self.append({'type':event_type, 17 'timestamp':datetime.now().isoformat(), 18 'payload':kw})
19
20 - def __str__(self):
21 _s = [] 22 for item in self: 23 _s.append("-"*20) 24 _s.append("Type: '%s' [%s]\nData:" % (item['type'], item['timestamp'])) 25 for key, value in item['payload'].iteritems(): 26 _s.append("%s: %s" % (key, value)) 27 28 return "\n".join(_s)
29
30 - def to_json(self):
31 from compatible_libs import json 32 if json: 33 th_l.debug("Attempting to dump %s history items to JSON" % len(self)) 34 return json.dumps(self) 35 else: 36 th_l.error("Cannot procede with converting the transaction history to JSON")
37
38 - def to_pretty_json(self):
39 from compatible_libs import json 40 if json: 41 th_l.debug("Attempting to dump %s history items to indented, readable JSON" % len(self)) 42 return json.dumps(self, indent=True) 43 else: 44 th_l.error("Cannot procede with converting the transaction history to JSON")
45