degal/main.py
changeset 76 e22d9f699081
parent 72 168a2d065f17
child 87 a7a18893730d
equal deleted inserted replaced
75:18b3b1926720 76:e22d9f699081
    14     # create parser using the given command
    14     # create parser using the given command
    15     parser = OptionParser(prog=command_name)
    15     parser = OptionParser(prog=command_name)
    16     
    16     
    17     # define options
    17     # define options
    18     parser.add_option('-G', "--gallery-path", dest='gallery_path', help="Use DIR as the Gallery path [default: CWD]", metavar='DIR', default=None)
    18     parser.add_option('-G', "--gallery-path", dest='gallery_path', help="Use DIR as the Gallery path [default: CWD]", metavar='DIR', default=None)
    19     parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", default=True)
    19     parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", action="store_true", default=False)
    20     
    20     
    21     return parser
    21     return parser
    22 
    22 
    23 def build_config (options) :
    23 def build_config (options) :
    24     """
    24     """
    45     """
    45     """
    46     
    46     
    47     # read path from config
    47     # read path from config
    48     return gallery.Gallery(config.gallery_path, config)
    48     return gallery.Gallery(config.gallery_path, config)
    49 
    49 
    50 def load_command (options, args) :
    50 def load_command (config, args) :
    51     """
    51     """
    52         Figure out what command to run and with what args
    52         Figure out what command to run and with what args
    53     """
    53     """
    54     
    54     
    55     # XXX: hardcoded
    55     # XXX: hardcoded
    56     return commands.main, args
    56     return commands.main, args, {}
       
    57 
       
    58 def run_command (config, gallery, command, args, kwargs) :
       
    59     """
       
    60         Run the given command
       
    61     """
       
    62     
       
    63     # setup the command execution context
       
    64     command_ctx = command.setup(config, gallery)
       
    65     
       
    66     try :
       
    67         # run it
       
    68         return command_ctx(*args, **kwargs)
       
    69     
       
    70     except :
       
    71         command_ctx.handle_error()
    57 
    72 
    58 def main (argv) :
    73 def main (argv) :
    59     """
    74     """
    60         Main entry point
    75         Main entry point
    61     """
    76     """
    62 
    77 
    63     # load commands
    78     ## load commands
    64     commands = load_commands()
    79     #commands = load_commands()
    65 
    80 
    66     # build optparser
    81     # build optparser
    67     parser = option_parser(argv[0])
    82     parser = option_parser(argv[0])
    68     
    83     
    69     # parse the given argv
    84     # parse the given argv
    74 
    89 
    75     # open gallery
    90     # open gallery
    76     gallery = load_gallery(config)
    91     gallery = load_gallery(config)
    77 
    92 
    78     # figure out what command to run
    93     # figure out what command to run
    79     command_func, command_args = load_command(options, args)
    94     command, args, kwargs = load_command(config, args)
    80     
    95 
    81     # run the selected command
    96     # run the selected command
    82     ret = command_func(gallery, *command_args)
    97     ret = run_command(config, gallery, command, args, kwargs)
    83     
    98     
    84     if ret is None :
    99     if ret is None :
    85         # success
   100         # success
    86         return 0
   101         return 0
    87 
   102