# -*- coding: utf-8 -*-
from traceback import format_exc
import wx
from library import i18n
_ = i18n.i18n()
import db_lib
class pnl(wx.Panel):
"""
"""
def __init__(self, parent, paths, db_inst, modify):
"""
"""
super(pnl, self).__init__(parent)
self.parent = parent
self.db_inst = db_inst
self.paths = paths
self.modify, self.name = modify
self._txt_fb_name = wx.TextCtrl(self)
self._txt_path = wx.TextCtrl(self, size=(200, -1))
bt_search = wx.Button(self, label=_("bt_search"))
self._ch_table_list = wx.Choice(self)
bt_save = wx.Button(self, label=_("bt_save"))
sz_all = wx.FlexGridSizer(0, 3, 5, 5)
flags = wx.ALIGN_CENTER | wx.ALL
sz_all.Add(wx.StaticText(self, label=_("lbl_wizard_file_fb_name")), 0, flags, 10)
sz_all.Add(self._txt_fb_name, 0, flags | wx.EXPAND, 10)
sz_all.Add((0,0))
sz_all.Add(wx.StaticText(self, label=_("lbl_wizard_file_path")), 0, flags, 10)
sz_all.Add(self._txt_path, 0, flags, 10)
sz_all.Add(bt_search, 0, flags, 10)
sz_all.Add(wx.StaticText(self, label=_("lbl_wizard_file_tables")), 0, flags, 10)
sz_all.Add(self._ch_table_list, 0, flags | wx.EXPAND, 10)
sz_all.Add((0,0))
sz_all.Add((0,0))
sz_all.Add(bt_save, 0, flags)
sz_all.Add((0,0))
#event bind
bt_search.Bind(wx.EVT_BUTTON, self.OnButtonSearch)
bt_save.Bind(wx.EVT_BUTTON, self.OnButtonSave)
self.SetSizerAndFit(sz_all)
def OnButtonSearch(self, evt):
"""
"""
dlg = wx.FileDialog(self)
if dlg.ShowModal() == wx.ID_OK:
file_path = dlg.GetPath()
else:
file_path = None
if not file_path:
return
self._txt_path.SetValue(file_path)
try:
self.paths.log4write.write("Try to open %s" % file_path)
self._db = db_lib.Sqlite(file_path)
table_list = self._db.conn()
except:
err = format_exc()
self.paths.log4write.write("Error on open db:, %s" % err)
wx.MessageBox(err, _("error"), wx.ICON_ERROR)
return
self._ch_table_list.Clear()
if table_list:
self._ch_table_list.AppendItems(table_list)
def OnButtonSave(self, evt):
"""
"""
fb_name = self._txt_fb_name.GetValue().strip()
db_path = self._txt_path.GetValue().strip()
table_name = self._ch_table_list.GetStringSelection()
if not (fb_name and table_name and db_path):
return
fb_other = "%s," % db_path
ret, cols = self._db.get_cols(table_name)
if ret:
return
#delete id if found
cols = [x for x in cols if not x == "id"]
status, ret = self.db_inst.queryReturn('SELECT name FROM fb WHERE name = ?', (fb_name, ))
if ret:
wx.MessageBox(_("_db_exist"), _("error", 1), wx.ICON_ERROR)
self.paths.log4write.write("db exists, %s" % fb_name)
self.paths.log4write.write("db exists query: %s, %s" % (status, ret))
return
status, ret_order = self.db_inst.queryReturn('SELECT fb_order FROM fb ORDER BY fb_order')
if status:
self.err_generic(ret)
return
if ret_order:
last_number = ret_order[len(ret_order) -1][0]
else:
last_number = 0
self.db_inst.queryReturn('INSERT INTO fb (name, fb_type, fb_order, fb_fields, fb_db_name, fb_other) VALUES \
("%s", "file", %s, "%s", "%s", "%s")' %\
(fb_name, last_number +1 , ','.join(cols), table_name, fb_other)
)
self.parent.update_parent(fb_name)
|