degal/main.py
author Tero Marttila <terom@fixme.fi>
Fri, 05 Jun 2009 23:05:49 +0300
changeset 72 168a2d065f17
parent 48 20355dd2e61a
child 76 e22d9f699081
permissions -rw-r--r--
use new command/gallery stuff in main.py
"""
    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", dest='gallery_path', help="Use DIR as the Gallery path [default: CWD]", metavar='DIR', default=None)
    parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", default=True)
    
    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

    # 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 (options, args) :
    """
        Figure out what command to run and with what args
    """
    
    # XXX: hardcoded
    return commands.main, args

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_func, command_args = load_command(options, args)
    
    # run the selected command
    ret = command_func(gallery, *command_args)
    
    if ret is None :
        # success
        return 0

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