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
|
|
12
|
|
13 def ensure_dir(directory):
|
|
14 """ensure a directory exists"""
|
|
15 if os.path.exists(directory):
|
|
16 if not os.path.isdir(directory):
|
|
17 raise OSError("Not a directory: '{}'".format(directory))
|
|
18 return directory
|
|
19 os.makedirs(directory)
|
|
20 return directory
|
|
21
|
|
22
|
|
23 class SQLExParser(argparse.ArgumentParser):
|
|
24 """CLI option parser"""
|
|
25
|
|
26 def __init__(self, **kwargs):
|
|
27 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
|
|
28 kwargs.setdefault('description', __doc__)
|
|
29 argparse.ArgumentParser.__init__(self, **kwargs)
|
|
30 self.add_argument('db', help="sqlite `.db` file")
|
|
31 self.options = None
|
|
32
|
|
33 def parse_args(self, *args, **kw):
|
|
34 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
|
|
35 self.validate(options)
|
|
36 self.options = options
|
|
37 return options
|
|
38
|
|
39 def validate(self, options):
|
|
40 """validate options"""
|
|
41
|
|
42 def main(args=sys.argv[1:]):
|
|
43 """CLI"""
|
|
44
|
|
45 # parse command line options
|
|
46 parser = SQLExParser()
|
|
47 options = parser.parse_args(args)
|
|
48
|
|
49 if __name__ == '__main__':
|
|
50 main()
|
|
51
|
|
52
|