summaryrefslogtreecommitdiff
path: root/scripts/view.py
blob: 9486e51e9004a42881ced078d4ba1f67ff6bb57f (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#! /usr/bin/env python3
#===============================
#
# viewer
#
# 2019/02/07 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
#===============================
import yaml
import sys
import os
import re

import base

#====================================
#
# viewer
#
#====================================
class viewer(base.base):

    #--------------------
    # parse
    #
    # -f :          with file
    # -r :          with relationships
    # --oneline:    one line
    #--------------------
    def parse(self, arg):
        for arg in arg:
            if (arg == "-f"):
                self.with_file = 1
            elif (arg == "-r"):
                self.with_rel = 1
            elif (arg == "--oneline"):
                self.one_line = 1
            elif (os.path.exists(arg) and
                  re.match("{}/projects".format(self.top()),
                           os.path.abspath(arg))):
                self.files += [arg]

    #--------------------
    # __init__
    #--------------------
    def __init__(self, arg):

        self.text	= ""
        self.files	= []
        self.with_file	= 0
        self.with_rel	= 0
        self.one_line	= 0

        self.parse(arg)
        self.data = {}
        self.file = ""

    #--------------------
    # get_item
    #--------------------
    def get_item(self, data):
        # Ughhhh
        # All Yaml data is "dictionary", and not simple to getting data !!
        # ( `⌒´)d	I hate you !!
        return list(data)[0]

    #--------------------
    # get_related_file
    #--------------------
    def get_related_file(self, data):
        file = self.run("egrep -l \"key:\s+{}\" -r {}/projects".\
                        format(data, self.top()))
        if (file):
            return file
        else:
            return data

    #--------------------
    # set_data
    # get_data
    #--------------------
    def set_data(self, file):
        F = open(file, "r+")
        self.data = yaml.load(F)
        F.close()
        self.file = file

    def get_data(self, key):
        data = self.data[key]
        if (data):
            return data
        else:
            return []

    #--------------------
    # make_one
    #	for --oneline
    #--------------------
    def make_one(self, tag, file):

        if (len(self.text) > 0):
            self.text += "\n"

        if (tag):
            self.text += "{:<22}".format(self.color("tag", tag))

        if (not os.path.exists(file)):
            self.text += self.color("error", "No such task ({})".format(file))
            return

        self.set_data(file)

        # show "key"
        self.text += "{}: {}".format(
            self.color("key", self.get_data("key")),
            self.get_data("title"))

        # show relationships
        relationships = self.get_data("relationships")
        if ((not tag) and self.with_rel and relationships):
            for d in relationships:
                self.make_one(self.get_item(d.keys()),
                              self.get_related_file(self.get_item(d.values())))

    #--------------------
    # make_full
    #	for normal
    #--------------------
    def make_full(self, tag, file):

        if (len(self.text) > 0):
            self.text += "\n"

        if (tag):
            self.text += "{:<22}".format(self.color("tag", tag))

        if (not os.path.exists(file)):
            self.text += self.color("error", "No such task ({})".format(file))
            return

        self.set_data(file)

        # show "key"
        self.text += "{}, {}, {}\n".format(
            self.color("key",    self.get_data("key")),
            self.color("status", self.get_data("status")),
            self.color("team",   self.get_data("team")));

        # show title
        self.text += "Title:    {}\n".format(self.get_data("title"))

        # show assignee
        self.text += "Assignee: {}\n".format(self.get_data("assignee"))

        if (self.with_file):
            self.text += "File:     {}\n".format(
                os.path.abspath(file).replace("{}/".format(self.top()), ""))

        # show comments
        comments = self.get_data("comments")
        if (comments):
            for comment in comments:
                self.text += "\n{}".format(comment)

        # show relationships
        relationships = self.get_data("relationships")
        if ((not tag) and self.with_rel and relationships):
            for d in relationships:
                self.make_full(self.get_item(d.keys()),
                               self.get_related_file(self.get_item(d.values())))

    #--------------------
    # show
    #--------------------
    def show(self):
        for f in self.files:
            if (self.one_line):
                self.make_one(None, f)
            else:
                self.make_full(None, f)

        # escape " -> \"
        # otherwise " will be disappeared
        self.text = self.text.replace("\"", "\\\"");
        self.do("echo \"{}\" | {}".format(
            self.text, self.config("viewer")))

#====================================
#
# As command
#
#====================================
if __name__=='__main__':
    viewer(sys.argv).show()