terom@48: """ terom@48: Main entry point for the command-line interface terom@48: """ terom@48: terom@72: import gallery, commands, config as config_module terom@48: terom@48: from optparse import OptionParser terom@48: terom@48: def option_parser (command_name) : terom@48: """ terom@48: Build the OptionParser that we use terom@48: """ terom@48: terom@48: # create parser using the given command terom@48: parser = OptionParser(prog=command_name) terom@48: terom@48: # define options terom@87: parser.add_option('-G', "--gallery-path", metavar='DIR', dest='gallery_path', default=None, terom@87: help="Use DIR as the Gallery path [default: CWD]") terom@87: terom@87: parser.add_option('-F', "--force-update", dest='force_update', action="store_true", default=False, terom@101: help="--force-thumb + --force-html") terom@101: terom@101: parser.add_option("--force-thumb", dest='force_thumb', action="store_true", default=False, terom@101: help="Force-update all thumbnails") terom@101: terom@101: parser.add_option("--force-html", dest='force_html', action="store_true", default=False, terom@101: help="Force-update all .html files") terom@101: terom@101: parser.add_option('-d', "--debug", dest='debug', action="store_true", default=False, terom@101: help="Show debug output") terom@101: terom@101: parser.add_option('-q', "--quiet", dest='quiet', action="store_true", default=False, terom@101: help="Reduced output") terom@48: terom@48: return parser terom@48: terom@72: def build_config (options) : terom@72: """ terom@72: Build a configuration object with the given options terom@72: """ terom@72: terom@72: # build default config terom@72: config = config_module.Configuration() terom@72: terom@72: # apply options terom@72: if options.gallery_path : terom@72: config.gallery_path = options.gallery_path terom@72: terom@87: if options.force_update : terom@101: config.force_html = True terom@101: config.force_thumb = True terom@101: terom@101: if options.force_thumb : terom@101: config.force_thumb = True terom@101: terom@101: if options.force_html : terom@101: config.force_html = True terom@101: terom@101: if options.debug : terom@101: config.log_level = config_module.logging.DEBUG terom@101: terom@101: if options.quiet : terom@101: config.log_level = config_module.logging.WARN terom@87: terom@72: # XXX: load config file(s) terom@72: terom@72: return config terom@72: terom@72: def load_gallery (config) : terom@72: """ terom@72: Create the Gallery object that we are manipulating terom@72: """ terom@72: terom@72: # read path from config terom@72: return gallery.Gallery(config.gallery_path, config) terom@72: terom@76: def load_command (config, args) : terom@72: """ terom@72: Figure out what command to run and with what args terom@72: """ terom@72: terom@72: # XXX: hardcoded terom@76: return commands.main, args, {} terom@76: terom@76: def run_command (config, gallery, command, args, kwargs) : terom@76: """ terom@76: Run the given command terom@76: """ terom@76: terom@76: # setup the command execution context terom@76: command_ctx = command.setup(config, gallery) terom@92: terom@92: # run with error handling terom@92: return command_ctx.run() terom@72: terom@48: def main (argv) : terom@48: """ terom@48: Main entry point terom@48: """ terom@48: terom@76: ## load commands terom@76: #commands = load_commands() terom@72: terom@48: # build optparser terom@48: parser = option_parser(argv[0]) terom@48: terom@48: # parse the given argv terom@72: options, args = parser.parse_args(argv[1:]) terom@72: terom@72: # build our config terom@72: config = build_config(options) terom@72: terom@72: # open gallery terom@72: gallery = load_gallery(config) terom@72: terom@72: # figure out what command to run terom@76: command, args, kwargs = load_command(config, args) terom@76: terom@48: # run the selected command terom@76: ret = run_command(config, gallery, command, args, kwargs) terom@72: terom@72: if ret is None : terom@72: # success terom@72: return 0 terom@48: terom@72: else : terom@72: # exit with error code terom@72: assert isinstance(ret, int) terom@72: return ret terom@72: