Skip to content

Commit a0c4f7c

Browse files
committed
survive DISPLAY-less situation
1 parent 53bb6e1 commit a0c4f7c

1 file changed

Lines changed: 95 additions & 52 deletions

File tree

sqlite_bro/sqlite_bro.py

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,72 @@
3333
tipwindow = None
3434

3535

36+
3637
class App:
3738
"""the GUI graphic application"""
3839

39-
def __init__(self):
40+
def __init__(self, use_gui=True):
4041
"""create a tkk graphic interface with a main window tk_win"""
4142
self.__version__ = "0.11.1"
4243
self._title = "of 2021-08-09b : 'Script me more !'"
4344
self.conn = None # Baresql database object
4445
self.database_file = ""
45-
self.tk_win = Tk()
46-
self.tk_win.title(
47-
"A graphic SQLite Client in 1 Python file (" + self.__version__ + ")"
48-
)
49-
self.tk_win.option_add("*tearOff", FALSE) # hint of tk documentation
50-
self.tk_win.minsize(600, 200) # minimal size
51-
52-
self.font_size = 10
53-
self.font_wheight = 0
5446
self.initialdir = "."
55-
# With a Menubar and Toolbar
56-
self.create_menu()
57-
self.create_toolbar()
5847

59-
# Create style "ButtonNotebook"
60-
self.create_style()
61-
# Initiate Drag State
62-
self.state_drag = False
63-
self.state_drag_index = 0
6448

65-
# With a Panedwindow of two frames: 'Database' and 'Queries'
66-
p = ttk.Panedwindow(self.tk_win, orient=HORIZONTAL)
67-
p.pack(fill=BOTH, expand=1)
49+
# Do we use a GUI ?
50+
self.use_gui = use_gui # gui ok by default
51+
try:
52+
self.tk_win = Tk()
53+
except:
54+
self.use_gui = False
55+
56+
if self.use_gui:
57+
self.tk_win.title(
58+
"A graphic SQLite Client in 1 Python file (" + self.__version__ + ")"
59+
)
60+
self.tk_win.option_add("*tearOff", FALSE) # hint of tk documentation
61+
self.tk_win.minsize(600, 200) # minimal size
6862

69-
f_database = ttk.Labelframe(p, text="Databases", width=200, height=100)
70-
p.add(f_database)
71-
f_queries = ttk.Labelframe(p, text="Queries", width=200, height=100)
72-
p.add(f_queries)
63+
self.font_size = 10
64+
self.font_wheight = 0
65+
# With a Menubar and Toolbar
66+
self.create_menu()
67+
self.create_toolbar()
68+
69+
# Create style "ButtonNotebook"
70+
self.create_style()
71+
# Initiate Drag State
72+
self.state_drag = False
73+
self.state_drag_index = 0
74+
75+
76+
# With a Panedwindow of two frames: 'Database' and 'Queries'
77+
p = ttk.Panedwindow(self.tk_win, orient=HORIZONTAL)
78+
p.pack(fill=BOTH, expand=1)
79+
80+
f_database = ttk.Labelframe(p, text="Databases", width=200, height=100)
81+
p.add(f_database)
82+
f_queries = ttk.Labelframe(p, text="Queries", width=200, height=100)
83+
p.add(f_queries)
84+
85+
# build tree view 't' inside the left 'Database' Frame
86+
self.db_tree = ttk.Treeview(
87+
f_database, displaycolumns=[], columns=("detail", "action")
88+
)
89+
self.db_tree.tag_configure("run")
90+
self.db_tree.pack(fill=BOTH, expand=1)
7391

74-
# build tree view 't' inside the left 'Database' Frame
75-
self.db_tree = ttk.Treeview(
76-
f_database, displaycolumns=[], columns=("detail", "action")
77-
)
78-
self.db_tree.tag_configure("run")
79-
self.db_tree.pack(fill=BOTH, expand=1)
92+
# create a notebook 'n' inside the right 'Queries' Frame
93+
self.n = NotebookForQueries(self.tk_win, f_queries, [],
94+
self.use_gui)
8095

81-
# create a notebook 'n' inside the right 'Queries' Frame
82-
self.n = NotebookForQueries(self.tk_win, f_queries, [])
96+
# Bind keyboard shortcuts
97+
self.tk_win.bind("<F9>", self.run_tab)
98+
else:
99+
# create a GUI-Less notebook 'n'
100+
self.n = NotebookForQueries(None, None, [], self.use_gui)
83101

84-
# Bind keyboard shortcuts
85-
self.tk_win.bind("<F9>", self.run_tab)
86102

87103
# define default home directory
88104
self.home = expanduser("~")
@@ -321,6 +337,9 @@ def close_db(self):
321337

322338
def actualize_db(self):
323339
"""refresh the database view"""
340+
if not self.use_gui:
341+
return
342+
324343
# bind double-click for easy user interaction
325344
self.db_tree.tag_bind("run", "<Double-1>", self.t_doubleClicked)
326345
self.db_tree.tag_bind("run_up", "<Double-1>", self.t_doubleClicked)
@@ -362,6 +381,12 @@ def quit_db(self):
362381

363382
def run_tab(self, event=None):
364383
"""clear previous results and run current script of a tab"""
384+
if not self.use_gui:
385+
active_tab_id = len(self.n.nongui_tabs) - 1
386+
script = self.n.nongui_tabs[active_tab_id]
387+
# print ("silently running,active_tab_id , script) # testtmp
388+
self.create_and_add_results(script, active_tab_id)
389+
return
365390
active_tab_id = self.n.notebook.select()
366391
if active_tab_id != "":
367392
# remove previous results
@@ -983,6 +1008,8 @@ def bip(c):
9831008
self.n.add_treeview(
9841009
tab_tk_id, ("Error !",), [(msg,)], "Error !", instru
9851010
)
1011+
if not self.use_gui:
1012+
print( "Error !",[msg])
9861013
if log is not None: # write to logFile
9871014
log.write("Error ! %s : %s" % (msg, instru))
9881015
sql_error = True
@@ -1172,23 +1199,30 @@ class NotebookForQueries:
11721199
"""Create a Notebook with a list in the First frame
11731200
and query results in following treeview frames"""
11741201

1175-
def __init__(self, tk_win, root, queries):
1176-
self.tk_win = tk_win
1177-
self.root = root
1178-
self.notebook = Notebook(root, style="ButtonNotebook") # ttk.
1202+
def __init__(self, tk_win, root, queries, use_gui):
1203+
self.use_gui = use_gui
1204+
self.nongui_tabs =["",]
1205+
if self.use_gui:
1206+
self.tk_win = tk_win
1207+
self.root = root
1208+
self.notebook = Notebook(root, style="ButtonNotebook") # ttk.
11791209

1180-
self.fw_labels = {} # tab_tk_id -> Scripting frame python object
1181-
self.fw_result_nbs = {} # tab_tk_id -> Notebook of Results
1210+
self.fw_labels = {} # tab_tk_id -> Scripting frame python object
1211+
self.fw_result_nbs = {} # tab_tk_id -> Notebook of Results
11821212

1183-
# resize rules
1184-
root.columnconfigure(0, weight=1)
1185-
root.rowconfigure(0, weight=1)
1186-
# grid widgets
1187-
self.notebook.grid(row=0, column=0, sticky=(N, W, S, E))
1213+
# resize rules
1214+
root.columnconfigure(0, weight=1)
1215+
root.rowconfigure(0, weight=1)
1216+
# grid widgets
1217+
self.notebook.grid(row=0, column=0, sticky=(N, W, S, E))
11881218

11891219
def new_query_tab(self, title, query):
11901220
"""add a Tab 'title' to the notebook, containing the Script 'query'"""
11911221

1222+
if not self.use_gui:
1223+
self.nongui_tabs += [query]
1224+
return len(self.nongui_tabs) - 1
1225+
11921226
fw_welcome = ttk.Panedwindow(self.tk_win, orient=VERTICAL) # tk_win
11931227
fw_welcome.pack(fill="both", expand=True)
11941228
self.notebook.add(fw_welcome, text=(title))
@@ -1244,6 +1278,8 @@ def remove_treeviews(self, given_tk_id):
12441278

12451279
def add_treeview(self, given_tk_id, columns, data, title="__", subt=""):
12461280
"""add a dataset result to the given tab tk_id"""
1281+
if not self.use_gui:
1282+
return
12471283
# ensure we work on lists
12481284
if isinstance(columns, (type("e"), type("e"))):
12491285
tree_columns = [columns]
@@ -2009,12 +2045,8 @@ def _main():
20092045
CREATE TABLE toto.new_item as select * from "main"."item";
20102046
.dump
20112047
"""
2012-
# print(args)
2013-
app = App()
2014-
# start with a memory Database and a welcome
2015-
app.new_db(":memory:")
20162048

2017-
if "argparse" in globals():
2049+
if "argparse" in globals(): # not before Python-3.2
20182050
parser = argparse.ArgumentParser(
20192051
description="sqlite_bro : a graphic SQLite browser in 1 Python file"
20202052
)
@@ -2038,6 +2070,13 @@ def _main():
20382070
"-sc", "--scripts", type=str, help="qive a list of initial scripts"
20392071
)
20402072
args = parser.parse_args()
2073+
2074+
if args.quiet:
2075+
app = App(use_gui=False)
2076+
else:
2077+
app = App(use_gui=True)
2078+
# start with a memory Database and a welcome
2079+
app.new_db(":memory:")
20412080
if args.database:
20422081
app.open_db(args.database)
20432082
if args.scripts:
@@ -2057,8 +2096,12 @@ def _main():
20572096
if args.quiet:
20582097
app.close_db
20592098
else:
2099+
app = App(use_gui=True)
2100+
# start with a memory Database and a welcome
2101+
app.new_db(":memory:")
20602102
app.n.new_query_tab("Welcome", welcome_text)
2061-
app.tk_win.mainloop()
2103+
if app.use_gui:
2104+
app.tk_win.mainloop()
20622105

20632106

20642107
if __name__ == "__main__":

0 commit comments

Comments
 (0)