Passed
Push — master ( 5fd921...68583f )
by Alexander
01:12
created

things3_to_kanban.get_rows()   A

Complexity

Conditions 1

Size

Total Lines 34
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 34
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""KanbanView for Things 3."""
5
6
from __future__ import print_function
7
8
__author__ = "Luc Beaulieu and Alexander Willner"
9
__copyright__ = "Copyright 2018 Luc Beaulieu / 2020 Alexander Willner"
10
__credits__ = ["Luc Beaulieu", "Alexander Willner"]
11
__license__ = "unknown"
12
__version__ = "1.1.0"
13
__maintainer__ = "Alexander Willner"
14
__email__ = "[email protected]"
15
__status__ = "Development"
16
17
import webbrowser
18
import codecs
19
from os.path import dirname, realpath
20
from os import environ
21
from random import shuffle
22
import things3
23
24
# Basic config
25
ANONYMIZE = bool(environ.get('ANONYMIZE'))
26
27
# Basic variables
28
FILE_HTML = dirname(realpath(__file__)) + '/kanban.html'
29
30
31
def anonymize(word):
32
    """Scramble output for screenshots."""
33
34
    if ANONYMIZE is True:
35
        word = list(word)
36
        shuffle(word)
37
        word = ''.join(word)
38
    return word
39
40
41
def write_html_column(cssclass, file, header, sql):
42
    """Create a column in the output."""
43
44
    rows = things3.get_rows(sql)
45
46
    file.write("<div class='column'><div class=''>" +
47
               "<h2 class='" + cssclass + "'>" + header +
48
               "<span class='size'>" + str(len(rows)) +
49
               "</span></h2>")
50
51
    for row in rows:
52
        task_uuid = str(row[0]) if row[0] is not None else ''
53
        task_title = anonymize(str(row[1])) if row[1] is not None else ''
54
        context_title = anonymize(str(row[2])) if row[2] is not None else ''
55
        context_uuid = str(row[3]) if row[3] is not None else ''
56
        deadline = str(row[4]) if row[4] is not None else ''
57
58
        task_link = '<a href="things:///show?id=' + task_uuid + '">' + \
59
            task_title + '</a>' if task_uuid != '' else task_title
60
        context_link = '<a href="things:///show?id=' + context_uuid + '">' + \
61
            context_title + '</a>' if context_uuid != '' else context_title
62
        css_class = 'hasProject' if context_title != '' else 'hasNoProject'
63
        css_class = 'hasDeadline' if deadline != '' else css_class
64
65
        file.write('<div class="box">' + task_link +
66
                   '<div class="deadline">' + deadline + '</div>' +
67
                   '<div class="area ' + css_class + '">' + context_link +
68
                   '</div>' +
69
                   '</div>')
70
    file.write("</div></div>")
71
72
73
def write_html_header(file):
74
    """Write HTML header."""
75
76
    message = """
77
        <!DOCTYPE html>
78
        <html>
79
        <head>
80
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
81
          <link rel="stylesheet" href="../resources/style.css">
82
          <title>KanbanView for Things 3</title>
83
        </head>
84
85
        <body>
86
          <header>
87
            <a href="#" onclick="refresh();" title="click to refresh">
88
              <img class="logo" src="../resources/logo.png" alt="logo">
89
            </a>
90
          </header>
91
          <article class='some-page-wrapper'>
92
            <div class='row'>
93
        """
94
    file.write(message)
95
96
97
def write_html_footer(file):
98
    """Write HTML footer."""
99
100
    message = """
101
            </div>
102
          </article>
103
        <footer class="footer"><br />
104
        Copyright &copy;2018 Luc Beaulieu / 2020 Alexander Willner
105
        </footer></body></html>"""
106
    file.write(message)
107
108
109
def write_html_columns(file):
110
    """Write HTML columns."""
111
112
    write_html_column("color1", file, "Backlog", things3.LIST_SOMEDAY)
113
    write_html_column("color5", file, "Upcoming", things3.LIST_UPCOMING)
114
    write_html_column("color3", file, "Waiting", things3.LIST_WAITING)
115
    write_html_column("color4", file, "Inbox", things3.LIST_INBOX)
116
    write_html_column("color2", file, "MIT", things3.LIST_MIT)
117
    write_html_column("color6", file, "Today", things3.LIST_TODAY)
118
    write_html_column("color7", file, "Next", things3.LIST_ANYTIME)
119
120
121
def main():
122
    """Convert Things 3 database to Kanban HTML view."""
123
124
    file = codecs.open(FILE_HTML, 'w', 'utf-8')
125
126
    write_html_header(file)
127
128
    write_html_columns(file)
129
130
    write_html_footer(file)
131
132
    file.close()
133
134
    webbrowser.open_new_tab('file://' + FILE_HTML)
135
136
137
if __name__ == "__main__":
138
    main()
139