degal/main.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Jul 2009 21:59:01 +0300
changeset 144 97505a789003
parent 124 cac613118e75
permissions -rw-r--r--
reorganize/rename the commands and their options stuff, options like --force-html are now split out from main.py into commands/update.py
"""
    Main entry point for the command-line interface
"""

import gallery, command, commands, config, version

from optparse import OptionParser
import os.path

def load_commands () :
    """
        Build the CommandList for us to use
    """

    return command.CommandList(commands.COMMANDS)

def build_config () :
    """
        Build the default configuration to use
    """
    
    return config.Configuration()

def option_parser (exec_name, command) :
    """
        Build the OptionParser that we use, with the given command
    """
    
    parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="Degal %s" % version.VERSION_STRING)
    
    # core options
    parser.add_option('-C', "--config",         metavar='PATH', dest="_load_config_path",
            help="Load configuration from PATH")

    parser.add_option('-H', "--gallery-path",   metavar='DIR',
            help="Use DIR as the Gallery path instead of the CWD")

    parser.add_option("--with-exif",            action="store_true",
            help="Include Exif metadata in updated .html files")
    
    parser.add_option("--exif-handler",         metavar='NAME', dest="exif_handler_name",
            help="Use named Exif handler: pyexiv2, EXIFpy")

    parser.add_option('-c', "--thread-count",   metavar='COUNT', type="int",
            help="Use COUNT threads for concurrent tasks")

    parser.add_option('-d', "--debug",          action="store_const", dest="log_level", const=config.logging.DEBUG,
            help="Show debug output")

    parser.add_option('-q', "--quiet",          action="store_const", dest="log_level", const=config.logging.WARN,
            help="Reduced output (only warnings)")
    
    # command's options
    parser.add_option_group(command.option_group(parser))

    return parser

def parse_args (config, parser, args) :
    """
        Parse command-line options/arguments.

        Returns the remaining positional arguments.
    """
    
    # parse the given arguments, storing output directly in the config
    _, args = parser.parse_args(args=args, values=config)

    # return the posargs
    return args

def postprocess_config (config) :
    """
        Post-process our Configuration after our command-line arguments have been parsed.

        This will attempt to load any additional configuration.
    """
    
    # figure out what, if any, path to import
    if hasattr(config, '_load_config_path') :
        path = config._load_config_path

    elif os.path.exists('degal.cfg') :
        path = 'degal.cfg'
    
    else :
        return

    # import it
    config.import_file(path)

def load_gallery (config) :
    """
        Create the Gallery object that we are manipulating
    """
    
    # read path from config
    return gallery.Gallery(config.gallery_path, config)

def main (argv) :
    """
        Main entry point
    """

    ## load commands
    commands = load_commands()

    # build our default config
    config = build_config()
    
    
    # XXX: hardcoded
    command = commands.lookup('update')

    # build optparser
    parser = option_parser(argv[0], command)

    # parse the args into our config
    args = parse_args(config, parser, argv[1:])

    # postprocess
    postprocess_config(config)

    # open gallery
    gallery = load_gallery(config)

    # run the selected command
    # XXX: mix up configs with options
    ret = command.apply(config, gallery, config, *args).run()


    if ret is None :
        # success
        return 0

    else :
        # exit with error code
        assert isinstance(ret, int)
        return ret