-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfargen-time-add.py
executable file
·105 lines (90 loc) · 3.06 KB
/
fargen-time-add.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
import argparse, time
from datetime import datetime, timedelta
from dateutil.parser import parse
mysql_date_format_str="%Y-%m-%d %H:%M"
now = time.strftime(mysql_date_format_str)
parser = argparse.ArgumentParser(description='Process time instance.')
parser.add_argument('-s', '--start',
help='specify instance start datetime')
parser.add_argument('-e', '--end',
help='specify instance end datetime')
parser.add_argument('-i', '--instantaneous',
dest='instantaneous',
action='store_true',
default=False,
help='specify instance as instantaneous')
parser.add_argument('-m', '--message',
help='specify instance message')
args = parser.parse_args()
if args.start:
strp_start = parse(args.start).replace(second=0, microsecond=0)
args.start = strp_start.strftime(mysql_date_format_str)
#print "s: ",args.start
#print "s2:",strp_start
if args.end:
strp_end = parse(args.end).replace(second=0, microsecond=0)
args.end = strp_end.strftime(mysql_date_format_str)
#print "e: ",args.end
#print "e2:",strp_end
#if args.message:
#print "m: ",args.message
#if args.instantaneous:
#print "i: ",args.instantaneous
if args.start and args.end:
#print "[INFO] START and END are specified"
if strp_start < strp_end:
print "[INFO] Duration:", str(strp_end - strp_start)
if args.instantaneous:
print "[ERR!] Option INSTANTANEOUS included"
exit(1)
#else:
#print "[INFO] Option INSTANTANEOUS included"
elif strp_start == strp_end:
#print "[INFO] Instance has no duration"
if not args.instantaneous:
print "[ERR!] Option INSTANTANEOUS not included"
exit(1)
#else:
#print "[INFO] Option INSTANTANEOUS included"
elif strp_start > strp_end:
print "[ERR!] START is later than END"
exit(1)
elif args.start and not args.end:
#print "[INFO] No END specified"
if args.instantaneous:
print "[INFO] Setting END to START by option INSTANTANEOUS"
args.end = args.start
else:
print "[INFO] No option INSTANTANEOUS supplied; assuming incomplete"
elif args.end and not args.start:
#print "[INFO] No START specified"
if args.instantaneous:
print "[INFO] Setting START to END by option INSTANTANEOUS"
args.start = args.end
#else:
print "[INFO] No option INSTANTANEOUS supplied; assuming incomplete"
elif not args.start and not args.end:
print "[INFO] START and END not set"
if args.instantaneous:
print "[INFO] Option INSTANTANEOUS supplied; assuming current time"
args.start = now
args.end = now
else:
print "[ERR!] Option INSTANTANEOUS not supplied"
exit(1)
# DATABASE STUFF
import sqlite3
conn = sqlite3.connect('/home/chrisfargen/.fargen/time.sqlite')
c = conn.cursor()
# Create table
c.execute("CREATE TABLE IF NOT EXISTS instance (start text, end text, message text)")
t = (args.start, args.end, args.message,)
# Insert a row of data
c.execute("INSERT INTO instance VALUES (?,?,?)", t)
if c.rowcount:
print "[INFO] TIME:",c.lastrowid,"added.",c.rowcount,"row(s) affected."
# Save (commit) the changes
conn.commit()
conn.close()
# source: http://docs.python.org/2/library/sqlite3.html