degal/main.py
author Tero Marttila <terom@fixme.fi>
Thu, 11 Jun 2009 21:26:05 +0300
branchthreaded-tasks
changeset 89 4b254a90d6d0
parent 87 a7a18893730d
permissions -rw-r--r--
add task_threads config setting
"""
    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('-R', "--read-only",      dest='read_only',  action="store_true", default=False,
            help="Do not attempt to modify the gallery")

    parser.add_option('-F', "--force-update",   dest='force_update', action="store_true", default=False,
            help="Force updates, even for fresh items")

    parser.add_option('-j', "--jobs",           metavar='COUNT', type="int", dest='task_threads', default=None,
            help="Set threaded concurrency level for tasks")
    
    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.read_only :
        config.read_only = True

    if options.force_update :
        config.force_update = True

    if options.task_threads :
        config.task_threads = options.task_threads

    # 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)
    
    try :
        # run it
        return command_ctx(*args, **kwargs)
    
    except :
        command_ctx.handle_error()

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