new structure for bin/degal, adding degal/commands, degal/main use-distutils
authorTero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 20:33:15 +0300
branchuse-distutils
changeset 48 20355dd2e61a
parent 47 189f331c7960
child 50 724121253d34
new structure for bin/degal, adding degal/commands, degal/main
bin/degal
degal/commands.py
degal/main.py
--- a/bin/degal	Wed Jun 03 19:40:35 2009 +0300
+++ b/bin/degal	Wed Jun 03 20:33:15 2009 +0300
@@ -4,42 +4,12 @@
 # Copyright 2008 Tero Marttila
 #
 
-from degal import folder, shorturl, log
-
-import os.path, os
-from optparse import OptionParser
-
-def main (dir='.', targets=()) :
-    root_filter = {}
-    
-    for target in targets :
-        f = root_filter
-        for path_part in target.split('/') :
-            if path_part :
-                if path_part not in f :
-                    f[path_part] = {}
-                    
-                f = f[path_part]
-    
-    log.title("Indexing %s...", dir)
-    root = folder.Folder(dir)
-    root.index(root_filter)
-    log.up()
-    
-    if False :
-        log.title("Syncing ShortURLs...")
-        shorturl.updateDB(root)
-        log.up()
-
-    log.title("Rendering updated dirs...")
-    root.render()
-    log.up()
+# import package
+import degal.main
 
 if __name__ == '__main__' :
-    parser = OptionParser(usage="usage: %prog [options] ... [target ...]")
+    import sys
     
-    parser.add_option("-d", "--dir", dest="dir", help="look for images in DIR and write the HTML there", metavar="DIR", default=".")
-    
-    options, filter_targets = parser.parse_args()
-    
-    main(options.dir, filter_targets)
+    # run entrypoint with command arguments, and exit with returned error code
+    sys.exit(degal.main.main(sys.argv))
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/commands.py	Wed Jun 03 20:33:15 2009 +0300
@@ -0,0 +1,42 @@
+"""
+    Implementations of the core CLI commands
+"""
+
+import log, shorturl, folder
+
+def main (dir='.', targets=()) :
+    """
+        Scan the given root dir for all images, and render updated ones
+    """
+
+    root_filter = {}
+    
+    # parse the targets into the hierarchial root_filter that we use
+    for target in targets :
+        f = root_filter
+        for path_part in target.split('/') :
+            if path_part :
+                if path_part not in f :
+                    f[path_part] = {}
+                    
+                f = f[path_part]
+    
+    # build the index
+    log.title("Indexing %s...", dir)
+    root = folder.Folder(dir)
+    root.index(root_filter)
+    log.up()
+    
+    # XXX: maintain shorturls stuff
+    if False :
+        log.title("Syncing ShortURLs...")
+        shorturl.updateDB(root)
+        log.up()
+
+    # render output
+    log.title("Rendering updated dirs...")
+    root.render()
+    log.up()
+
+    return 0
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/main.py	Wed Jun 03 20:33:15 2009 +0300
@@ -0,0 +1,35 @@
+"""
+    Main entry point for the command-line interface
+"""
+
+import commands
+
+from optparse import OptionParser
+
+def option_parser (command_name) :
+    """
+        Build the OptionParser that we use
+    """
+    
+    # create parser using the given command
+    parser = OptionParser(prog=command_name)
+    
+    # define options
+    parser.add_option('-d', "--dir", dest='dir', help="Use DIR as the image/HTML path [default: CWD]", metavar='DIR', default='.')
+    
+    return parser
+
+def main (argv) :
+    """
+        Main entry point
+    """
+
+    # build optparser
+    parser = option_parser(argv[0])
+    
+    # parse the given argv
+    options, filter_targets = parser.parse_args(argv[1:])
+    
+    # run the selected command
+    return commands.main(options.dir, filter_targets)
+