aboutsummaryrefslogtreecommitdiff
path: root/anarchronofile
blob: 26ee7e54823f9dd8c978cfbd5bc8c3c3f0259909 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Ana(r)chronofile parser.
#
# Copyright (C) 2021 Silvio Rhatto <rhatto@riseup.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Dependencies
import os
import yaml
import argparse
from pprint import pprint

class AnarChronofile:
    def __init__(self, args):
        if not os.path.exists(args.file[0]):
            raise FileNotFoundError('File not found: ' + args.file[0])

        if os.path.isfile(args.file[0]):
            self.args = args
            self.file = open(args.file[0], '+r')
            self.yaml = yaml.load(self.file)

    def run(self):
        if getattr(self, 'args', None) is None:
            return

        print(self.args)
        print("---")
        print("")

        if self.args.topic is not None:
            topic = self.args.topic

            for item in self.yaml:
                if topic in item:
                    if self.args.subtopic is not None:
                        subtopic = self.args.subtopic

                        if subtopic in item[topic]:
                            print('Date: ' + str(item['date']))
                            print("\n")
                            pprint(item[topic][subtopic])
                            print("\n")

                    else:
                        print('Date: ' + str(item['date']))
                        print("\n")
                        pprint(item[topic])
                        print("\n")

        else:
            pprint(self.yaml)

def cmdline():
    """
    Evalutate the command line.

    :return: Command line arguments.
    """

    basename = os.path.basename(__file__)
    examples = "Examples:\n\t" + basename + " incoming.yaml\n"
    epilog   = examples
    parser   = argparse.ArgumentParser(description='Parse an ana(r)chronofile',
                                       epilog=epilog,
                                       formatter_class=argparse.RawDescriptionHelpFormatter,)

    parser.add_argument('file',     nargs=1,   help='YAML file')
    parser.add_argument('topic',    nargs='?', help='Topic to search')
    parser.add_argument('subtopic', nargs='?', help='Subtopic to search')

    # Get args
    args = parser.parse_args()

    return args

if __name__ == "__main__":
    args = cmdline()

    try:
        parser = AnarChronofile(args)
        parser.run()
    except (FileNotFoundError, KeyboardInterrupt) as e:
        print(e)
        exit(1)