feedwarrior

Slim, portable tooling for creating and distributing decentralized append logs
git clone git://git.defalsify.org/logwarrior.git
Info | Log | Files | Refs | README | LICENSE

feedwarrior (3782B)


      1 #!/usr/bin/python
      2 
      3 # Author: Louis Holbrook <dev@holbrook.no> (https://holbrook.no)
      4 # License: GPLv3
      5 # Description: Work log tool
      6 
      7 # standard imports
      8 import os
      9 import sys
     10 import argparse
     11 import configparser
     12 import json
     13 import logging
     14 
     15 # local imports
     16 import feedwarrior
     17 from feedwarrior.cmd import create as cmd_create
     18 from feedwarrior.cmd import entry as cmd_entry
     19 from feedwarrior.cmd import show as cmd_show
     20 from feedwarrior.cmd import ls as cmd_list
     21 from feedwarrior.cmd import add as cmd_add
     22 
     23 logging.basicConfig(level=logging.ERROR)
     24 logg = logging.getLogger()
     25 
     26 
     27 def matches_part(full, part):
     28     if len(part) > len(full):
     29         return False
     30     return full[:len(part)] == part
     31 
     32 
     33 
     34 argparser = argparse.ArgumentParser(description='create and manipulate feedwarrior feeds')
     35 argparser.add_argument('-l', help='feed log to operate on')
     36 argparser.add_argument('-c', type=str, default='config.ini', help='configuration file')
     37 argparser.add_argument('-v', action='store_true', help='be verbose')
     38 argparser.add_argument('--headers', action='store_true', help='add headers in output')
     39 sub = argparser.add_subparsers()
     40 # TODO: add subparser to same level flags as main parser
     41 sub.dest = 'command'
     42 sub_entry = sub.add_parser('entry', help='add entry to feed')
     43 cmd_entry.parse_args(sub_entry)
     44 sub_show = sub.add_parser('show', help='view feed log')
     45 cmd_show.parse_args(sub_show)
     46 sub_create = sub.add_parser('create', help='create new feed')
     47 cmd_create.parse_args(sub_create)
     48 sub_list = sub.add_parser('list', help='list feeds')
     49 cmd_list.parse_args(sub_list)
     50 sub_add = sub.add_parser('add', help='add new entry with editor')
     51 cmd_add.parse_args(sub_add)
     52 
     53 
     54 args = argparser.parse_known_args()
     55 args = argparser.parse_args(args[1], args[0])
     56 if args.v:
     57     logging.getLogger().setLevel(logging.DEBUG)
     58 
     59 logg.debug('attempting to load config {}'.format(args.c))
     60 config = feedwarrior.load_config(args.c)
     61 
     62 
     63 def get_feed_by_name(s):
     64     index_path = os.path.join(config.feeds_dir, 'names', s)
     65     resolved_path = os.path.realpath(index_path)
     66     os.stat(resolved_path)
     67     logg.debug('feed path {} resolves to {}'.format(index_path, resolved_path))
     68     return os.path.basename(resolved_path)
     69 
     70 feed_current = None
     71 if args.l != None:
     72     try:
     73         uu = feedwarrior.common.parse_uuid(args.l)
     74         feed_current = feedwarrior.feed(uu)
     75     except ValueError:
     76         try:
     77             uu = get_feed_by_name(args.l)
     78             feed_current = feedwarrior.feed(uu)
     79         except FileNotFoundError as e:
     80             sys.stderr.write('cannot resolve feed {}\n'.format(args.l))
     81             sys.exit(1)
     82 
     83 
     84 
     85 cmd_mod = None
     86 if args.command == None or matches_part('show', args.command):
     87     if feed_current == None:
     88         sys.stderr.write('plesae speficy a feed for showing\n')
     89         sys.exit(1)
     90     feed_current = feedwarrior.load_feed(config.data_dir, feed_current.uuid)
     91     cmd_mod = cmd_show
     92 elif matches_part('create', args.command):
     93     feed_current = feedwarrior.feed(parent=feed_current)
     94     cmd_mod = cmd_create
     95 elif matches_part('entry', args.command):
     96     cmd_mod = cmd_entry
     97 elif matches_part('list', args.command) or args.command == 'ls':
     98     cmd_mod = cmd_list
     99 elif matches_part('add', args.command):
    100     cmd_mod = cmd_add
    101 else:
    102     sys.stderr.write('invalid command {}\n'.format(args.command))
    103     sys.exit(1)
    104 
    105 try:
    106     os.makedirs(config.entries_dir, mode=0o777, exist_ok=False)
    107     os.makedirs(config.feeds_dir, mode=0o777, exist_ok=False)
    108     os.makedirs(os.path.join(config.feeds_dir, 'names'), mode=0o777, exist_ok=False)
    109     logg.debug('creating datadir {}'.format(config.data_dir))
    110 except FileExistsError as e:
    111     logg.debug('found existing datadir {}'.format(config.data_dir))
    112 
    113 cmd_mod.check_args(args)
    114 
    115 if __name__ == '__main__':
    116     cmd_mod.execute(config, feed_current, args)