comparison sqlex/main.py @ 3:5f1e1ac96aa7

stub works with SELECT
author Jeff Hammel <k0scist@gmail.com>
date Sat, 01 Apr 2017 09:19:22 -0700
parents 63a75d318b06
children b440206930ac
comparison
equal deleted inserted replaced
2:63a75d318b06 3:5f1e1ac96aa7
6 """ 6 """
7 7
8 # imports 8 # imports
9 import argparse 9 import argparse
10 import os 10 import os
11 import sqlite3
12 import sys 11 import sys
12 from .model import SQLEx
13
13 14
14 def ensure_dir(directory): 15 def ensure_dir(directory):
15 """ensure a directory exists""" 16 """ensure a directory exists"""
16 if os.path.exists(directory): 17 if os.path.exists(directory):
17 if not os.path.isdir(directory): 18 if not os.path.isdir(directory):
28 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) 29 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
29 kwargs.setdefault('description', __doc__) 30 kwargs.setdefault('description', __doc__)
30 argparse.ArgumentParser.__init__(self, **kwargs) 31 argparse.ArgumentParser.__init__(self, **kwargs)
31 self.add_argument('db', 32 self.add_argument('db',
32 help="sqlite `.db` file") 33 help="sqlite `.db` file")
33 self.add_argument( 34 self.add_argument('--tables', '--list-tables', dest='list_tables',
35 action='store_true', default=False,
36 help="list tables and exit")
34 self.options = None 37 self.options = None
35 38
36 def parse_args(self, *args, **kw): 39 def parse_args(self, *args, **kw):
37 options = argparse.ArgumentParser.parse_args(self, *args, **kw) 40 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
38 self.validate(options) 41 self.validate(options)
41 44
42 def validate(self, options): 45 def validate(self, options):
43 """validate options""" 46 """validate options"""
44 47
45 try: 48 try:
46 open(options.db) 49 open(options.db).close()
47 except Exception as e: 50 except Exception as e:
48 self.error("Could not open '{}': {}".format(options.db, e)) 51 self.error("Could not open '{}': {}".format(options.db, e))
49 52
50 53
51 def main(args=sys.argv[1:]): 54 def main(args=sys.argv[1:]):
53 56
54 # parse command line options 57 # parse command line options
55 parser = SQLExParser() 58 parser = SQLExParser()
56 options = parser.parse_args(args) 59 options = parser.parse_args(args)
57 60
61 # connect to database
62 db = SQLEx(options.db)
63
64 if options.list_tables:
65 tables = db.tables()
66 print ('\n'.join(tables))
67 return
68
58 if __name__ == '__main__': 69 if __name__ == '__main__':
59 main() 70 main()
60
61