Skip to content

Commit

Permalink
Pypi version 0.7.2 , remember last directory
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebig committed Jun 19, 2014
1 parent f35036d commit da3db1d
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 120 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Changelog
=========


2014-06-19a : v0.7.2 'Remember me'
----------------------------------

* keep memory of last directory used


2014-06-17a : v0.7.1
--------------------

Expand Down
253 changes: 133 additions & 120 deletions sqlite_bro.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class App:
"""the GUI graphic application"""
def __init__(self):
"""create a tkk graphic interface with a main window tk_win"""
self.__version__ = '0.7.1'
self._title= "2014-06-17a : Pypi done right !"
self.__version__ = '0.7.2'
self._title= "2014-06-19a : 'Remember me'"
self.conn = None # Baresql database object
self.database_file = ""
self.tk_win = Tk()
Expand All @@ -41,6 +41,7 @@ def __init__(self):

self.font_size = 10
self.font_wheight = 0
self.initialdir = "."
# With a Menubar and Toolbar
self.create_menu()
self.create_toolbar()
Expand Down Expand Up @@ -108,14 +109,13 @@ def create_toolbar(self):
x.n.notebook.select()), "Clear script result"),
('newtab_img', lambda x=self: x.n.new_query_tab("___", ""),
"Create a new script"),
('csvin_img', lambda x=self: import_csvtb([x.conn, x.actualize_db]),
"Import a CSV file into a table"),
('csvex_img', lambda x=self: export_csvtb([x.conn, x.db_tree]),
"Export selected table to a CSV file"),
('csvin_img', self.import_csvtb, "Import a CSV file into a table"),
('csvex_img', self.export_csvtb,
"Export selected table to a CSV file"),
('dbdef_img', self.savdb_script,
"Save main database as a SQL script"),
('qryex_img', lambda x=self: export_csvqr([x.conn, x.n]),
"Export script selection to a CSV file"),
"Save main database as a SQL script"),
('qryex_img',self.export_csvqr,
"Export script selection to a CSV file"),
('exe_img', self.exsav_script,
"Run script+output to a file (First 200 rec. per Qry)"),
('sqlin_img', self.load_script, "Load a SQL script file"),
Expand All @@ -127,17 +127,23 @@ def create_toolbar(self):
b.pack(side=LEFT, padx=2, pady=2)
self.createToolTip(b, tip)

def set_initialdir(self, proposal):
"""change initial dir, if possible"""
if os.path.isfile(proposal):
self.initialdir = os.path.split(proposal)

def new_db(self, filename=''):
"""create a new database"""
if filename == '':
filename = filedialog.asksaveasfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
title="Define a new database name and location",
filetypes=[("default", "*.db"), ("other", "*.db*"),
("all", "*.*")])
if filename != '':
self.database_file = filename
if os.path.isfile(filename):
self.set_initialdir(filename)
if messagebox.askyesno(
message='Confirm Destruction of previous Datas ?',
icon='question', title='Destroying'):
Expand All @@ -148,22 +154,24 @@ def new_db(self, filename=''):
def open_db(self):
"""open an existing database"""
filename = filedialog.askopenfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
filetypes=[("default", "*.db"), ("other", "*.db*"),
("all", "*.*")])
if filename != '':
self.set_initialdir(filename)
self.database_file = filename
self.conn = Baresql(self.database_file)
self.actualize_db()

def load_script(self):
"""load a script file, ask validation of detected Python code"""
filename = filedialog.askopenfilename(
defaultextension='.sql',
initialdir=self.initialdir, defaultextension='.sql',
filetypes=[("default", "*.sql"), ("other", "*.txt"),
("all", "*.*")])
if filename != '':
text = ((filename.replace("\\", "/")).split("/")[-1]).split(".")[0]
self.set_initialdir(filename)
text = os.path.split(filename)[1].split(".")[0]
with io.open(filename, encoding=guess_encoding(filename)[0]) as f:
script = f.read()
sqls = self.conn.get_sqlsplit(script, remove_comments=True)
Expand All @@ -186,11 +194,12 @@ def load_script_ok(self, thetop, entries, actions):
def savdb_script(self):
"""save database as a script file"""
filename = filedialog.asksaveasfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
title="save database structure in a text file",
filetypes=[("default", "*.sql"), ("other", "*.txt"),
("all", "*.*")])
if filename != '':
self.set_initialdir(filename)
with io.open(filename, 'w', encoding='utf-8') as f:
for line in self.conn.iterdump():
f.write('%s\n' % line)
Expand All @@ -203,11 +212,12 @@ def sav_script(self):
fw = self.n.fw_labels[active_tab_id]
script = fw.get(1.0, END)[:-1]
filename = filedialog.asksaveasfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
title="save script in a sql file",
filetypes=[("default", "*.sql"), ("other", "*.txt"),
("all", "*.*")])
if filename != "":
self.set_initialdir(filename)
with io.open(filename, 'w', encoding='utf-8') as f:
if "你好 мир Artisou à croute" not in script:
f.write("/*utf-8 tag : 你好 мир Artisou à croute*/\n")
Expand All @@ -216,13 +226,14 @@ def sav_script(self):
def attach_db(self):
"""attach an existing database"""
filename = filedialog.askopenfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
title="Choose a database to attach ",
filetypes=[("default", "*.db"), ("other", "*.db*"),
("all", "*.*")])
attach = ((filename.replace("\\", "/")).split("/")[-1]).split(".")[0]
attach = os.path.split(filename)[1].split(".")[0]

if filename != '':
self.set_initialdir(filename)
attach_order = "ATTACH DATABASE '%s' as '%s' " % (filename, attach)
self.conn.execute(attach_order)
self.actualize_db()
Expand Down Expand Up @@ -297,12 +308,13 @@ def exsav_script(self):
fw = self.n.fw_labels[active_tab_id]
script = fw.get(1.0, END)[:-1]
filename = filedialog.asksaveasfilename(
defaultextension='.db',
initialdir=self.initialdir, defaultextension='.db',
title="execute Script + output in a log file",
filetypes=[("default", "*.txt"), ("other", "*.log"),
("all", "*.*")])
if filename == "":
return
self.set_initialdir(filename)
with io.open(filename, 'w', encoding='utf-8') as f:
if "你好 мир Artisou à croute" not in script:
f.write("/*utf-8 tag : 你好 мир Artisou à croute*/\n")
Expand Down Expand Up @@ -593,6 +605,109 @@ def bip(c):

self.conn.conn.isolation_level = isolation # restore standard

def import_csvtb(self):
"""import csv dialog (with guessing of encoding and separator)"""
csv_file = filedialog.askopenfilename(
initialdir=self.initialdir, defaultextension='.db',
title="Choose a csv fileto import ",
filetypes=[("default", "*.csv"), ("other", "*.txt"),
("all", "*.*")])
if csv_file != '':
self.set_initialdir(csv_file)
# guess encoding
encodings = guess_encoding(csv_file)
# guess Header and delimiter
with io.open(csv_file, encoding=encodings[0]) as f:
preview = f.read(9999)
has_header = True
default_sep = ","
default_quote = '"'
try:
dialect = csv.Sniffer().sniff(preview)
has_header = csv.Sniffer().has_header(preview)
default_sep = dialect.delimiter
default_quote = Dialect.quotechar
except:
pass # sniffer can fail
default_decim = [".", ","] if default_sep != ";" else [",", "."]

# Request form : List of Horizontal Frame names 'FramLabel'
# or fields : 'Label', 'InitialValue',['r' or 'w', Width, Height]
table_name = csv_file.replace("\\", "/").split("/")[-1].split(".")[0]
dlines = "\n\n".join(preview.splitlines()[:3])
guess_sql = guess_sql_creation(table_name, default_sep,
default_decim, has_header, dlines, default_quote)[2]
fields_in = ['', ['csv Name', csv_file, 'r', 100], '',
['table Name', table_name],
['column separator', default_sep, 'w', 20],
['string delimiter', default_quote, 'w', 20],
'', ['Decimal separator', default_decim],
['Encoding', encodings],
'Fliflaps', ['Header line', has_header],
['Create table', True],
['Replace existing data', True], '',
['first 3 lines', dlines, 'r', 100, 10], '',
['use manual creation request', False], '',
['creation request', guess_sql, 'w', 100, 10]]
actions = ([self.conn, self.actualize_db])
create_dialog(("Importing %s" % csv_file), fields_in,
("Import", import_csvtb_ok), actions)

def export_csvtb(self):
"""get selected table definition and launch cvs export dialog"""
# determine selected table
actions = [self.conn, self.db_tree]
selitem = self.db_tree.focus() # get tree item having the focus
if selitem != '':
seltag = self.db_tree.item(selitem, "tag")[0]
if seltag == "run_up": # if 'run-up', do as if dbl-click 1 level up
selitem = self.db_tree.parent(selitem)
# get final information
definition, query = self.db_tree.item(selitem, "values")
if query != "": # run the export_csv dialog
title = ('Export Table "%s" to ?' %
self.db_tree.item(selitem, "text"))
self.export_csv_dialog(query, title, actions)

def export_csvqr(self):
"""get tab selected definition and launch cvs export dialog"""
actions = [self.conn, self.n]
active_tab_id = self.n.notebook.select()
if active_tab_id != '': # get current selection (or all)
fw = self.n.fw_labels[active_tab_id]
try:
query = fw.get('sel.first', 'sel.last')
except:
query = fw.get(1.0, END)[:-1]
if query != "":
self.export_csv_dialog(query, "Export Query", actions)

def export_csv_dialog(self, query="--", text="undefined.csv", actions=[]):
"""export csv dialog"""
# proposed encoding (we favorize utf-8 or utf-8-sig)
encodings = ["utf-8", locale.getdefaultlocale()[1], "utf-16",
"utf-8-sig"]
if os.name == 'nt':
encodings = ["utf-8-sig", locale.getdefaultlocale()[1], "utf-16",
"utf-8"]
# proposed csv separator
default_sep = [",", "|", ";"]
csv_file = filedialog.asksaveasfilename(
initialdir=self.initialdir, defaultextension='.db', title=text,
filetypes=[("default", "*.csv"), ("other", "*.txt"),
("all", "*.*")])
if csv_file != "":
# Request form (http://www.python-course.eu/tkinter_entry_widgets.php)
fields = ['', ['csv Name', csv_file, 'r', 100], '',
['column separator', default_sep],
['Header line', True],
['Encoding', encodings], '',
["Data to export (MUST be 1 Request)",
(query), 'w', 100, 10]]

create_dialog(("Export to %s" % csv_file), fields,
("Export", export_csv_ok), actions)


class NotebookForQueries():
"""Create a Notebook with a list in the First frame
Expand Down Expand Up @@ -777,52 +892,6 @@ def guess_sql_creation(table_name, separ, decim, header, data, quoter='"'):
return sql_crea, typ, head


def import_csvtb(actions):
"""import csv dialog (with guessing of encoding and separator)"""
csv_file = filedialog.askopenfilename(
defaultextension='.db',
title="Choose a csv fileto import ",
filetypes=[("default", "*.csv"), ("other", "*.txt"), ("all", "*.*")])
# guess encoding
encodings = guess_encoding(csv_file)
# guess Header and delimiter
with io.open(csv_file, encoding=encodings[0]) as f:
preview = f.read(9999)
has_header = True
default_sep = ","
default_quote = '"'
try:
dialect = csv.Sniffer().sniff(preview)
has_header = csv.Sniffer().has_header(preview)
default_sep = dialect.delimiter
default_quote = Dialect.quotechar
except:
pass # sniffer can fail
default_decim = [".", ","] if default_sep != ";" else [",", "."]

# Request form : List of Horizontal Frame names 'FramLabel'
# or fields : 'Label', 'InitialValue',['r' or 'w', Width, Height]
table_name = (csv_file.replace("\\", "/")).split("/")[-1].split(".")[0]
dlines = "\n\n".join(preview.splitlines()[:3])
guess_sql = guess_sql_creation(table_name, default_sep, default_decim,
has_header, dlines, default_quote)[2]
fields_in = ['', ['csv Name', csv_file, 'r', 100], '',
['table Name', table_name],
['column separator', default_sep, 'w', 20],
['string delimiter', default_quote, 'w', 20],
'', ['Decimal separator', default_decim],
['Encoding', encodings],
'Fliflaps', ['Header line', has_header],
['Create table', True],
['Replace existing data', True], '',
['first 3 lines', dlines, 'r', 100, 10], '',
['use manual creation request', False], '',
['creation request', guess_sql, 'w', 100, 10]]

create_dialog(("Importing %s" % csv_file), fields_in,
("Import", import_csvtb_ok), actions)


def guess_encoding(csv_file):
"""guess the encoding of the given file"""
with io.open(csv_file, "rb") as f:
Expand All @@ -840,32 +909,6 @@ def guess_encoding(csv_file):
return [locale.getdefaultlocale()[1], "utf-8"]


def export_csv_dialog(query="select 42", text="undefined.csv", actions=[]):
"""export csv dialog"""
# proposed encoding (we favorize utf-8 or utf-8-sig)
encodings = ["utf-8", locale.getdefaultlocale()[1], "utf-16", "utf-8-sig"]
if os.name == 'nt':
encodings = ["utf-8-sig", locale.getdefaultlocale()[1], "utf-16",
"utf-8"]
# proposed csv separator
default_sep = [",", "|", ";"]

csv_file = filedialog.asksaveasfilename(
defaultextension='.db', title=text,
filetypes=[("default", "*.csv"), ("other", "*.txt"), ("all", "*.*")])
if csv_file != "":
# Request form (http://www.python-course.eu/tkinter_entry_widgets.php)
fields = ['', ['csv Name', csv_file, 'r', 100], '',
['column separator', default_sep],
['Header line', True],
['Encoding', encodings], '',
["Data to export (MUST be 1 Request)", (query), 'w', 100, 10]
]

create_dialog(("Export to %s" % csv_file), fields,
("Export", export_csv_ok), actions)


def create_dialog(title, fields_in, buttons, actions):
"""create a formular with title, fields, button, data"""
# drawing the request form
Expand Down Expand Up @@ -1044,36 +1087,6 @@ def export_csv_ok(thetop, entries, actions):
fout.close


def export_csvtb(actions):
"""get selected table definition and launch cvs export dialog"""
# determine selected table
db_tree = actions[1]
selitem = db_tree.focus() # get tree item having the focus
if selitem != '':
seltag = db_tree.item(selitem, "tag")[0]
if seltag == "run_up": # if 'run-up', do as if dbl-click 1 level up
selitem = db_tree.parent(selitem)
# get final information
definition, query = db_tree.item(selitem, "values")
if query != "": # run the export_csv dialog
title = ('Export Table "%s" to ?' % db_tree.item(selitem, "text"))
export_csv_dialog(query, title, actions)


def export_csvqr(actions):
"""get tab selected definition and launch cvs export dialog"""
n = actions[1]
active_tab_id = n.notebook.select()
if active_tab_id != '': # get current selection (or all)
fw = n.fw_labels[active_tab_id]
try:
query = fw.get('sel.first', 'sel.last')
except:
query = fw.get(1.0, END)[:-1]
if query != "":
export_csv_dialog(query, "Export Query", actions)


def get_leaves(conn, category, attached_db="", tbl=""):
"""returns a list of 'category' objects in attached_db
[objectCode, objectLabel, Definition, 'sub-level']
Expand Down

0 comments on commit da3db1d

Please sign in to comment.