aboutsummaryrefslogtreecommitdiff
path: root/weekly/weekly.py
blob: 076192a32a75669a26d9264afca01669d3ec9dcf (plain) (blame)
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
import os
import datetime
from subprocess import Popen
from . import __version__


def user_message(tm, status):
    banner = '=' * 80
    marker = '{0}\n{1}\n{2}\n{0}'.format(banner, tm, status)
    done = False
    lines = []

    while not done:
        msg = ''
        output = ''

        try:
            print('>>>', end='')
            msg = input()
        except EOFError:
            done = True

        if msg == '.' or msg == 'EOF':
            done = True

        if not done:
            if len(lines) < 1:
                lines.append(marker)
            lines.append(msg)

        if lines:
            output = '\n'.join([x for x in lines])
            output += '\n'

    return output


def show_messages(path):
    for root, dirs, files in os.walk(path):
        for f in sorted(files, reverse=False):
            ingest = os.path.join(root, f)
            with open(ingest, 'r') as fp:
                for line in fp:
                    print(line.rstrip())


def main():
    DATA = os.path.abspath(os.path.expanduser('~/.reports/data'))
    now = datetime.datetime.now()
    year, weekno, day = now.isocalendar()

    if not os.path.exists(DATA):
        os.makedirs(DATA)

    todays_path = os.path.join(DATA, str(year), str(weekno))
    todays_file = os.path.join(todays_path, str(day))

    if not os.path.exists(todays_path):
        os.makedirs(todays_path)

    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--important', action='store_true',
                        required=False)
    parser.add_argument('-r', '--retrieve', action='store_true')
    parser.add_argument('-e', '--edit', action='store_true')
    parser.add_argument('-V', '--version', action='store_true')
    args = parser.parse_args()

    if args.version:
        print(__version__)
        return 0

    if args.edit:
        editor = '/usr/bin/vim'
        if 'EDITOR' in os.environ:
            editor = os.environ['EDITOR']
        command = ' '.join([editor, todays_file])
        proc = Popen(command.split())
        proc.communicate()
        retval = proc.wait()
        if retval is None:
            retval = 1

        return retval

    if args.retrieve:
        show_messages(todays_path)
        return 0

    status = 'NOTE'
    if args.important:
        status = 'IMPORTANT'

    message = user_message(now, status)
    with open(todays_file, 'a') as fp:
        fp.write(message)
        fp.write(os.linesep)

    return 0


if __name__ == '__main__':
    sys.exit(main())