- Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcsv_tool.py
More file actions
Latest commit
executable file
·381 lines (293 loc) · 10.7 KB
/
Copy pathcsv_tool.py
File metadata and controls
executable file
·381 lines (293 loc) · 10.7 KB
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
"""
importos
importsys
importcsv
importwx
importwx.grid
# empty table:
empty_table= []
foriinrange(30):
row= []
forjinrange(8):
row.append("")
empty_table.append(row)
classCSVTable(wx.grid.PyGridTableBase):
def__init__(self, filename=None):
wx.grid.PyGridTableBase.__init__(self)
iffilenameisNone:
self.table=empty_table
self._comp_size()
else:
self.LoadFromFile(filename)
def_comp_size(self):
cols=0
forrowinself.table:
cols=max(len(row), cols)
self.num_cols=cols
self.num_rows=len(self.table)
defLoadFromFile(self, filename):
try:
reader=csv.reader(file(filename, 'rU'),
dialect='excel') # [optional keyword args])
exceptcsv.Error:
dlg=wx.E
self.table= [rowforrowinreader]
self._comp_size()
defSave(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)
defRemoveEmptyStuff(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
foriinrange(len(self.table)-1, -1, -1):
empty_row=True
forj, cellinenumerate(self.table[i]):
ifcell:
empty_row=False
ifempty_row:
delself.table[i]
else:
break
# look for max columns:
max_col=0
forrowinself.table:
forj, cellinenumerate(row):
ifcell:
max_col=max(j, max_col)
# strip the extra columns
foriinrange(len(self.table)):
self.table[i] =self.table[i][:max_col+1]
self._comp_size()
defGetNumberRows(self):
"""Return the number of rows in the grid"""
returnlen(self.table)
defGetNumberCols(self):
"""Return the number of columns in the grid"""
returnself.num_cols
defIsEmptyCell(self, row, col):
"""Return True if the cell is empty"""
try:
self.table[row][col]
exceptIndexError:
returnTrue
returnFalse
defGetTypeName(self, row, col):
"""Return the name of the data type of the value in the cell"""
returnwx.grid.GRID_VALUE_STRING
defGetValue(self, row, col):
"""Return the value of a cell"""
print"GetValue called:", row, col
try:
returnself.table[row][col]
exceptIndexError:
return""
defSetValue(self, row, col, value):
"""Set the value of a cell"""
self.table[row][col] =value
classButtonBar(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)
defOnAutoSize(self, evt=None):
self.Grid.AutoSize()
defOnOpen(self, evt=None):
self.MainFrame.OnOpen()
defOnSave(self, evt=None):
self.MainFrame.OnSave()
defOnSaveAs(self, evt=None):
self.MainFrame.OnSaveAs()
classCSVGrid(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 )
defLoadNewFile(self, filename):
self.table.LoadFromFile(filename)
self.SetTable(self.table)
#self.AutoSize()
self.ForceRefresh()
#self.SetTable( table)
defSaveFile(self, filename):
self.table.Save(filename)
self.SetTable(self.table)
classCSVFrame(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=""
defOpenFile(self, filename):
self.grid.LoadNewFile(filename)
self.CurrentFilename=os.path.abspath(filename)
self.SetTitle(filename)
self.grid.Layout()
self.grid.Refresh()
self.grid.Update()
defOnQuit(self,Event):
self.Destroy()
defOnAbout(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()
defOnHelp(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()
defOnOpen(self, event=None):
ifself.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)
ifdlg.ShowModal() ==wx.ID_OK:
f=dlg.GetPath()
self.OpenFile(f)
dlg.Destroy()
defOnSave(self, event=None):
ifself.CurrentFilename:
self.grid.SaveFile(self.CurrentFilename)
else:
self.OnSaveAs(event)
defOnSaveAs(self, event=None):
self.CurrentFilename
ifself.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)
ifdlg.ShowModal() ==wx.ID_OK:
filename=dlg.GetPath()
self.grid.SaveFile(filename)
dlg.Destroy()
defOnPrefs(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()
classMyApp(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)
defOnInit(self):
self.frame=CSVFrame()
self.frame.Show()
importsys
try:
f=sys.argv[1]
self.frame.OpenFile(f)
exceptIndexError:
pass
returnTrue
defBringWindowToFront(self):
try: # it's possible for this event to come when the frame is closed
self.GetTopWindow().Raise()
except:
pass
defOnActivate(self, event):
# if this is an activate event, rather than something else, like iconize.
ifevent.GetActive():
self.BringWindowToFront()
event.Skip()
defOpenFileMessage(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)
defMacOpenFile(self, filename):
"""Called for files droped on dock icon, or opened via finders context menu"""
iffilename==sys.argv[0]:
pass# there was no filename in command line
else:
self.frame.OpenFile(filename)
defMacReopenApp(self):
"""Called when the doc icon is clicked, and ???"""
self.BringWindowToFront()
defMacNewFile(self):
pass
defMacPrintFile(self, file_path):
pass
if__name__=="__main__":
app=MyApp(False)
app.MainLoop()