-
Notifications
You must be signed in to change notification settings - Fork 21
/
csv_tool.py
executable file
·381 lines (293 loc) · 10.7 KB
/
csv_tool.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
"""
csv_tool.py
This is a small, simple app that simply reads, writes, and lets you edit CSV files.
It's not a spreadsheet, as it doesn't handle any calculations or anything -- just text in CSV files
"""
import os
import sys
import csv
import wx
import wx.grid
# empty table:
empty_table = []
for i in range(30):
row = []
for j in range(8):
row.append("")
empty_table.append(row)
class CSVTable(wx.grid.PyGridTableBase):
def __init__(self, filename=None):
wx.grid.PyGridTableBase.__init__(self)
if filename is None:
self.table = empty_table
self._comp_size()
else:
self.LoadFromFile(filename)
def _comp_size(self):
cols = 0
for row in self.table:
cols = max(len(row), cols)
self.num_cols = cols
self.num_rows = len(self.table)
def LoadFromFile(self, filename):
try:
reader = csv.reader(file(filename, 'rU'),
dialect='excel') # [optional keyword args])
except csv.Error:
dlg = wx.E
self.table = [row for row in reader]
self._comp_size()
def Save(self, filename):
"""
save the table to the given filename
"""
# first cleanout empty rows
self.RemoveEmptyStuff()
writer = csv.writer(file(filename, 'wb'),
dialect='excel')
writer.writerows(self.table)
def RemoveEmptyStuff(self):
"""
removes empty rows at the end of the table
and columns at the right of the table
not used right now....
"""
# strip the extra rows
for i in range(len(self.table)-1, -1, -1):
empty_row = True
for j, cell in enumerate(self.table[i]):
if cell:
empty_row=False
if empty_row:
del self.table[i]
else:
break
# look for max columns:
max_col = 0
for row in self.table:
for j, cell in enumerate(row):
if cell:
max_col = max(j, max_col)
# strip the extra columns
for i in range(len(self.table)):
self.table[i] = self.table[i][:max_col+1]
self._comp_size()
def GetNumberRows(self):
"""Return the number of rows in the grid"""
return len(self.table)
def GetNumberCols(self):
"""Return the number of columns in the grid"""
return self.num_cols
def IsEmptyCell(self, row, col):
"""Return True if the cell is empty"""
try:
self.table[row][col]
except IndexError:
return True
return False
def GetTypeName(self, row, col):
"""Return the name of the data type of the value in the cell"""
return wx.grid.GRID_VALUE_STRING
def GetValue(self, row, col):
"""Return the value of a cell"""
print "GetValue called:", row, col
try:
return self.table[row][col]
except IndexError:
return ""
def SetValue(self, row, col, value):
"""Set the value of a cell"""
self.table[row][col] = value
class ButtonBar(wx.Panel):
def __init__(self, Grid, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
self.Grid = Grid
self.MainFrame = parent
OpenButton = wx.Button(self, label="Open")
OpenButton.Bind(wx.EVT_BUTTON, self.OnOpen)
SaveButton = wx.Button(self, label="Save")
SaveButton.Bind(wx.EVT_BUTTON, self.OnSave)
SaveAsButton = wx.Button(self, label="Save As")
SaveAsButton.Bind(wx.EVT_BUTTON, self.OnSaveAs)
AutoSizeButton = wx.Button(self, label="AutoSize")
AutoSizeButton.Bind(wx.EVT_BUTTON, self.OnAutoSize)
S = wx.BoxSizer(wx.HORIZONTAL)
S.Add(OpenButton, 0, wx.ALL, 5)
S.Add(SaveButton, 0, wx.ALL, 5)
S.Add(SaveAsButton, 0, wx.ALL, 5)
S.Add(AutoSizeButton, 0, wx.ALL, 5)
self.SetSizer(S)
def OnAutoSize(self, evt=None):
self.Grid.AutoSize()
def OnOpen(self, evt=None):
self.MainFrame.OnOpen()
def OnSave(self, evt=None):
self.MainFrame.OnSave()
def OnSaveAs(self, evt=None):
self.MainFrame.OnSaveAs()
class CSVGrid(wx.grid.Grid):
def __init__(self, *args, **kwargs):
wx.grid.Grid.__init__(self, *args, **kwargs)
# set up the TableBase
self.table = CSVTable()
self.SetTable( self.table )
def LoadNewFile(self, filename):
self.table.LoadFromFile(filename)
self.SetTable(self.table)
#self.AutoSize()
self.ForceRefresh()
#self.SetTable( table)
def SaveFile(self, filename):
self.table.Save(filename)
self.SetTable(self.table)
class CSVFrame(wx.Frame):
def __init__(self, title = "CSV Editor"):
wx.Frame.__init__(self, None, size = (800, 600), title=title)
##Build the menu bar
MenuBar = wx.MenuBar()
FileMenu = wx.Menu()
item = FileMenu.Append(wx.ID_EXIT, text = "&Exit")
self.Bind(wx.EVT_MENU, self.OnQuit, item)
item = FileMenu.Append(wx.ID_ANY, text = "&Open")
self.Bind(wx.EVT_MENU, self.OnOpen, item)
item = FileMenu.Append(wx.ID_ANY, text = "&Save")
self.Bind(wx.EVT_MENU, self.OnSave, item)
item = FileMenu.Append(wx.ID_ANY, text = "&SaveAs")
self.Bind(wx.EVT_MENU, self.OnSaveAs, item)
item = FileMenu.Append(wx.ID_PREFERENCES, text = "&Preferences")
self.Bind(wx.EVT_MENU, self.OnPrefs, item)
MenuBar.Append(FileMenu, "&File")
HelpMenu = wx.Menu()
item = HelpMenu.Append(wx.ID_HELP, "CSV &Help",
"Help for this simple CSV reader")
self.Bind(wx.EVT_MENU, self.OnHelp, item)
## this gets put in the App menu on OS-X
item = HelpMenu.Append(wx.ID_ABOUT, "&About",
"More information About this program")
self.Bind(wx.EVT_MENU, self.OnAbout, item)
MenuBar.Append(HelpMenu, "&Help")
self.SetMenuBar(MenuBar)
self.grid = CSVGrid(self)
self.ButtonBar = ButtonBar(self.grid, self)
S = wx.BoxSizer(wx.VERTICAL)
S.Add(self.ButtonBar, 0, wx.EXPAND)
S.Add(self.grid, 1, wx.EXPAND)
self.SetSizer(S)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.CurrentFilename = ""
def OpenFile(self, filename):
self.grid.LoadNewFile(filename)
self.CurrentFilename = os.path.abspath(filename)
self.SetTitle(filename)
self.grid.Layout()
self.grid.Refresh()
self.grid.Update()
def OnQuit(self,Event):
self.Destroy()
def OnAbout(self, event):
dlg = wx.MessageDialog(self,
"This is a small program to view\n"
"and edit simple CSV files\n",
"About Me", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnHelp(self, event):
dlg = wx.MessageDialog(self, "This would be help\n"
"If there was any\n",
"CSV Help", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnOpen(self, event=None):
if self.CurrentFilename:
cur_dir, cur_name = os.path.split(self.CurrentFilename)
else:
cur_dir = "."
cur_name = ""
dlg = wx.FileDialog(self, 'Choose a csv file to open',
cur_dir,
'',
'*.csv',
wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
f = dlg.GetPath()
self.OpenFile(f)
dlg.Destroy()
def OnSave(self, event=None):
if self.CurrentFilename:
self.grid.SaveFile(self.CurrentFilename)
else:
self.OnSaveAs(event)
def OnSaveAs(self, event=None):
self.CurrentFilename
if self.CurrentFilename:
cur_dir, cur_name = os.path.split(self.CurrentFilename)
else:
cur_dir = os.getcwd()
cur_name = ""
dlg = wx.FileDialog(self, 'filename to save',
cur_dir,
cur_name,
'*.csv',
wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
self.grid.SaveFile(filename)
dlg.Destroy()
def OnPrefs(self, event):
dlg = wx.MessageDialog(self,
"This would be an preferences Dialog\n"
"If there were any preferences to set.\n",
"Preferences", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
class MyApp(wx.App):
def __init__(self, *args, **kwargs):
wx.App.__init__(self, *args, **kwargs)
# This catches events when the app is asked to activate by some other
# process
self.Bind(wx.EVT_ACTIVATE_APP, self.OnActivate)
def OnInit(self):
self.frame = CSVFrame()
self.frame.Show()
import sys
try:
f = sys.argv[1]
self.frame.OpenFile(f)
except IndexError:
pass
return True
def BringWindowToFront(self):
try: # it's possible for this event to come when the frame is closed
self.GetTopWindow().Raise()
except:
pass
def OnActivate(self, event):
# if this is an activate event, rather than something else, like iconize.
if event.GetActive():
self.BringWindowToFront()
event.Skip()
def OpenFileMessage(self, filename):
dlg = wx.MessageDialog(None,
"This app was just asked to open:\n%s\n"%filename,
"File Dropped",
wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
self.frame.OpenFile(filename)
def MacOpenFile(self, filename):
"""Called for files droped on dock icon, or opened via finders context menu"""
if filename == sys.argv[0]:
pass # there was no filename in command line
else:
self.frame.OpenFile(filename)
def MacReopenApp(self):
"""Called when the doc icon is clicked, and ???"""
self.BringWindowToFront()
def MacNewFile(self):
pass
def MacPrintFile(self, file_path):
pass
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()