-
Notifications
You must be signed in to change notification settings - Fork 3
/
export-sql.py
executable file
·170 lines (130 loc) · 5.21 KB
/
export-sql.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/python
###############################################################################
#
# Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# http://www.opensource.org/licenses/mit-license.php
#
###############################################################################
"""
export-sql.py -- Export the taskwarrior database as a series of SQL commands.
Example usage::
$ ./export-sql.py | sqlite3 mytasks.db
$ /usr/bin/sqlite3 mytasks.db "select * from annotations;"
This script has only been tested with sqlite3, but in theory, it could be
easily modified to supported mysql, postgres or whatever you choose.
Author: Ralph Bean
"""
import sys
import commands
import json
from datetime import datetime
# Note that you may want to modify the field sizes to suit your usage.
table_definitions = """
CREATE TABLE tasks (
uuid VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
entry DATETIME NOT NULL,
end DATETIME,
priority VARCHAR(32),
project VARCHAR(32),
status VARCHAR(32),
PRIMARY KEY (uuid)
);
CREATE TABLE annotations (
uuid VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
entry DATETIME NOT NULL,
FOREIGN KEY(uuid) REFERENCES tasks(uuid)
);
"""
replacements = {
'"': '&dquot;',
"'": '"',
'[': '&open;',
']': '&close;',
'/': '\\/',
}
def escape(s):
""" Escape a string in the taskwarrior style """
for unsafe, safe in replacements.iteritems():
s = s.replace(unsafe, safe)
return s
# A lookup table for how to convert various values by type to SQL
conversion_lookup = {
# Tack on an extra set of quotes
unicode: lambda v: "'%s'" % escape(v),
# Do the same as for unicode
str: lambda v: convert(unicode(v)),
# Convert to ISO format and do the same as for unicode
datetime: lambda v: convert(v.isoformat(' ')),
# Replace python None with SQL NULL
type(None): lambda v: 'NULL',
}
# Compose a value with its corresponding function in conversion_lookup
convert = lambda v: conversion_lookup.get(type(v), lambda v: v)(v)
def parse_datetime(task):
""" Parse the datetime strings given to us by `task export` """
for key in ['entry', 'end']:
if key in task:
task[key] = datetime.strptime(task[key], "%Y%m%dT%H%M%SZ")
return task
def to_sql(task):
""" Create a list of SQL INSERT statements out of a task python dict """
def make_annotation(annot):
""" Create a list of SQL INSERT statements for an annotation """
annot['uuid'] = task['uuid']
template = "{uuid}, {description}, {entry}"
annot = dict(zip(annot.keys(), map(convert, annot.values())))
values = template.format(**annot)
return "INSERT INTO \"annotations\" VALUES(%s)" % values
template = "{uuid}, {description}, {entry}, {end}, " + \
"{priority}, {project}, {status}"
nullables = ['end', 'priority', 'project', 'status']
defaults = dict([(key, None) for key in nullables])
defaults['annotations'] = []
defaults.update(task)
defaults = dict(zip(defaults.keys(), map(convert, defaults.values())))
values = template.format(**defaults)
annotations = map(make_annotation, defaults['annotations'])
return ["INSERT INTO \"tasks\" VALUES(%s)" % values] + annotations
def main():
""" Return a list of SQL statements. """
# Use the taskwarrior 2.0+ export command to filter and return JSON
command = "task rc.verbose=nothing rc.json.array=yes " + " ".join(sys.argv[1:]) + " export"
# Load each task from json to a python dict
tasks = json.loads(commands.getoutput(command))
# Mangle datetime strings into python datetime objects
tasks = map(parse_datetime, tasks)
# Produce formatted SQL statements for each task
inserts = sum(map(to_sql, tasks), [])
return inserts
if __name__ == '__main__':
# Get the INSERT statements
lines = main()
# Combine them with semicolons
sql = table_definitions + ";\n".join(lines) + ';'
# Print them out, decorated with sqlite3 trappings
print """
BEGIN TRANSACTION;
{sql}
COMMIT;""".format(sql=sql)
###############################################################################