-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheasyBankcsv2qifFrontend.py
executable file
·192 lines (143 loc) · 5.04 KB
/
easyBankcsv2qifFrontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python
'''
Author: [email protected]
Date: 16. Feb 2014
'''
from __future__ import print_function
import sys, os
import os.path
import json
import easyBankcsv2qif
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
DEFAULT_ENC_FROM = 'iso-8859-1'
DEFAULT_ENC_TO = 'utf-8'
DEFAULT_CONFIG_FILE = os.path.expanduser(
'~/.config/easyBankcsv2qifFrontend.conf')
CONF_DIR_ACCOUNTS = 'accountnames'
class Frontend(object):
def __init__(self):
object.__init__(self)
self._config = None
self.readConfigFile()
self.account = None
self.newFilename = None
def saveDialog(self, filename):
dialog = Gtk.FileChooserDialog(title="Please choose a file",
parent=None,
action=Gtk.FileChooserAction.SAVE)
dialog.add_buttons(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE,
Gtk.ResponseType.OK)
dialog.set_do_overwrite_confirmation(True)
dialog.set_current_name(filename)
dialog.props.title = "save file in QIF format, choose a name..."
# add a ComboBoxText to specify the Name of the Account
# to be specified in the QIF file
box = dialog.get_content_area()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
box.add(hbox)
label = Gtk.Label(label="Select Account name:")
combo = Gtk.ComboBoxText.new_with_entry()
hbox.pack_start(label, True, True, 10)
hbox.pack_start(combo, True, True, 10)
# add known account names to the combobox
for i in self._config[CONF_DIR_ACCOUNTS]:
combo.append_text(i)
# select the first one
if self._config[CONF_DIR_ACCOUNTS]:
combo.set_active(0)
dialog.show_all()
# show the dialog and wait for user response
response = dialog.run()
self.newFilename = None
if response == Gtk.ResponseType.OK:
self.newFilename = dialog.get_filename()
self.account = combo.get_active_text()
# write new account to the config file
if len(self.account) > 0 \
and self.account not in self._config[CONF_DIR_ACCOUNTS]:
self._config[CONF_DIR_ACCOUNTS].append(self.account)
self.writeConfigFile()
dialog.destroy()
return self.newFilename
def processedDialog(self, message):
dialog = Gtk.MessageDialog(message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK)
dialog.props.title = "EasyBank CSV to QIF finished"
dialog.props.text = "CSV 2 QIF converstion finished"
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
def errorMessage(self, headline, message):
dialog = Gtk.MessageDialog(message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK)
dialog.props.title = "Error"
dialog.props.text = headline
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
def readConfigFile(self):
conf = None
try:
confFile = open(DEFAULT_CONFIG_FILE, 'r')
conf = json.load(confFile)
confFile.close()
except IOError as detail:
print(detail)
# check/initialize configuration
if type(conf) != dict:
conf = {}
if CONF_DIR_ACCOUNTS not in conf:
conf[CONF_DIR_ACCOUNTS] = []
self._config = conf
return conf
def writeConfigFile(self, conf = None):
if conf == None:
conf = self._config
try:
confFile = open(DEFAULT_CONFIG_FILE, 'w')
json.dump(conf, confFile)
confFile.close()
except IOError as detail:
print(detail)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: " + sys.argv[0] + " <filename>")
sys.exit(1)
f = Frontend()
csvFilename = sys.argv[1]
instream = None
try:
instream = open(csvFilename, mode='r', encoding=DEFAULT_ENC_FROM)
except IOError as detail:
f.errorMessage("could not open input file '" + csvFilename + "'",
str(detail))
sys.exit(1)
newFilename = os.path.basename(csvFilename)
newFilename += ".qif"
f.saveDialog(newFilename)
newFilename = f.newFilename
account = f.account or ''
if newFilename == None:
instream.close()
sys.exit(0)
outstream = None
try:
outstream = open(newFilename, mode='w', encoding=DEFAULT_ENC_TO)
except IOError as detail:
f.errorMessage("could not open output file", str(detail))
instream.close()
sys.exit(1)
# do processing here
converter = easyBankcsv2qif.EasyCSV2QIFconverter(instream, outstream, account)
converter.convert()
instream.close()
outstream.close()
# show summary dialog
f.processedDialog(converter.getSummary())
# remove the csv file
# os.unlink(csvFilename) better keep them