comparison agl.py @ 14:db4951d2d303

Add option to graph N days ago easily.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 11 Sep 2017 22:09:37 +0930
parents 988e511a5f29
children 7fd3dc0516f8
comparison
equal deleted inserted replaced
13:988e511a5f29 14:db4951d2d303
26 def main(): 26 def main():
27 parser = argparse.ArgumentParser() 27 parser = argparse.ArgumentParser()
28 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") 28 parser.add_argument('-u', '--update', help = 'Update data', action="store_true")
29 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") 29 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true")
30 parser.add_argument('-f', '--filename', help = 'Filename to save graph as (uses strftime on start)', type = str) 30 parser.add_argument('-f', '--filename', help = 'Filename to save graph as (uses strftime on start)', type = str)
31 parser.add_argument('-d', '--days', help = 'Days ago to graph', type = int)
31 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) 32 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date)
32 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) 33 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date)
33 34
34 args = parser.parse_args() 35 args = parser.parse_args()
35 36
38 conf.read(confname) 39 conf.read(confname)
39 username = conf.get('DEFAULT', 'username') 40 username = conf.get('DEFAULT', 'username')
40 password = conf.get('DEFAULT', 'password') 41 password = conf.get('DEFAULT', 'password')
41 dbfn = conf.get('DEFAULT', 'db') 42 dbfn = conf.get('DEFAULT', 'db')
42 43
43 if (args.start is None) ^ (args.end is None):
44 parser.error('Must specify start and end or neither')
45
46 if not args.update and not args.graph: 44 if not args.update and not args.graph:
47 parser.error('Nothing to do') 45 parser.error('Nothing to do')
48 46
49 start = args.start 47 start = args.start
50 if start is None: 48 if start is None:
51 start = datetime.date.today() 49 start = datetime.date.today()
52 start = datetime.datetime(start.year, start.month, start.day) 50 start = datetime.datetime(start.year, start.month, start.day)
53 51
52 if args.days is not None:
53 start -= datetime.timedelta(days = args.days)
54
54 end = args.end 55 end = args.end
55 if end is None: 56 if end is None:
56 end = start + datetime.timedelta(days = 1) 57 end = start + datetime.timedelta(days = 1)
57 end = datetime.datetime(end.year, end.month, end.day) 58 end = datetime.datetime(end.year, end.month, end.day)
59
58 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES) 60 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES)
59 cur = dbh.cursor() 61 cur = dbh.cursor()
60 if args.update: 62 if args.update:
61 date = start 63 date = start
62 while date < end: 64 while date < end: