Skip to content

Commit 72be710

Browse files
committed
'.shell' option and better '~' management
1 parent cc6b19a commit 72be710

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

sqlite_bro/sqlite_bro.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,20 @@ def load_script(self):
249249
with io.open(filename, encoding=guess_encoding(filename)[0]) as f:
250250
script = f.read()
251251
sqls = self.conn.get_sqlsplit(script, remove_comments=True)
252-
dg = [s for s in sqls if s.strip(" \t\n\r")[:5] == "pydef"]
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+
]
253256
if dg:
254257
fields = [
255258
"",
256259
["In Script File:", filename, "r", 100],
257260
"",
258-
["Python Script", "".join(dg), "r", 80, 20],
261+
["non pure SQL code", "\n".join(dg), "r", 80, 20],
259262
]
260263

261264
create_dialog(
262-
("Ok for this Python Code ?"),
265+
("Ok for this non pure SQL code ?"),
263266
fields,
264267
("Confirm", self.load_script_ok),
265268
[text, script],
@@ -854,6 +857,8 @@ def bip(c):
854857
try:
855858
if shell_list[0] == ".cd" and len(shell_list) >= 2:
856859
db_file = shell_list[1]
860+
self.db_file = self.db_file.strip("'")
861+
self.db_file = self.db_file.strip('"')
857862
if (db_file + "z")[0] == "~":
858863
self.current_directory = os.path.join(
859864
self.home, db_file[1:]
@@ -889,6 +894,8 @@ def bip(c):
889894
self.output_file = shell_list[2]
890895
else:
891896
self.output_file = shell_list[1]
897+
self.output_file = self.output_file.strip("'")
898+
self.output_file = self.output_file.strip('"')
892899
if (self.output_file + "z")[0] == "~":
893900
self.output_file = os.path.join(
894901
self.home, self.output_file[1:]
@@ -907,6 +914,8 @@ def bip(c):
907914
self.init_output, self.once_mode = False, False
908915
if shell_list[0] == ".import" and len(shell_list) >= 2:
909916
csv_file = shell_list[1]
917+
self.csv_file = self.csv_file.strip("'")
918+
self.csv_file = self.csv_file.strip('"')
910919
if (csv_file + "z")[0] == "~":
911920
csv_file = os.path.join(self.home, csv_file[1:])
912921
guess = guess_csv(csv_file)
@@ -951,6 +960,8 @@ def bip(c):
951960
if shell_list[0] == ".dump":
952961
if len(shell_list) >= 2:
953962
csv_file = shell_list[1]
963+
self.csv_file = self.csv_file.strip("'")
964+
self.csv_file = self.csv_file.strip('"')
954965
if (csv_file + "z")[0] == "~":
955966
csv_file = os.path.join(self.home, csv_file[1:])
956967
with io.open(csv_file, "w", encoding="utf-8") as f:
@@ -966,6 +977,8 @@ def bip(c):
966977
)
967978
if shell_list[0] == ".read" and len(shell_list) >= 2:
968979
filename = shell_list[1]
980+
self.filename = self.filename.strip("'")
981+
self.filename = self.filename.strip('"')
969982
if (filename + "z")[0] == "~":
970983
filename = os.path.join(self.home, filename[1:])
971984
with io.open(
@@ -1002,6 +1015,8 @@ def bip(c):
10021015
with db_to:
10031016
self.conn.conn.backup(db_to)
10041017
db_to.close()
1018+
if shell_list[0] == ".shell" and len(shell_list) >= 2:
1019+
os.system(instru[len(".print") + 1:] + "\n")
10051020

10061021
except IOError as err:
10071022
msg = "I/O error: {0}".format(err)
@@ -2033,12 +2048,13 @@ def _main():
20332048
-- .read FILE Read input from FILE
20342049
-- .restore FILE Restore content of DB (default "main") from FILE
20352050
-- .separator COL Set column separator in next .once exports (default ,)
2051+
-- .shell CMD ARGS... Run CMD ARGS... in a system shell
20362052
20372053
.headers on
20382054
.separator ;
2039-
.once --bom '~this_file_of_result.txt'
2055+
.once --bom '~this_file_of result.txt'
20402056
select ItemNo, Description from item order by ItemNo desc;
2041-
.import '~this_file_of_result.txt' in_this_table
2057+
.import '~this_file_of result.txt' in_this_table
20422058
.cd ~
20432059
ATTACH 'test.db' as toto;
20442060
DROP TABLE IF EXISTS toto.new_item;

0 commit comments

Comments
 (0)