degal/main.py
changeset 72 168a2d065f17
parent 48 20355dd2e61a
child 76 e22d9f699081
equal deleted inserted replaced
71:98da2964c95c 72:168a2d065f17
     1 """
     1 """
     2     Main entry point for the command-line interface
     2     Main entry point for the command-line interface
     3 """
     3 """
     4 
     4 
     5 import commands
     5 import gallery, commands, config as config_module
     6 
     6 
     7 from optparse import OptionParser
     7 from optparse import OptionParser
     8 
     8 
     9 def option_parser (command_name) :
     9 def option_parser (command_name) :
    10     """
    10     """
    13     
    13     
    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('-d', "--dir", dest='dir', help="Use DIR as the image/HTML path [default: CWD]", metavar='DIR', default='.')
    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     
    20     
    20     return parser
    21     return parser
       
    22 
       
    23 def build_config (options) :
       
    24     """
       
    25         Build a configuration object with the given options
       
    26     """
       
    27     
       
    28     # build default config
       
    29     config = config_module.Configuration()
       
    30     
       
    31     # apply options
       
    32     if options.gallery_path :
       
    33         config.gallery_path = options.gallery_path
       
    34     
       
    35     if options.read_only :
       
    36         config.read_only = True
       
    37 
       
    38     # XXX: load config file(s)
       
    39 
       
    40     return config
       
    41 
       
    42 def load_gallery (config) :
       
    43     """
       
    44         Create the Gallery object that we are manipulating
       
    45     """
       
    46     
       
    47     # read path from config
       
    48     return gallery.Gallery(config.gallery_path, config)
       
    49 
       
    50 def load_command (options, args) :
       
    51     """
       
    52         Figure out what command to run and with what args
       
    53     """
       
    54     
       
    55     # XXX: hardcoded
       
    56     return commands.main, args
    21 
    57 
    22 def main (argv) :
    58 def main (argv) :
    23     """
    59     """
    24         Main entry point
    60         Main entry point
    25     """
    61     """
    26 
    62 
       
    63     # load commands
       
    64     commands = load_commands()
       
    65 
    27     # build optparser
    66     # build optparser
    28     parser = option_parser(argv[0])
    67     parser = option_parser(argv[0])
    29     
    68     
    30     # parse the given argv
    69     # parse the given argv
    31     options, filter_targets = parser.parse_args(argv[1:])
    70     options, args = parser.parse_args(argv[1:])
       
    71 
       
    72     # build our config
       
    73     config = build_config(options)
       
    74 
       
    75     # open gallery
       
    76     gallery = load_gallery(config)
       
    77 
       
    78     # figure out what command to run
       
    79     command_func, command_args = load_command(options, args)
    32     
    80     
    33     # run the selected command
    81     # run the selected command
    34     return commands.main(options.dir, filter_targets)
    82     ret = command_func(gallery, *command_args)
       
    83     
       
    84     if ret is None :
       
    85         # success
       
    86         return 0
    35 
    87 
       
    88     else :
       
    89         # exit with error code
       
    90         assert isinstance(ret, int)
       
    91         return ret
       
    92