degal/main.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 16:19:27 +0300
changeset 109 66a01c0806f1
parent 101 698dc68a985d
child 111 ecceaf23c969
permissions -rw-r--r--
backout config.read_only as a useless feature
"""
    Main entry point for the command-line interface
"""

import gallery, commands, config as config_module

from optparse import OptionParser

def option_parser (command_name) :
    """
        Build the OptionParser that we use
    """
    
    # create parser using the given command
    parser = OptionParser(prog=command_name)
    
    # define options
    parser.add_option('-G', "--gallery-path",   metavar='DIR',  dest='gallery_path',    default=None,
            help="Use DIR as the Gallery path [default: CWD]")

    parser.add_option('-F', "--force-update",   dest='force_update', action="store_true", default=False,
            help="--force-thumb + --force-html")

    parser.add_option("--force-thumb",          dest='force_thumb', action="store_true", default=False,
            help="Force-update all thumbnails")

    parser.add_option("--force-html",           dest='force_html', action="store_true", default=False,
            help="Force-update all .html files")

    parser.add_option('-d', "--debug",          dest='debug', action="store_true", default=False,
            help="Show debug output")

    parser.add_option('-q', "--quiet",           dest='quiet', action="store_true", default=False,
            help="Reduced output")
    
    return parser

def build_config (options) :
    """
        Build a configuration object with the given options
    """
    
    # build default config
    config = config_module.Configuration()
    
    # apply options
    if options.gallery_path :
        config.gallery_path = options.gallery_path
    
    if options.force_update :
        config.force_html = True
        config.force_thumb = True
    
    if options.force_thumb :
        config.force_thumb = True

    if options.force_html :
        config.force_html = True

    if options.debug :
        config.log_level = config_module.logging.DEBUG

    if options.quiet :
        config.log_level = config_module.logging.WARN

    # XXX: load config file(s)

    return config

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

def load_command (config, args) :
    """
        Figure out what command to run and with what args
    """
    
    # XXX: hardcoded
    return commands.main, args, {}

def run_command (config, gallery, command, args, kwargs) :
    """
        Run the given command
    """
    
    # setup the command execution context
    command_ctx = command.setup(config, gallery)
  
    # run with error handling
    return command_ctx.run()

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

    ## load commands
    #commands = load_commands()

    # build optparser
    parser = option_parser(argv[0])
    
    # parse the given argv
    options, args = parser.parse_args(argv[1:])

    # build our config
    config = build_config(options)

    # open gallery
    gallery = load_gallery(config)

    # figure out what command to run
    command, args, kwargs = load_command(config, args)

    # run the selected command
    ret = run_command(config, gallery, command, args, kwargs)
    
    if ret is None :
        # success
        return 0

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