diff --git a/wag/wag.py b/wag/wag.py index 4b2a50a..28a1587 100755 --- a/wag/wag.py +++ b/wag/wag.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python import feedparser import sys import time @@ -6,10 +5,7 @@ import ConfigParser from templates import get_rendered_string, TemplateNotFound import filters -import wagparser -default_config = os.environ['HOME'] + '/.wag/feeds' -default_template = 'default_rss_template' def get_feed(func): def its_a_front(args): @@ -18,124 +14,80 @@ def its_a_front(args): return func(args, entries) return its_a_front - -@get_feed -def display_feed(args, entries): - """Read the feeds once """ - number_of_entries = len(entries) - if number_of_entries == 0: - print "There are zero feeds for %s with url %s" % (args.name, args.url) - sys.exit(1) - try: - print get_rendered_string(args.template, entries[number_of_entries-args.lines:]) - except TemplateNotFound: - print "%s does not appear to be a valid template in your template path" % args.template - sys.exit(1) - except AttributeError: - print "Either invalid url or invalid name" +class Wag(object): + def __init__(self, args, feeds_object): + self.args = args + self.feeds_object = feeds_object -wag_parser = wagparser.WagParser(display_feed, prog="wag", - description="tail rss/atom feeds") + @multi_feed + def display_feed(self, feed): + """Takes a list of entries from config""" + number_of_entries = len(feed) + if number_of_entries == 0: + print "There are zero feeds for %s with url %s" % (args.name, args.url) + sys.exit(1) -wag_parser.add_argument('-n', '--lines', type=int, default=None, - help="The number of entries") -wag_parser.add_argument('-t', '--template', default=None, - help='the template to render. REMINDER: must be in template_path') -wag_parser.add_argument('-c', '--config', default=default_config, - help="Use a new config file. (default: %s)" % default_config) -wag_parser.add_argument('-s', '--sleep-interval', type=int, default=300, - help='with -f, sleep for approximately N seconds (default 1.0) between iterations') -wag_parser.add_argument('-v', '--verbose', action='store_true') -wag_parser.add_argument('name', metavar='name/url', default=None) + try: + print get_rendered_string(args.template, entries[number_of_entries-args.lines:]) + except TemplateNotFound: + print "%s does not appear to be a valid template in your template path" % args.template + sys.exit(1) + except AttributeError: + print "Either invalid url or invalid name" -@wag_parser.arg_function('-k', '--keys', - help="prints out the valid keys for that url/name") -@get_feed -def show_keys(args, entries): - - for k in entries[0]: - key_str = "'%s'" % str(k) - #this was stolen from rson - if args.verbose is True: - key_str += " => '%s'" % entries[0][k] - - print key_str - - sys.exit() - -@wag_parser.arg_function('-l', '--list', help="lists all the valid names in your config file") -def list(args): - config_file = ConfigParser.RawConfigParser() - config_file.read(args.config) - for section in config_file.sections(): - print "'" + section + "'" - sys.exit() - -@wag_parser.arg_function('-f', '--follow') -@get_feed -def follow(args, entries): - try: - last_entry = entries[-1] - while True: - time.sleep(args.sleep_interval) - - new_entries = feedparser.parse(args.url).entries - - pos = len(new_entries) - for entry in new_entries: - if entry.updated_parsed > last_entry.updated_parsed: - pos -= 1 - else: - break - - new_entries.reverse() - - try: - rendered_string = get_rendered_string(args.template, new_entries[pos:]) - if rendered_string != '': - print rendered_string - except IndexError: - pass - - last_entry = new_entries[-1] - - except KeyboardInterrupt: - sys.exit() - - except IndexError: - print "%s has no entries or is an invalid url" % args.url - sys.exit(1) - + @single_feed + def show_keys(self, feed): + + for k in entries[0]: + key_str = "'%s'" % str(k) + #this was stolen from rson + if args.verbose is True: + key_str += " => '%s'" % entries[0][k] + + print key_str -@get_feed -def fix_lines(args, entries): - number_of_entries = len(entries) - if args.lines is None: - args.lines = number_of_entries + sys.exit() - return args - -def get_config(args): - - config_file = ConfigParser.RawConfigParser() - config_file.read(args.config) - - try: - args.url = config_file.get(args.name, 'url') - except ConfigParser.NoSectionError: - args.url = args.name - - if args.template is None: + def list(self): + config_file = ConfigParser.RawConfigParser() + config_file.read(args.config) + for section in config_file.sections(): + print "'" + section + "'" + sys.exit() + + @multi_feed + def follow(self, feed): try: - args.template = config_file.get(args.name, 'template') - except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): - args.template = default_template - - return args - -def main(): - result = wag_parser.run_parser([get_config, fix_lines]) - + last_entry = entries[-1] + while True: + time.sleep(args.sleep_interval) + + new_entries = feedparser.parse(args.url).entries + + pos = len(new_entries) + for entry in new_entries: + if entry.updated_parsed > last_entry.updated_parsed: + pos -= 1 + else: + break + + new_entries.reverse() + + try: + rendered_string = get_rendered_string(args.template, new_entries[pos:]) + if rendered_string != '': + print rendered_string + except IndexError: + pass + + last_entry = new_entries[-1] + + except KeyboardInterrupt: + sys.exit() + + except IndexError: + print "%s has no entries or is an invalid url" % args.url + sys.exit(1) diff --git a/wag/wagparser.py b/wag/wagparser.py deleted file mode 100644 index 2bb71eb..0000000 --- a/wag/wagparser.py +++ /dev/null @@ -1,72 +0,0 @@ -""" - -This module allows us to call functions when a its flag is detected. -For example: - - @arg_function('-f', '--feed') - def feed(args): - #do something. - -if we pass -f or --feed on the command line the feed function should be run -and all of the other arguments will be passed into it. - -""" -import argparse - -class WagParser(argparse.ArgumentParser): - def __init__(self, default=None, *args, **kwargs): - super(WagParser, self).__init__(*args, **kwargs) - self.order = [] - self.command_list = {} - self.default = default - - def arg_function(self, *args, **kwargs): - """ - - Allows the decorated function to be called when its flag is passed. - All functions with this decorator lose the ability to specify an - action. It is always changed to 'store_true'. - - Note: If you care about priority the functions at the top of the - source file will have higher priority than the functions at the bottom of the - source file. - - """ - kwargs['action']='store_true' - the_arg = self.add_argument(*args, **kwargs) - self.order.append(the_arg.dest) - def wrapped(func): - self.command_list[the_arg.dest] = func - return func - return wrapped - - def run_parser(self, funcs): - """ - This does all the heavy lifting and will actually run your program - It finds which functions to call and when. - - Once your functions have ran it will return your functions return values - in a dictionary with the following format {argparse_dest: return_value} - - funcs is a list of callable objects that get called right after the args - are parsed. It takes the list of arguments and modifies them. Each function - in the list must have the following siginature: - func(args): - return newargs - - """ - args = self.parse_args() - if type(funcs) != list(): - funcs = list(funcs) - - for func in funcs: - args = func(args) - - results = {} - for dest in self.order: - if getattr(args,dest): - results[dest] = self.command_list[dest](args) - - if results == {} and self.default: - results['default'] = self.default(args) - return results