Mercurial > hg > sworduploader
annotate sword2-libraries-pyinstaller-compatible/sword2/transaction_history.py @ 22:d1752c7031e4 timeouts tip
Updated .hgignore to ignore sword2_logging.conf and anything in .cache
author | Steve Welburn <stephen.welburn@eecs.qmul.ac.uk> |
---|---|
date | Tue, 22 Jan 2013 14:43:42 +0000 |
parents | 8b69bba225c9 |
children |
rev | line source |
---|---|
marco@16 | 1 #!/usr/bin/env python |
marco@16 | 2 # -*- coding: utf-8 -*- |
marco@16 | 3 |
marco@16 | 4 """ |
marco@16 | 5 Provides a class to hold the `sword2.Connection` transaction history and give simple means for export (JSON) and reporting. |
marco@16 | 6 """ |
marco@16 | 7 |
marco@16 | 8 from sword2_logging import logging |
marco@16 | 9 |
marco@16 | 10 from datetime import datetime |
marco@16 | 11 |
marco@16 | 12 th_l = logging.getLogger(__name__) |
marco@16 | 13 |
marco@16 | 14 class Transaction_History(list): |
marco@16 | 15 def log(self, event_type, **kw): |
marco@16 | 16 self.append({'type':event_type, |
marco@16 | 17 'timestamp':datetime.now().isoformat(), |
marco@16 | 18 'payload':kw}) |
marco@16 | 19 |
marco@16 | 20 def __str__(self): |
marco@16 | 21 _s = [] |
marco@16 | 22 for item in self: |
marco@16 | 23 _s.append("-"*20) |
marco@16 | 24 _s.append("Type: '%s' [%s]\nData:" % (item['type'], item['timestamp'])) |
marco@16 | 25 for key, value in item['payload'].iteritems(): |
marco@16 | 26 _s.append("%s: %s" % (key, value)) |
marco@16 | 27 |
marco@16 | 28 return "\n".join(_s) |
marco@16 | 29 |
marco@16 | 30 def to_json(self): |
marco@16 | 31 from compatible_libs import json |
marco@16 | 32 if json: |
marco@16 | 33 th_l.debug("Attempting to dump %s history items to JSON" % len(self)) |
marco@16 | 34 return json.dumps(self) |
marco@16 | 35 else: |
marco@16 | 36 th_l.error("Cannot procede with converting the transaction history to JSON") |
marco@16 | 37 |
marco@16 | 38 def to_pretty_json(self): |
marco@16 | 39 from compatible_libs import json |
marco@16 | 40 if json: |
marco@16 | 41 th_l.debug("Attempting to dump %s history items to indented, readable JSON" % len(self)) |
marco@16 | 42 return json.dumps(self, indent=True) |
marco@16 | 43 else: |
marco@16 | 44 th_l.error("Cannot procede with converting the transaction history to JSON") |
marco@16 | 45 |