Mercurial > hg > sqlex
annotate sqlex/main.py @ 4:b440206930ac
stub table optional positional argument
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 01 Apr 2017 09:28:49 -0700 |
parents | 5f1e1ac96aa7 |
children | 3a7f515571dc |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 sql(ite) explorer/exporter | |
6 """ | |
7 | |
8 # imports | |
9 import argparse | |
10 import os | |
11 import sys | |
3 | 12 from .model import SQLEx |
13 | |
0 | 14 |
15 def ensure_dir(directory): | |
16 """ensure a directory exists""" | |
17 if os.path.exists(directory): | |
18 if not os.path.isdir(directory): | |
19 raise OSError("Not a directory: '{}'".format(directory)) | |
20 return directory | |
21 os.makedirs(directory) | |
22 return directory | |
23 | |
24 | |
25 class SQLExParser(argparse.ArgumentParser): | |
26 """CLI option parser""" | |
27 | |
28 def __init__(self, **kwargs): | |
29 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) | |
30 kwargs.setdefault('description', __doc__) | |
31 argparse.ArgumentParser.__init__(self, **kwargs) | |
1 | 32 self.add_argument('db', |
33 help="sqlite `.db` file") | |
4
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
34 self.add_argument('table', nargs='?', |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
35 help="table to operate on") |
3 | 36 self.add_argument('--tables', '--list-tables', dest='list_tables', |
37 action='store_true', default=False, | |
38 help="list tables and exit") | |
0 | 39 self.options = None |
40 | |
41 def parse_args(self, *args, **kw): | |
42 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
43 self.validate(options) | |
44 self.options = options | |
45 return options | |
46 | |
47 def validate(self, options): | |
48 """validate options""" | |
49 | |
1 | 50 try: |
3 | 51 open(options.db).close() |
1 | 52 except Exception as e: |
53 self.error("Could not open '{}': {}".format(options.db, e)) | |
54 | |
2 | 55 |
0 | 56 def main(args=sys.argv[1:]): |
57 """CLI""" | |
58 | |
59 # parse command line options | |
60 parser = SQLExParser() | |
61 options = parser.parse_args(args) | |
62 | |
3 | 63 # connect to database |
64 db = SQLEx(options.db) | |
65 | |
66 if options.list_tables: | |
4
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
67 # list tables and return |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
68 # if `table` argument is provided, exit 1 |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
69 # if not available. Otherwise exit 0 |
3 | 70 tables = db.tables() |
71 print ('\n'.join(tables)) | |
4
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
72 retval = 0 |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
73 if options.table: |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
74 retval = int(options.table not in tables) |
b440206930ac
stub table optional positional argument
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
75 return retval |
3 | 76 |
0 | 77 if __name__ == '__main__': |
78 main() |