degal/main.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 23:43:40 +0300
changeset 120 55cb7fc9c8fb
parent 118 60b126ff0b74
child 124 cac613118e75
permissions -rw-r--r--
add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
"""
    Main entry point for the command-line interface
"""

import gallery, commands, config

from optparse import OptionParser
import os.path

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

def option_parser (exec_name) :
    """
        Build the OptionParser that we use
    """
    
    parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="???")
    
    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('-F', "--force-update",   action="store_true",
            help="--force-thumb + --force-html")

    parser.add_option("--force-thumb",          action="store_true",
            help="Force-update thumbnails")

    parser.add_option("--force-html",           action="store_true",
            help="Force-update .html files")
    
    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)")
    
    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 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 our default config
    config = build_config()

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

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

    # postprocess
    postprocess_config(config)

    # 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