diff options
Diffstat (limited to 'anarchronofile')
-rwxr-xr-x | anarchronofile | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/anarchronofile b/anarchronofile new file mode 100755 index 0000000..26ee7e5 --- /dev/null +++ b/anarchronofile @@ -0,0 +1,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) |