|
1 | 1 | #!/usr/bin/python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | from __future__ import print_function, unicode_literals, division # Python2.7 |
4 | | - |
| 4 | +import argparse |
5 | 5 |
|
6 | 6 | import sqlite3 as sqlite |
7 | 7 | import sys |
@@ -33,7 +33,7 @@ class App: |
33 | 33 | def __init__(self): |
34 | 34 | """create a tkk graphic interface with a main window tk_win""" |
35 | 35 | self.__version__ = '0.10.0' |
36 | | - self._title = "of 2021-08-01a : 'Hello, better .scripting World!'" |
| 36 | + self._title = "of 2021-08-01a : 'Hello, scripting World!'" |
37 | 37 | self.conn = None # Baresql database object |
38 | 38 | self.database_file = "" |
39 | 39 | self.tk_win = Tk() |
@@ -1598,10 +1598,33 @@ def export_writer(self, sql, csv_file, header=True, |
1598 | 1598 | writer.writerows(cursor.fetchall()) |
1599 | 1599 |
|
1600 | 1600 | def _main(): |
| 1601 | + parser = argparse.ArgumentParser(description='sqlite_bro : a graphic SQLite browser in 1 Python file') |
| 1602 | + parser.add_argument("-q", "--quiet", action='store_true', help="do not launch the gui") |
| 1603 | + parser.add_argument("-w", "--wait", action='store_true', help="wait the user to launch the scripts") |
| 1604 | + parser.add_argument("-db", "--database", default=":memory:", type=str, help="specify initial Database if not ':memory:'") |
| 1605 | + parser.add_argument("-sc", "--scripts", type=str, help="qive a list of initial scripts") |
| 1606 | + args = parser.parse_args() |
| 1607 | + print(args) |
1601 | 1608 | app = App() |
1602 | 1609 | # start with a memory Database and a welcome |
1603 | 1610 | app.new_db(":memory:") |
1604 | | - welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons) |
| 1611 | + |
| 1612 | + if args.database: |
| 1613 | + app.open_db(args.database) |
| 1614 | + if args.scripts: |
| 1615 | + if isinstance(args.scripts, str): |
| 1616 | + scripts = [args.scripts, '', ''] |
| 1617 | + else: |
| 1618 | + scripts = args.scripts |
| 1619 | + for script in scripts: |
| 1620 | + if os.path.isfile(script): |
| 1621 | + with io.open(script, encoding=guess_encoding(script)[0]) as f: |
| 1622 | + welcome_text = f.read() |
| 1623 | + app.n.new_query_tab("Welcome", welcome_text) |
| 1624 | + if args.wait == False: |
| 1625 | + app.run_tab() |
| 1626 | + else: |
| 1627 | + welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons) |
1605 | 1628 | \n-- to CREATE a table 'items' and a table 'parts' : |
1606 | 1629 | DROP TABLE IF EXISTS item; DROP TABLE IF EXISTS part; |
1607 | 1630 | CREATE TABLE item (ItemNo, Description,Kg , PRIMARY KEY (ItemNo)); |
@@ -1672,8 +1695,11 @@ def _main(): |
1672 | 1695 | DROP TABLE IF EXISTS toto.new_item; |
1673 | 1696 | CREATE TABLE toto.new_item as select * from "main"."item" |
1674 | 1697 | """ |
1675 | | - app.n.new_query_tab("Welcome", welcome_text) |
1676 | | - app.tk_win.mainloop() |
| 1698 | + app.n.new_query_tab("Welcome", welcome_text) |
| 1699 | + if args.quiet == True: |
| 1700 | + app.close_db |
| 1701 | + else: |
| 1702 | + app.tk_win.mainloop() |
1677 | 1703 |
|
1678 | 1704 | if __name__ == '__main__': |
1679 | 1705 | _main() # create a tkk graphic interface with a main window tk_win |
0 commit comments