annotate lemuriformes/table2csv.py @ 18:56596902e9ae default tip

add some setup + tests
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:57:03 -0800
parents 4793f99b73e0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 dump a MySQL table to CSV
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import csv
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 from .db import MySQLConnection, MySQLParser
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 class MySQLParser(argparse.ArgumentParser):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def __init__(self, **kwargs):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 argparse.ArgumentParser.__init__(self, **kwargs)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 self.add_arguments()
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 self.options = None
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def add_arguments(self):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 self.add_argument('host')
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 self.add_argument('db')
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 self.add_argument('-u', '--user', dest='user', default='root',
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 help="MySQL user [DEFAULT: %(default)s]")
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 self.add_argument('-p', '--password', dest='password',
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 help="MySQL password [DEFAULT: %(default)s]")
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def parse_args(self, args):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 options = argparse.ArgumentParser.parse_args(self, args)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 self.options = self.validate(options)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 return self.options
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 def validate(self, options):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 """validate options"""
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 return options
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 def connection(self):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 if self.options is None:
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 raise Exception("parse_args not called successfully!")
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 return MySQLConnection(host=self.options.host,
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 user=self.options.user,
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 password=self.options.password,
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 db=self.options.db)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 def main(args=sys.argv[1:]):
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 # parse command line
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 parser = MySQLParser(description=__doc__)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 parser.add_argument('table',
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 help="table to dump")
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 parser.add_argument('-o', '--output', dest='output',
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 type=argparse.FileType('w'), default=sys.stdout,
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 help="path to put data to, or stdout by default")
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 options = parser.parse_args(args)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 # read table
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 connection = parser.connection()
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 data = connection("SELECT * FROM {table}".format(table=options.table))
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 # dump table
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 writer = csv.writer(options.output)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 writer.writerows(data)
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 options.output.flush()
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 if __name__ == '__main__':
4793f99b73e0 [lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 main()