From 2435cb246a8d9f05bf69c4a4474eb1bc6aed12e7 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sat, 20 Feb 2021 14:50:44 -0300 Subject: Feat: anarchronofile --- anarchronofile | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 anarchronofile diff --git a/anarchronofile b/anarchronofile new file mode 100755 index 0000000..a21aa08 --- /dev/null +++ b/anarchronofile @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Ana(r)chronofile parser. +# +# Copyright (C) 2021 Silvio Rhatto +# +# 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 . + +# 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]) + + self.args = args + self.file = open(args.file[0], '+r') + self.yaml = yaml.load(self.file) + + def run(self): + 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) -- cgit v1.2.3