terom@48: """ terom@48: Main entry point for the command-line interface terom@48: """ terom@48: terom@118: import gallery, commands, config terom@48: terom@48: from optparse import OptionParser terom@118: import os.path terom@48: terom@118: def build_config () : terom@118: """ terom@118: Build the default configuration to use terom@118: """ terom@118: terom@118: return config.Configuration() terom@118: terom@118: def option_parser (exec_name) : terom@48: """ terom@48: Build the OptionParser that we use terom@48: """ terom@48: terom@118: parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="???") terom@48: terom@118: parser.add_option('-C', "--config", metavar='PATH', dest="_load_config_path", terom@118: help="Load configuration from PATH") terom@87: terom@118: parser.add_option('-H', "--gallery-path", metavar='DIR', terom@118: help="Use DIR as the Gallery path instead of the CWD") terom@118: terom@118: parser.add_option('-F', "--force-update", action="store_true", terom@101: help="--force-thumb + --force-html") terom@101: terom@118: parser.add_option("--force-thumb", action="store_true", terom@118: help="Force-update thumbnails") terom@101: terom@118: parser.add_option("--force-html", action="store_true", terom@118: help="Force-update .html files") terom@111: terom@118: parser.add_option("--with-exif", action="store_true", terom@111: help="Include Exif metadata in updated .html files") terom@120: terom@120: parser.add_option("--exif-handler", metavar='NAME', dest="exif_handler_name", terom@120: help="Use named Exif handler: pyexiv2, EXIFpy") terom@101: terom@118: parser.add_option('-c', "--thread-count", metavar='COUNT', type="int", terom@118: help="Use COUNT threads for concurrent tasks") terom@117: terom@118: parser.add_option('-d', "--debug", action="store_const", dest="log_level", const=config.logging.DEBUG, terom@101: help="Show debug output") terom@101: terom@118: parser.add_option('-q', "--quiet", action="store_const", dest="log_level", const=config.logging.WARN, terom@118: help="Reduced output (only warnings)") terom@48: terom@48: return parser terom@48: terom@118: def parse_args (config, parser, args) : terom@72: """ terom@118: Parse command-line options/arguments. terom@118: terom@118: Returns the remaining positional arguments. terom@72: """ terom@72: terom@118: # parse the given arguments, storing output directly in the config terom@118: _, args = parser.parse_args(args=args, values=config) terom@101: terom@118: # return the posargs terom@118: return args terom@117: terom@118: def postprocess_config (config) : terom@118: """ terom@118: Post-process our Configuration after our command-line arguments have been parsed. terom@101: terom@118: This will attempt to load any additional configuration. terom@118: """ terom@118: terom@118: # figure out what, if any, path to import terom@118: if hasattr(config, '_load_config_path') : terom@118: path = config._load_config_path terom@87: terom@118: elif os.path.exists('degal.cfg') : terom@118: path = 'degal.cfg' terom@118: terom@118: else : terom@118: return terom@72: terom@118: # import it terom@118: config.import_file(path) 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@118: # build our default config terom@118: config = build_config() terom@118: terom@48: # build optparser terom@48: parser = option_parser(argv[0]) terom@72: terom@118: # parse the args into our config terom@118: args = parse_args(config, parser, argv[1:]) terom@118: terom@118: # postprocess terom@118: postprocess_config(config) 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@118: terom@118: terom@48: # run the selected command terom@76: ret = run_command(config, gallery, command, args, kwargs) terom@118: terom@118: 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: