|
|
@ -4,6 +4,12 @@ from tkinter import filedialog |
|
|
|
|
|
|
|
|
|
|
|
import validators |
|
|
|
import validators |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_STATE_1 = 'no valid files selected' |
|
|
|
|
|
|
|
APP_STATE_2 = 'no valid fields selected' |
|
|
|
|
|
|
|
APP_STATE_3 = 'ok, single files' |
|
|
|
|
|
|
|
APP_STATE_4 = 'ok, multiple files' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StatusPanel(tk.Frame): |
|
|
|
class StatusPanel(tk.Frame): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent): |
|
|
|
def __init__(self, parent): |
|
|
@ -48,12 +54,15 @@ class FieldPanel(tk.Frame): |
|
|
|
self._parent = parent |
|
|
|
self._parent = parent |
|
|
|
self.is_single = tk.BooleanVar() |
|
|
|
self.is_single = tk.BooleanVar() |
|
|
|
self.is_single.set(True) |
|
|
|
self.is_single.set(True) |
|
|
|
self.rbtn_single = tk.Radiobutton(self, text='single excel files', variable=self.is_single, value=True, command=self.disable_listbox) |
|
|
|
self.rbtn_single = tk.Radiobutton( |
|
|
|
self.rbtn_combine = tk.Radiobutton(self, text='one combined excel file', variable=self.is_single, value=False, command=self.enable_listbox) |
|
|
|
self, text='single excel files', variable=self.is_single, value=True, command=self.disable_listbox) |
|
|
|
|
|
|
|
self.rbtn_combine = tk.Radiobutton( |
|
|
|
|
|
|
|
self, text='one combined excel file', variable=self.is_single, value=False, command=self.enable_listbox) |
|
|
|
self.rbtn_single.pack(anchor=tk.W) |
|
|
|
self.rbtn_single.pack(anchor=tk.W) |
|
|
|
self.rbtn_combine.pack(anchor=tk.W) |
|
|
|
self.rbtn_combine.pack(anchor=tk.W) |
|
|
|
self.listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, activestyle='none') |
|
|
|
self.listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, activestyle='none', height=19) |
|
|
|
self.listbox.pack(fill=tk.X, ipady=5, ipadx=5) |
|
|
|
self.listbox.pack(fill=tk.X, ipady=5, ipadx=5, ) |
|
|
|
|
|
|
|
self.listbox.bind('<<ListboxSelect>>', self._parent.toggle_run_button) |
|
|
|
self.pack(side=tk.TOP, fill=tk.X) |
|
|
|
self.pack(side=tk.TOP, fill=tk.X) |
|
|
|
|
|
|
|
|
|
|
|
def disable_radio_btn(self): |
|
|
|
def disable_radio_btn(self): |
|
|
@ -62,6 +71,7 @@ class FieldPanel(tk.Frame): |
|
|
|
|
|
|
|
|
|
|
|
def disable_listbox(self): |
|
|
|
def disable_listbox(self): |
|
|
|
self.listbox.config(state="disabled") |
|
|
|
self.listbox.config(state="disabled") |
|
|
|
|
|
|
|
self._parent.toggle_run_button() |
|
|
|
|
|
|
|
|
|
|
|
def disable(self): |
|
|
|
def disable(self): |
|
|
|
self.disable_listbox() |
|
|
|
self.disable_listbox() |
|
|
@ -74,6 +84,7 @@ class FieldPanel(tk.Frame): |
|
|
|
|
|
|
|
|
|
|
|
def enable_listbox(self): |
|
|
|
def enable_listbox(self): |
|
|
|
self.listbox.config(state="normal") |
|
|
|
self.listbox.config(state="normal") |
|
|
|
|
|
|
|
self._parent.toggle_run_button() |
|
|
|
|
|
|
|
|
|
|
|
def enable(self): |
|
|
|
def enable(self): |
|
|
|
self.enable_listbox() |
|
|
|
self.enable_listbox() |
|
|
@ -91,13 +102,12 @@ class FieldPanel(tk.Frame): |
|
|
|
self.disable_listbox() |
|
|
|
self.disable_listbox() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActionPanel(tk.Frame): |
|
|
|
class ActionPanel(tk.Frame): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent): |
|
|
|
def __init__(self, parent): |
|
|
|
tk.Frame.__init__(self, parent.master) |
|
|
|
tk.Frame.__init__(self, parent.master) |
|
|
|
self._parent = parent |
|
|
|
self._parent = parent |
|
|
|
self.btn_go = tk.Button(self, text="GO!", command=self._parent.quit) |
|
|
|
self.btn_go = tk.Button(self, text="GO!", command=self._parent.convert_files) |
|
|
|
self.btn_go.pack(side=tk.LEFT, pady=5, padx=5) |
|
|
|
self.btn_go.pack(side=tk.LEFT, pady=5, padx=5) |
|
|
|
self.btn_quit = tk.Button(self, text="Quit", command=self._parent.quit) |
|
|
|
self.btn_quit = tk.Button(self, text="Quit", command=self._parent.quit) |
|
|
|
self.btn_quit.pack(side=tk.RIGHT, pady=5, padx=5) |
|
|
|
self.btn_quit.pack(side=tk.RIGHT, pady=5, padx=5) |
|
|
@ -113,10 +123,12 @@ class ActionPanel(tk.Frame): |
|
|
|
class Application(tk.Frame): |
|
|
|
class Application(tk.Frame): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, master): |
|
|
|
def __init__(self, master): |
|
|
|
master.minsize(height=330, width=200) |
|
|
|
master.minsize(height=330, width=300) |
|
|
|
tk.Frame.__init__(self, master) |
|
|
|
tk.Frame.__init__(self, master) |
|
|
|
self._master = master |
|
|
|
self._master = master |
|
|
|
self.pack(fill=tk.BOTH) |
|
|
|
self.pack(fill=tk.BOTH) |
|
|
|
|
|
|
|
self.file_validator = None |
|
|
|
|
|
|
|
self.validated_files = None |
|
|
|
self.status_panel = StatusPanel(self) |
|
|
|
self.status_panel = StatusPanel(self) |
|
|
|
self.file_panel = FilePanel(self) |
|
|
|
self.file_panel = FilePanel(self) |
|
|
|
self.field_panel = FieldPanel(self) |
|
|
|
self.field_panel = FieldPanel(self) |
|
|
@ -124,6 +136,7 @@ class Application(tk.Frame): |
|
|
|
self.reset() |
|
|
|
self.reset() |
|
|
|
|
|
|
|
|
|
|
|
def reset(self): |
|
|
|
def reset(self): |
|
|
|
|
|
|
|
self.status_panel.clear_text() |
|
|
|
self.field_panel.disable() |
|
|
|
self.field_panel.disable() |
|
|
|
self.action_panel.disable() |
|
|
|
self.action_panel.disable() |
|
|
|
self.file_validator = None |
|
|
|
self.file_validator = None |
|
|
@ -145,13 +158,53 @@ class Application(tk.Frame): |
|
|
|
self.validated_files = [f for f in validators.validate_files(file_selection, self.file_validator)] |
|
|
|
self.validated_files = [f for f in validators.validate_files(file_selection, self.file_validator)] |
|
|
|
status = '%d of %d valid %s files found' % (len(self.validated_files), len(file_selection), self.file_validator.type) |
|
|
|
status = '%d of %d valid %s files found' % (len(self.validated_files), len(file_selection), self.file_validator.type) |
|
|
|
self.file_panel.set_text(status) |
|
|
|
self.file_panel.set_text(status) |
|
|
|
if len(self.validated_files) == 0: |
|
|
|
if self._state == APP_STATE_1: |
|
|
|
self.reset() |
|
|
|
return self.reset() |
|
|
|
return |
|
|
|
|
|
|
|
self.field_panel.enable_radio_btn() |
|
|
|
self.field_panel.enable_radio_btn() |
|
|
|
self.field_panel.set_listbox_content(self.file_validator.data_fields, self.file_validator.defaults) |
|
|
|
self.field_panel.set_listbox_content(self.file_validator.data_fields, self.file_validator.defaults) |
|
|
|
|
|
|
|
self.toggle_run_button() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def toggle_run_button(self, event=None): |
|
|
|
|
|
|
|
self.action_panel.disable() |
|
|
|
|
|
|
|
if self._state in (APP_STATE_3, APP_STATE_4): |
|
|
|
|
|
|
|
self.action_panel.enable() |
|
|
|
|
|
|
|
|
|
|
|
def quit(self): |
|
|
|
def quit(self): |
|
|
|
self._master.destroy() |
|
|
|
self._master.destroy() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_files(self): |
|
|
|
|
|
|
|
state = self._state |
|
|
|
|
|
|
|
if state not in (APP_STATE_3, APP_STATE_4): |
|
|
|
|
|
|
|
return self.reset() |
|
|
|
|
|
|
|
i = len(self.validated_files) |
|
|
|
|
|
|
|
if state == APP_STATE_3: |
|
|
|
|
|
|
|
text = 'Converting %d data files to %d excel files ...' % (i, i) |
|
|
|
|
|
|
|
self.status_panel.set_text(text) |
|
|
|
|
|
|
|
self._convert_to_single_files() |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
text = 'Combining %d data files into one excel file ...' % i |
|
|
|
|
|
|
|
self.status_panel.set_text(text) |
|
|
|
|
|
|
|
self._combine_data_files() |
|
|
|
|
|
|
|
self.status_panel.set_text(text + ' Done') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
|
|
def _state(self): |
|
|
|
|
|
|
|
c = self.field_panel.rbtn_single.config() |
|
|
|
|
|
|
|
if not self.validated_files or c['state'] == 'disabled': |
|
|
|
|
|
|
|
return APP_STATE_1 |
|
|
|
|
|
|
|
elif self.field_panel.is_single.get(): |
|
|
|
|
|
|
|
return APP_STATE_3 |
|
|
|
|
|
|
|
elif self.field_panel.listbox.curselection(): |
|
|
|
|
|
|
|
return APP_STATE_4 |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
return APP_STATE_2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_to_single_files(self): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _combine_data_files(self): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|