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@72: parser.add_option('-G', "--gallery-path", dest='gallery_path', help="Use DIR as the Gallery path [default: CWD]", metavar='DIR', default=None) terom@72: parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", default=True) 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@72: if options.read_only : terom@72: config.read_only = True terom@72: 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@72: def load_command (options, args) : terom@72: """ terom@72: Figure out what command to run and with what args terom@72: """ terom@72: terom@72: # XXX: hardcoded terom@72: return commands.main, args terom@72: terom@48: def main (argv) : terom@48: """ terom@48: Main entry point terom@48: """ terom@48: terom@72: # load commands terom@72: 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@72: command_func, command_args = load_command(options, args) terom@48: terom@48: # run the selected command terom@72: ret = command_func(gallery, *command_args) 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: