Passed
Push — master ( b6cc82...5a7423 )
by Alexander
01:23
created

src.things3_kanban.write_html_column()   C

Complexity

Conditions 11

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nop 4
dl 0
loc 33
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like src.things3_kanban.write_html_column() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""KanbanView (static) 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__ = "2.0.0"
13
__maintainer__ = "Alexander Willner"
14
__email__ = "[email protected]"
15
__status__ = "Development"
16
17
import codecs
18
from os import getcwd
19
from things3 import Things3
20
21
# Basic variables
22
FILE_HTML = getcwd() + '/kanban-static.html'
23
THINGS3 = Things3()
24
25
26
def write_html_column(cssclass, file, header, rows):
27
    """Create a column in the output."""
28
29
    file.write("<div class='column'><div class=''>" +
30
               "<h2 class='" + cssclass + "'>" + header +
31
               "<span class='size'>" + str(len(rows)) +
32
               "</span></h2>")
33
34
    for row in rows:
35
        task_uuid = str(row[THINGS3.I_UUID]) \
36
            if row[THINGS3.I_UUID] is not None else ''
37
        task_title = str(row[THINGS3.I_TITLE]) \
38
            if row[THINGS3.I_TITLE] is not None else ''
39
        context_title = str(row[THINGS3.I_CONTEXT]) \
40
            if row[THINGS3.I_CONTEXT] is not None else ''
41
        context_uuid = str(row[THINGS3.I_CONTEXT_UUID]) \
42
            if row[THINGS3.I_CONTEXT_UUID] is not None else ''
43
        deadline = str(row[THINGS3.I_DUE]) \
44
            if row[THINGS3.I_DUE] is not None else ''
45
46
        task_link = '<a href="things:///show?id=' + task_uuid + '">' + \
47
            task_title + '</a>' if task_uuid != '' else task_title
48
        context_link = '<a href="things:///show?id=' + context_uuid + '">' + \
49
            context_title + '</a>' if context_uuid != '' else context_title
50
        css_class = 'hasProject' if context_title != '' else 'hasNoProject'
51
        css_class = 'hasDeadline' if deadline != '' else css_class
52
53
        file.write('<div class="box">' + task_link +
54
                   '<div class="deadline">' + deadline + '</div>' +
55
                   '<div class="area ' + css_class + '">' + context_link +
56
                   '</div>' +
57
                   '</div>')
58
    file.write("</div></div>")
59
60
61
def write_html_header(file):
62
    """Write HTML header."""
63
64
    message = """
65
        <!DOCTYPE html>
66
        <html>
67
        <head>
68
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
69
          <link rel="stylesheet" href="./resources/kanban.css">
70
          <title>KanbanView for Things 3</title>
71
        </head>
72
73
        <body>
74
          <header>
75
            <a href="#" onclick="refresh();" title="click to refresh">
76
              <img class="logo" src="./resources/logo.png" alt="logo">
77
            </a>
78
          </header>
79
          <article class='some-page-wrapper'>
80
            <div class='row'>
81
        """
82
    file.write(message)
83
84
85
def write_html_footer(file):
86
    """Write HTML footer."""
87
88
    message = """
89
            </div>
90
          </article>
91
        <footer class="footer"><br />
92
        Copyright &copy;2018 Luc Beaulieu / 2020 Alexander Willner
93
        </footer></body></html>"""
94
    file.write(message)
95
96
97
def write_html_columns(file):
98
    """Write HTML columns."""
99
100
    write_html_column("color1", file, "Backlog", THINGS3.get_someday())
101
    write_html_column("color5", file, "Upcoming", THINGS3.get_upcoming())
102
    write_html_column("color3", file, "Waiting", THINGS3.get_waiting())
103
    write_html_column("color4", file, "Inbox", THINGS3.get_inbox())
104
    write_html_column("color2", file, "MIT", THINGS3.get_mit())
105
    write_html_column("color6", file, "Today", THINGS3.get_today())
106
    write_html_column("color7", file, "Next", THINGS3.get_anytime())
107
108
109
def main():
110
    """Convert Things 3 database to Kanban HTML view."""
111
112
    with codecs.open(FILE_HTML, 'w', 'utf-8') as file:
113
        write_html_header(file)
114
        write_html_columns(file)
115
        write_html_footer(file)
116
117
118
if __name__ == "__main__":
119
    main()
120