commit ac93b86ebcd7770a250bea63a81d0739277eb2a3
parent 97a6f91451f3242e7550a03cdcad9e40fe9cb9f0
Author: ThomasV <thomasv@gitorious>
Date: Mon, 5 May 2014 11:31:04 +0200
export as json or csv
Diffstat:
2 files changed, 132 insertions(+), 68 deletions(-)
diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
@@ -298,8 +298,7 @@ class ElectrumWindow(QMainWindow):
self.private_keys_menu.addAction(_("&Sweep"), self.sweep_key_dialog)
self.private_keys_menu.addAction(_("&Import"), self.do_import_privkey)
self.private_keys_menu.addAction(_("&Export"), self.export_privkeys_dialog)
-
- wallet_menu.addAction(_("&Export History"), self.do_export_history)
+ wallet_menu.addAction(_("&Export History"), self.export_history_dialog)
tools_menu = menubar.addMenu(_("&Tools"))
@@ -1880,24 +1879,10 @@ class ElectrumWindow(QMainWindow):
e.setReadOnly(True)
vbox.addWidget(e)
- hbox = QHBoxLayout()
- vbox.addLayout(hbox)
-
defaultname = 'electrum-private-keys.csv'
- directory = self.config.get('io_dir', unicode(os.path.expanduser('~')))
- path = os.path.join( directory, defaultname )
- filename_e = QLineEdit()
- filename_e.setText(path)
- def func():
- select_export = _('Select file to export your private keys to')
- p = self.getSaveFileName(select_export, defaultname, "*.csv")
- if p:
- filename_e.setText(p)
-
- button = QPushButton(_('File'))
- button.clicked.connect(func)
- hbox.addWidget(button)
- hbox.addWidget(filename_e)
+ select_msg = _('Select file to export your private keys to')
+ hbox, filename_e, csv_button = filename_field(self, self.config, defaultname, select_msg)
+ vbox.addLayout(hbox)
h, b = ok_cancel_buttons2(d, _('Export'))
b.setEnabled(False)
@@ -1933,7 +1918,7 @@ class ElectrumWindow(QMainWindow):
return
try:
- self.do_export_privkeys(filename, private_keys)
+ self.do_export_privkeys(filename, private_keys, csv_button.isChecked())
except (IOError, os.error), reason:
export_error_label = _("Electrum was unable to produce a private key-export.")
QMessageBox.critical(None, _("Unable to create csv"), export_error_label + "\n" + str(reason))
@@ -1945,12 +1930,16 @@ class ElectrumWindow(QMainWindow):
self.show_message(_("Private keys exported."))
- def do_export_privkeys(self, fileName, pklist):
- with open(fileName, "w+") as csvfile:
- transaction = csv.writer(csvfile)
- transaction.writerow(["address", "private_key"])
- for addr, pk in pklist.items():
- transaction.writerow(["%34s"%addr,pk])
+ def do_export_privkeys(self, fileName, pklist, is_csv):
+ with open(fileName, "w+") as f:
+ if is_csv:
+ transaction = csv.writer(f)
+ transaction.writerow(["address", "private_key"])
+ for addr, pk in pklist.items():
+ transaction.writerow(["%34s"%addr,pk])
+ else:
+ import json
+ f.write(json.dumps(pklist, indent = 4))
def do_import_labels(self):
@@ -1979,54 +1968,88 @@ class ElectrumWindow(QMainWindow):
QMessageBox.critical(None, _("Unable to export labels"), _("Electrum was unable to export your labels.")+"\n" + str(reason))
- def do_export_history(self):
- wallet = self.wallet
- select_export = _('Select file to export your wallet transactions to')
- fileName = QFileDialog.getSaveFileName(QWidget(), select_export, os.path.expanduser('~/electrum-history.csv'), "*.csv")
- if not fileName:
- return
+ def export_history_dialog(self):
- try:
- with open(fileName, "w+") as csvfile:
- transaction = csv.writer(csvfile)
- transaction.writerow(["transaction_hash","label", "confirmations", "value", "fee", "balance", "timestamp"])
- for item in wallet.get_tx_history():
- tx_hash, confirmations, is_mine, value, fee, balance, timestamp = item
- if confirmations:
- if timestamp is not None:
- try:
- time_string = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
- except [RuntimeError, TypeError, NameError] as reason:
- time_string = "unknown"
- pass
- else:
- time_string = "unknown"
- else:
- time_string = "pending"
+ d = QDialog(self)
+ d.setWindowTitle(_('Export History'))
+ d.setMinimumSize(400, 200)
+ vbox = QVBoxLayout(d)
- if value is not None:
- value_string = format_satoshis(value, True)
- else:
- value_string = '--'
+ defaultname = os.path.expanduser('~/electrum-history.csv')
+ select_msg = _('Select file to export your wallet transactions to')
- if fee is not None:
- fee_string = format_satoshis(fee, True)
- else:
- fee_string = '0'
+ hbox, filename_e, csv_button = filename_field(self, self.config, defaultname, select_msg)
+ vbox.addLayout(hbox)
- if tx_hash:
- label, is_default_label = wallet.get_label(tx_hash)
- label = label.encode('utf-8')
- else:
- label = ""
+ vbox.addStretch(1)
- balance_string = format_satoshis(balance, False)
- transaction.writerow([tx_hash, label, confirmations, value_string, fee_string, balance_string, time_string])
- QMessageBox.information(None,_("CSV Export created"), _("Your CSV export has been successfully created."))
+ h, b = ok_cancel_buttons2(d, _('Export'))
+ vbox.addLayout(h)
+ if not d.exec_():
+ return
+ filename = filename_e.text()
+ if not filename:
+ return
+
+ try:
+ self.do_export_history(self.wallet, filename, csv_button.isChecked())
except (IOError, os.error), reason:
export_error_label = _("Electrum was unable to produce a transaction export.")
- QMessageBox.critical(None,_("Unable to create csv"), export_error_label + "\n" + str(reason))
+ QMessageBox.critical(self, _("Unable to export history"), export_error_label + "\n" + str(reason))
+ return
+
+ QMessageBox.information(self,_("History exported"), _("Your wallet history has been successfully exported."))
+
+
+ def do_export_history(self, wallet, fileName, is_csv):
+ history = wallet.get_tx_history()
+ lines = []
+ for item in history:
+ tx_hash, confirmations, is_mine, value, fee, balance, timestamp = item
+ if confirmations:
+ if timestamp is not None:
+ try:
+ time_string = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
+ except [RuntimeError, TypeError, NameError] as reason:
+ time_string = "unknown"
+ pass
+ else:
+ time_string = "unknown"
+ else:
+ time_string = "pending"
+
+ if value is not None:
+ value_string = format_satoshis(value, True)
+ else:
+ value_string = '--'
+
+ if fee is not None:
+ fee_string = format_satoshis(fee, True)
+ else:
+ fee_string = '0'
+
+ if tx_hash:
+ label, is_default_label = wallet.get_label(tx_hash)
+ label = label.encode('utf-8')
+ else:
+ label = ""
+
+ balance_string = format_satoshis(balance, False)
+ if is_csv:
+ lines.append([tx_hash, label, confirmations, value_string, fee_string, balance_string, time_string])
+ else:
+ lines.append({'txid':tx_hash, 'date':"%16s"%time_string, 'label':label, 'value':value_string})
+
+ with open(fileName, "w+") as f:
+ if is_csv:
+ transaction = csv.writer(f)
+ transaction.writerow(["transaction_hash","label", "confirmations", "value", "fee", "balance", "timestamp"])
+ for line in lines:
+ transaction.writerow(line)
+ else:
+ import json
+ f.write(json.dumps(lines, indent = 4))
def sweep_key_dialog(self):
diff --git a/gui/qt/util.py b/gui/qt/util.py
@@ -79,7 +79,6 @@ def text_dialog(parent, title, label, ok_label, default=None):
-
def address_field(addresses):
hbox = QHBoxLayout()
address_e = QLineEdit()
@@ -95,7 +94,49 @@ def address_field(addresses):
hbox.addWidget(address_e)
return hbox, address_e
-
+
+def filename_field(parent, config, defaultname, select_msg):
+
+ vbox = QVBoxLayout()
+ vbox.addWidget(QLabel(_("Format")))
+ gb = QGroupBox("format", parent)
+ b1 = QRadioButton(gb)
+ b1.setText(_("CSV"))
+ b1.setChecked(True)
+ b2 = QRadioButton(gb)
+ b2.setText(_("json"))
+ vbox.addWidget(b1)
+ vbox.addWidget(b2)
+
+ hbox = QHBoxLayout()
+
+ directory = config.get('io_dir', unicode(os.path.expanduser('~')))
+ path = os.path.join( directory, defaultname )
+ filename_e = QLineEdit()
+ filename_e.setText(path)
+
+ def func():
+ text = unicode(filename_e.text())
+ _filter = "*.csv" if text.endswith(".csv") else "*.json" if text.endswith(".json") else None
+ p = unicode( QFileDialog.getSaveFileName(None, select_msg, text, _filter))
+ if p:
+ filename_e.setText(p)
+
+ button = QPushButton(_('File'))
+ button.clicked.connect(func)
+ hbox.addWidget(button)
+ hbox.addWidget(filename_e)
+ vbox.addLayout(hbox)
+
+ def set_csv(v):
+ text = unicode(filename_e.text())
+ text = text.replace(".json",".csv") if v else text.replace(".csv",".json")
+ filename_e.setText(text)
+
+ b1.clicked.connect(lambda: set_csv(True))
+ b2.clicked.connect(lambda: set_csv(False))
+
+ return vbox, filename_e, b1