degal/main.py
changeset 118 60b126ff0b74
parent 117 a2e4562deaab
child 120 55cb7fc9c8fb
--- a/degal/main.py	Sun Jun 14 20:05:11 2009 +0300
+++ b/degal/main.py	Sun Jun 14 22:52:07 2009 +0300
@@ -2,82 +2,86 @@
     Main entry point for the command-line interface
 """
 
-import gallery, commands, config as config_module
+import gallery, commands, config
 
 from optparse import OptionParser
+import os.path
 
-def option_parser (command_name) :
+def build_config () :
+    """
+        Build the default configuration to use
+    """
+    
+    return config.Configuration()
+
+def option_parser (exec_name) :
     """
         Build the OptionParser that we use
     """
     
-    # create parser using the given command
-    parser = OptionParser(prog=command_name)
+    parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="???")
     
-    # define options
-    parser.add_option('-G', "--gallery-path",   metavar='DIR',  dest='gallery_path',    default=None,
-            help="Use DIR as the Gallery path [default: CWD]")
+    parser.add_option('-C', "--config",         metavar='PATH', dest="_load_config_path",
+            help="Load configuration from PATH")
 
-    parser.add_option('-F', "--force-update",   dest='force_update', action="store_true", default=False,
+    parser.add_option('-H', "--gallery-path",   metavar='DIR',
+            help="Use DIR as the Gallery path instead of the CWD")
+
+    parser.add_option('-F', "--force-update",   action="store_true",
             help="--force-thumb + --force-html")
 
-    parser.add_option("--force-thumb",          dest='force_thumb', action="store_true", default=False,
-            help="Force-update all thumbnails")
+    parser.add_option("--force-thumb",          action="store_true",
+            help="Force-update thumbnails")
 
-    parser.add_option("--force-html",           dest='force_html', action="store_true", default=False,
-            help="Force-update all .html files")
+    parser.add_option("--force-html",           action="store_true",
+            help="Force-update .html files")
     
-    parser.add_option("--with-exif",            dest='exif_enabled', action="store_true", default=None,
+    parser.add_option("--with-exif",            action="store_true",
             help="Include Exif metadata in updated .html files")
 
-    parser.add_option('-c', "--thread-count",   dest='thread_count', type="int", default=None,
-            help="Size of thread pool")
+    parser.add_option('-c', "--thread-count",   metavar='COUNT', type="int",
+            help="Use COUNT threads for concurrent tasks")
 
-    parser.add_option('-d', "--debug",          dest='debug', action="store_true", default=False,
+    parser.add_option('-d', "--debug",          action="store_const", dest="log_level", const=config.logging.DEBUG,
             help="Show debug output")
 
-    parser.add_option('-q', "--quiet",           dest='quiet', action="store_true", default=False,
-            help="Reduced output")
+    parser.add_option('-q', "--quiet",          action="store_const", dest="log_level", const=config.logging.WARN,
+            help="Reduced output (only warnings)")
     
     return parser
 
-def build_config (options) :
+def parse_args (config, parser, args) :
     """
-        Build a configuration object with the given options
+        Parse command-line options/arguments.
+
+        Returns the remaining positional arguments.
     """
     
-    # build default config
-    config = config_module.Configuration()
-    
-    # apply options
-    if options.gallery_path :
-        config.gallery_path = options.gallery_path
-    
-    if options.force_update :
-        config.force_html = True
-        config.force_thumb = True
-    
-    if options.force_thumb :
-        config.force_thumb = True
+    # parse the given arguments, storing output directly in the config
+    _, args = parser.parse_args(args=args, values=config)
 
-    if options.force_html :
-        config.force_html = True
-
-    if options.exif_enabled is not None :
-        config.exif_enabled = options.exif_enabled
-
-    if options.thread_count is not None :
-        config.thread_count = options.thread_count
+    # return the posargs
+    return args
 
-    if options.debug :
-        config.log_level = config_module.logging.DEBUG
+def postprocess_config (config) :
+    """
+        Post-process our Configuration after our command-line arguments have been parsed.
 
-    if options.quiet :
-        config.log_level = config_module.logging.WARN
+        This will attempt to load any additional configuration.
+    """
+    
+    # figure out what, if any, path to import
+    if hasattr(config, '_load_config_path') :
+        path = config._load_config_path
 
-    # XXX: load config file(s)
+    elif os.path.exists('degal.cfg') :
+        path = 'degal.cfg'
+    
+    else :
+        return
 
-    return config
+    # import it
+    config.import_file(path)
 
 def load_gallery (config) :
     """
@@ -114,14 +118,17 @@
     ## load commands
     #commands = load_commands()
 
+    # build our default config
+    config = build_config()
+
     # build optparser
     parser = option_parser(argv[0])
-    
-    # parse the given argv
-    options, args = parser.parse_args(argv[1:])
 
-    # build our config
-    config = build_config(options)
+    # parse the args into our config
+    args = parse_args(config, parser, argv[1:])
+
+    # postprocess
+    postprocess_config(config)
 
     # open gallery
     gallery = load_gallery(config)
@@ -129,9 +136,12 @@
     # figure out what command to run
     command, args, kwargs = load_command(config, args)
 
+
+
     # run the selected command
     ret = run_command(config, gallery, command, args, kwargs)
-    
+
+
     if ret is None :
         # success
         return 0