#! /usr/bin/python2
"""The Planet aggregator.

A flexible and easy-to-use aggregator for generating websites.

Visit http://www.planetplanet.org/ for more information and to download
the latest version.

Requires Python 2.1, recommends 2.3.
"""

__authors__ = [ "Scott James Remnant <scott@netsplit.com>",
                "Jeff Waugh <jdub@perkypants.org>" ]
__license__ = "Python"


import os, shutil, sys
from os import path

DEFAULT_CONFIGURATION_FILE_NAME = "/usr/share/planet-venus/example/default.ini"
DEFAULT_THEME_DIRECTORY_NAME = "/usr/share/planet-venus/theme/default"

if __name__ == "__main__":
    config_file = []
    offline = 0
    verbose = 0
    debug = 0
    only_if_new = 0
    expunge = 0
    debug_splice = 0
    no_publish = 0

    def display_error(message):
        print >> sys.stderr, message
        print >> sys.stderr
        print >> sys.stderr, "Try `planet --help' for more information."
        sys.exit(1)

    for key, arg in enumerate(sys.argv[1:]):
        if arg == "-h" or arg == "--help":
            print """Usage: planet [OPTION]... [CONFIGURATION-FILE]

Planet Venus downloads news feeds published by web sites and aggregates their
content together into a single combined feed, latest news first.

The exit status is 0 for success or 1 for failure.

Options:

  -h, --help              display a short help message and exit
  -V, --version           display version information and exit
  -v, --verbose           verbose logging during update
  -D, --debug             debug logging during update
  -d  --debug-splice      debug splice
  -o, --offline           update from the cache only
  -n, --only-if-new       only spider new feeds
  -x, --expunge           expunge old entries from cache
  -c, --create=DIRECTORY  create a new planet in DIRECTORY
  --no-publish             Do not publish feeds using PubSubHubbub

Examples:

You can create a default planet:

  planet --create example

You can build this default planet right away:

  cd example && planet --verbose planet.ini

You can test the results in your favourite browser:

  sensible-browser output/index.html

The configuration file has more details for customisation:

  sensible-editor planet.ini

Report bugs using the `reportbug' command."""
            sys.exit(0)
        elif arg == "-V" or arg == "--version":
            print """planet - Planet Venus @version@

Copyright 2008, Sam Ruby <rubys@intertwingly.net>

Licenced under the Python Software Foundation License Version 2.

Written by Sam Ruby <rubys@intertwingly.net>."""
            sys.exit(0)
        elif arg == "-c" or arg == "--create":
            planet_directory_name = sys.argv[key + 2]
            configuration_file_name = path.join(planet_directory_name, "planet.ini")
            theme_directory_name = path.join(planet_directory_name, "theme")
            if not path.exists(planet_directory_name):
                os.makedirs(planet_directory_name)
            if not path.exists(configuration_file_name):
                shutil.copy(DEFAULT_CONFIGURATION_FILE_NAME, configuration_file_name)
            if not path.exists(theme_directory_name):
                shutil.copytree(DEFAULT_THEME_DIRECTORY_NAME, theme_directory_name)
        elif arg == "-v" or arg == "--verbose":
            verbose = 1
        elif arg == "-D" or arg == "--debug":
            debug = 1
        elif arg == "-o" or arg == "--offline":
            offline = 1
        elif arg == "-n" or arg == "--only-if-new":
            only_if_new = 1
        elif arg == "-x" or arg == "--expunge":
            expunge = 1
        elif arg == "-d" or arg == "--debug-splice":
            debug_splice = 1
        elif arg == "--no-publish":
            no_publish = 1
        elif arg.startswith("-"):
            display_error("Unknown option: %s" % arg)
        else:
            config_file.append(arg)

    if not config_file:
        display_error("No configuration file specified.")

    for i in config_file:
        if not path.exists(i):
            display_error("The configuration file does not exist: %s" % i)

    from planet import config
    config.load(config_file or 'config.ini')

    import planet
    if debug:
        planet.getLogger('DEBUG',config.log_format())
    elif verbose:
        planet.getLogger('INFO',config.log_format())

    if not offline:
        from planet import spider
        try:
            spider.spiderPlanet(only_if_new=only_if_new)
        except Exception, e:
            print e

    from planet import splice
    doc = splice.splice()

    if debug_splice:
        from planet import logger
        logger.info('writing debug.atom')
        debug=open('debug.atom','w')
        try:
            from lxml import etree
            from StringIO import StringIO
            tree = etree.tostring(etree.parse(StringIO(doc.toxml())))
            debug.write(etree.tostring(tree, pretty_print=True))
        except:
            debug.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
        debug.close

    splice.apply(doc.toxml('utf-8'))

    if config.pubsubhubbub_hub() and not no_publish:
        from planet import publish
        publish.publish(config)

    if expunge:
        from planet import expunge
        expunge.expungeCache
