terom@48: """ terom@48: Main entry point for the command-line interface terom@48: """ terom@48: terom@144: import gallery, command, commands, config, version terom@48: terom@48: from optparse import OptionParser terom@118: import os.path terom@48: terom@144: def load_commands () : terom@144: """ terom@144: Build the CommandList for us to use terom@144: """ terom@144: terom@144: return command.CommandList(commands.COMMANDS) terom@144: 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@144: def option_parser (exec_name, command) : terom@48: """ terom@144: Build the OptionParser that we use, with the given command terom@48: """ terom@48: terom@124: parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="Degal %s" % version.VERSION_STRING) terom@48: terom@144: # core options 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("--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@144: # command's options terom@144: parser.add_option_group(command.option_group(parser)) terom@144: 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@48: def main (argv) : terom@48: """ terom@48: Main entry point terom@48: """ terom@48: terom@76: ## load commands terom@144: commands = load_commands() terom@72: terom@118: # build our default config terom@118: config = build_config() terom@144: terom@144: terom@144: # XXX: hardcoded terom@144: command = commands.lookup('update') terom@118: terom@48: # build optparser terom@144: parser = option_parser(argv[0], command) 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@48: # run the selected command terom@144: # XXX: mix up configs with options terom@144: ret = command.apply(config, gallery, config, *args).run() 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: