Skip to content

Commit 4dc9849

Browse files
committed
.excel and .once+output -x options
1 parent 72be710 commit 4dc9849

1 file changed

Lines changed: 46 additions & 28 deletions

File tree

sqlite_bro/sqlite_bro.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import io
1717
import shlex # Simple lexical analysis
1818
from os.path import expanduser
19+
import tempfile as tmpf
20+
import subprocess
1921

2022
try: # We are Python 3.3+
2123
from tkinter import *
@@ -33,7 +35,6 @@
3335
tipwindow = None
3436

3537

36-
3738
class App:
3839
"""the GUI graphic application"""
3940

@@ -45,17 +46,16 @@ def __init__(self, use_gui=True):
4546
self.database_file = ""
4647
self.initialdir = "."
4748

48-
4949
# Do we use a GUI ?
5050
self.use_gui = use_gui # gui ok by default
5151
try:
5252
self.tk_win = Tk()
5353
except:
5454
self.use_gui = False
55-
55+
5656
if self.use_gui:
5757
self.tk_win.title(
58-
"A graphic SQLite Client in 1 Python file (" + self.__version__ + ")"
58+
"A graphic SQLite Client in 1 Python file (" + self.__version__ + ")"
5959
)
6060
self.tk_win.option_add("*tearOff", FALSE) # hint of tk documentation
6161
self.tk_win.minsize(600, 200) # minimal size
@@ -71,7 +71,6 @@ def __init__(self, use_gui=True):
7171
# Initiate Drag State
7272
self.state_drag = False
7373
self.state_drag_index = 0
74-
7574

7675
# With a Panedwindow of two frames: 'Database' and 'Queries'
7776
p = ttk.Panedwindow(self.tk_win, orient=HORIZONTAL)
@@ -90,16 +89,14 @@ def __init__(self, use_gui=True):
9089
self.db_tree.pack(fill=BOTH, expand=1)
9190

9291
# create a notebook 'n' inside the right 'Queries' Frame
93-
self.n = NotebookForQueries(self.tk_win, f_queries, [],
94-
self.use_gui)
92+
self.n = NotebookForQueries(self.tk_win, f_queries, [], self.use_gui)
9593

9694
# Bind keyboard shortcuts
9795
self.tk_win.bind("<F9>", self.run_tab)
9896
else:
99-
# create a GUI-Less notebook 'n'
97+
# create a GUI-Less notebook 'n'
10098
self.n = NotebookForQueries(None, None, [], self.use_gui)
10199

102-
103100
# define default home directory
104101
self.home = expanduser("~")
105102

@@ -114,6 +111,7 @@ def __init__(self, use_gui=True):
114111
self.output_file = None
115112
self.init_output = True
116113
self.output_mode = False
114+
self.x_mode = False
117115

118116
def create_menu(self):
119117
"""create the menu of the application"""
@@ -249,10 +247,12 @@ def load_script(self):
249247
with io.open(filename, encoding=guess_encoding(filename)[0]) as f:
250248
script = f.read()
251249
sqls = self.conn.get_sqlsplit(script, remove_comments=True)
252-
dg = [s for s in sqls if s.strip(" \t\n\r")[:5].lower()
253-
in ("pydef", ".read" , ".shel") or
254-
s.strip(" \t\n\r")[:1].lower() =="."
255-
]
250+
dg = [
251+
s
252+
for s in sqls
253+
if s.strip(" \t\n\r")[:5].lower() in ("pydef", ".read", ".shel")
254+
or s.strip(" \t\n\r")[:1].lower() == "."
255+
]
256256
if dg:
257257
fields = [
258258
"",
@@ -342,7 +342,7 @@ def actualize_db(self):
342342
"""refresh the database view"""
343343
if not self.use_gui:
344344
return
345-
345+
346346
# bind double-click for easy user interaction
347347
self.db_tree.tag_bind("run", "<Double-1>", self.t_doubleClicked)
348348
self.db_tree.tag_bind("run_up", "<Double-1>", self.t_doubleClicked)
@@ -857,8 +857,8 @@ def bip(c):
857857
try:
858858
if shell_list[0] == ".cd" and len(shell_list) >= 2:
859859
db_file = shell_list[1]
860-
self.db_file = self.db_file.strip("'")
861-
self.db_file = self.db_file.strip('"')
860+
db_file = db_file.strip("'")
861+
db_file = db_file.strip('"')
862862
if (db_file + "z")[0] == "~":
863863
self.current_directory = os.path.join(
864864
self.home, db_file[1:]
@@ -882,11 +882,14 @@ def bip(c):
882882
self.default_header = True
883883
if shell_list[0] == ".separator" and len(shell_list) >= 2:
884884
self.default_separator = shell_list[1]
885-
if shell_list[0] in (".once", ".output"):
885+
if shell_list[0] in (".once", ".output", ".excel"):
886+
if shell_list[0] == ".excel":
887+
shell_list= [".once" , "--bom" , "-x"]
886888
if shell_list[0] == ".once":
887889
self.once_mode, self.init_output = True, True
888890
else:
889891
self.output_mode, self.init_output = True, True
892+
self.x_mode = False
890893
self.encode_in = "utf-8"
891894
if "--bom" in shell_list: # keep access to the option
892895
self.encode_in = "utf-8-sig"
@@ -896,6 +899,11 @@ def bip(c):
896899
self.output_file = shell_list[1]
897900
self.output_file = self.output_file.strip("'")
898901
self.output_file = self.output_file.strip('"')
902+
if (self.output_file + "") == "-x":
903+
self.x_mode = True
904+
ff = tmpf.NamedTemporaryFile(delete=False, suffix=".csv")
905+
self.output_file = ff.name
906+
ff.close
899907
if (self.output_file + "z")[0] == "~":
900908
self.output_file = os.path.join(
901909
self.home, self.output_file[1:]
@@ -910,12 +918,12 @@ def bip(c):
910918
with io.open(
911919
self.output_file, write_mode, encoding=self.encode_in
912920
) as fout:
913-
fout.writelines(instru[len(".print") + 1:] + "\n")
921+
fout.writelines(instru[len(".print") + 1 :] + "\n")
914922
self.init_output, self.once_mode = False, False
915923
if shell_list[0] == ".import" and len(shell_list) >= 2:
916924
csv_file = shell_list[1]
917-
self.csv_file = self.csv_file.strip("'")
918-
self.csv_file = self.csv_file.strip('"')
925+
csv_file = csv_file.strip("'")
926+
csv_file = csv_file.strip('"')
919927
if (csv_file + "z")[0] == "~":
920928
csv_file = os.path.join(self.home, csv_file[1:])
921929
guess = guess_csv(csv_file)
@@ -960,8 +968,8 @@ def bip(c):
960968
if shell_list[0] == ".dump":
961969
if len(shell_list) >= 2:
962970
csv_file = shell_list[1]
963-
self.csv_file = self.csv_file.strip("'")
964-
self.csv_file = self.csv_file.strip('"')
971+
csv_file = csv_file.strip("'")
972+
csv_file = csv_file.strip('"')
965973
if (csv_file + "z")[0] == "~":
966974
csv_file = os.path.join(self.home, csv_file[1:])
967975
with io.open(csv_file, "w", encoding="utf-8") as f:
@@ -977,8 +985,8 @@ def bip(c):
977985
)
978986
if shell_list[0] == ".read" and len(shell_list) >= 2:
979987
filename = shell_list[1]
980-
self.filename = self.filename.strip("'")
981-
self.filename = self.filename.strip('"')
988+
filename = filename.strip("'")
989+
filename = filename.strip('"')
982990
if (filename + "z")[0] == "~":
983991
filename = os.path.join(self.home, filename[1:])
984992
with io.open(
@@ -1016,15 +1024,15 @@ def bip(c):
10161024
self.conn.conn.backup(db_to)
10171025
db_to.close()
10181026
if shell_list[0] == ".shell" and len(shell_list) >= 2:
1019-
os.system(instru[len(".print") + 1:] + "\n")
1027+
os.system(instru[len(".print") + 1 :] + "\n")
10201028

10211029
except IOError as err:
10221030
msg = "I/O error: {0}".format(err)
10231031
self.n.add_treeview(
10241032
tab_tk_id, ("Error !",), [(msg,)], "Error !", instru
10251033
)
10261034
if not self.use_gui:
1027-
print( "Error !",[msg])
1035+
print("Error !", [msg])
10281036
if log is not None: # write to logFile
10291037
log.write("Error ! %s : %s" % (msg, instru))
10301038
sql_error = True
@@ -1041,6 +1049,14 @@ def bip(c):
10411049
initialize=self.init_output,
10421050
)
10431051
self.once_mode, self.init_output = False, False
1052+
if self.x_mode:
1053+
os.system(
1054+
"start excel.exe "
1055+
+ self.output_file.replace("\\", "/")
1056+
)
1057+
ff = tmpf.NamedTemporaryFile(delete=False, suffix=".csv")
1058+
self.output_file = ff.name
1059+
ff.close
10441060
self.n.add_treeview(
10451061
tab_tk_id,
10461062
("qry", "file"),
@@ -1216,7 +1232,9 @@ class NotebookForQueries:
12161232

12171233
def __init__(self, tk_win, root, queries, use_gui):
12181234
self.use_gui = use_gui
1219-
self.nongui_tabs =["",]
1235+
self.nongui_tabs = [
1236+
"",
1237+
]
12201238
if self.use_gui:
12211239
self.tk_win = tk_win
12221240
self.root = root
@@ -1237,7 +1255,7 @@ def new_query_tab(self, title, query):
12371255
if not self.use_gui:
12381256
self.nongui_tabs += [query]
12391257
return len(self.nongui_tabs) - 1
1240-
1258+
12411259
fw_welcome = ttk.Panedwindow(self.tk_win, orient=VERTICAL) # tk_win
12421260
fw_welcome.pack(fill="both", expand=True)
12431261
self.notebook.add(fw_welcome, text=(title))

0 commit comments

Comments
 (0)