degal/main.py
changeset 144 97505a789003
parent 124 cac613118e75
equal deleted inserted replaced
143:a6e53a20fccb 144:97505a789003
     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 gallery, commands, config, version
     5 import gallery, command, commands, config, version
     6 
     6 
     7 from optparse import OptionParser
     7 from optparse import OptionParser
     8 import os.path
     8 import os.path
       
     9 
       
    10 def load_commands () :
       
    11     """
       
    12         Build the CommandList for us to use
       
    13     """
       
    14 
       
    15     return command.CommandList(commands.COMMANDS)
     9 
    16 
    10 def build_config () :
    17 def build_config () :
    11     """
    18     """
    12         Build the default configuration to use
    19         Build the default configuration to use
    13     """
    20     """
    14     
    21     
    15     return config.Configuration()
    22     return config.Configuration()
    16 
    23 
    17 def option_parser (exec_name) :
    24 def option_parser (exec_name, command) :
    18     """
    25     """
    19         Build the OptionParser that we use
    26         Build the OptionParser that we use, with the given command
    20     """
    27     """
    21     
    28     
    22     parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="Degal %s" % version.VERSION_STRING)
    29     parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="Degal %s" % version.VERSION_STRING)
    23     
    30     
       
    31     # core options
    24     parser.add_option('-C', "--config",         metavar='PATH', dest="_load_config_path",
    32     parser.add_option('-C', "--config",         metavar='PATH', dest="_load_config_path",
    25             help="Load configuration from PATH")
    33             help="Load configuration from PATH")
    26 
    34 
    27     parser.add_option('-H', "--gallery-path",   metavar='DIR',
    35     parser.add_option('-H', "--gallery-path",   metavar='DIR',
    28             help="Use DIR as the Gallery path instead of the CWD")
    36             help="Use DIR as the Gallery path instead of the CWD")
    29 
    37 
    30     parser.add_option('-F', "--force-update",   action="store_true",
       
    31             help="--force-thumb + --force-html")
       
    32 
       
    33     parser.add_option("--force-thumb",          action="store_true",
       
    34             help="Force-update thumbnails")
       
    35 
       
    36     parser.add_option("--force-html",           action="store_true",
       
    37             help="Force-update .html files")
       
    38     
       
    39     parser.add_option("--with-exif",            action="store_true",
    38     parser.add_option("--with-exif",            action="store_true",
    40             help="Include Exif metadata in updated .html files")
    39             help="Include Exif metadata in updated .html files")
    41     
    40     
    42     parser.add_option("--exif-handler",         metavar='NAME', dest="exif_handler_name",
    41     parser.add_option("--exif-handler",         metavar='NAME', dest="exif_handler_name",
    43             help="Use named Exif handler: pyexiv2, EXIFpy")
    42             help="Use named Exif handler: pyexiv2, EXIFpy")
    49             help="Show debug output")
    48             help="Show debug output")
    50 
    49 
    51     parser.add_option('-q', "--quiet",          action="store_const", dest="log_level", const=config.logging.WARN,
    50     parser.add_option('-q', "--quiet",          action="store_const", dest="log_level", const=config.logging.WARN,
    52             help="Reduced output (only warnings)")
    51             help="Reduced output (only warnings)")
    53     
    52     
       
    53     # command's options
       
    54     parser.add_option_group(command.option_group(parser))
       
    55 
    54     return parser
    56     return parser
    55 
    57 
    56 def parse_args (config, parser, args) :
    58 def parse_args (config, parser, args) :
    57     """
    59     """
    58         Parse command-line options/arguments.
    60         Parse command-line options/arguments.
    92     """
    94     """
    93     
    95     
    94     # read path from config
    96     # read path from config
    95     return gallery.Gallery(config.gallery_path, config)
    97     return gallery.Gallery(config.gallery_path, config)
    96 
    98 
    97 def load_command (config, args) :
       
    98     """
       
    99         Figure out what command to run and with what args
       
   100     """
       
   101     
       
   102     # XXX: hardcoded
       
   103     return commands.main, args, {}
       
   104 
       
   105 def run_command (config, gallery, command, args, kwargs) :
       
   106     """
       
   107         Run the given command
       
   108     """
       
   109     
       
   110     # setup the command execution context
       
   111     command_ctx = command.setup(config, gallery)
       
   112   
       
   113     # run with error handling
       
   114     return command_ctx.run()
       
   115 
       
   116 def main (argv) :
    99 def main (argv) :
   117     """
   100     """
   118         Main entry point
   101         Main entry point
   119     """
   102     """
   120 
   103 
   121     ## load commands
   104     ## load commands
   122     #commands = load_commands()
   105     commands = load_commands()
   123 
   106 
   124     # build our default config
   107     # build our default config
   125     config = build_config()
   108     config = build_config()
       
   109     
       
   110     
       
   111     # XXX: hardcoded
       
   112     command = commands.lookup('update')
   126 
   113 
   127     # build optparser
   114     # build optparser
   128     parser = option_parser(argv[0])
   115     parser = option_parser(argv[0], command)
   129 
   116 
   130     # parse the args into our config
   117     # parse the args into our config
   131     args = parse_args(config, parser, argv[1:])
   118     args = parse_args(config, parser, argv[1:])
   132 
   119 
   133     # postprocess
   120     # postprocess
   134     postprocess_config(config)
   121     postprocess_config(config)
   135 
   122 
   136     # open gallery
   123     # open gallery
   137     gallery = load_gallery(config)
   124     gallery = load_gallery(config)
   138 
   125 
   139     # figure out what command to run
       
   140     command, args, kwargs = load_command(config, args)
       
   141 
       
   142 
       
   143 
       
   144     # run the selected command
   126     # run the selected command
   145     ret = run_command(config, gallery, command, args, kwargs)
   127     # XXX: mix up configs with options
       
   128     ret = command.apply(config, gallery, config, *args).run()
   146 
   129 
   147 
   130 
   148     if ret is None :
   131     if ret is None :
   149         # success
   132         # success
   150         return 0
   133         return 0