misc. fixes
authorTero Marttila <terom@fixme.fi>
Fri, 05 Jun 2009 23:42:03 +0300
changeset 76 e22d9f699081
parent 75 18b3b1926720
child 77 2a53c5ade434
misc. fixes
degal/command.py
degal/commands/main.py
degal/filesystem.py
degal/folder.py
degal/html.py
degal/image.py
degal/main.py
--- a/degal/command.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/command.py	Fri Jun 05 23:42:03 2009 +0300
@@ -2,7 +2,7 @@
     Command implementations
 """
 
-import inspect, logging
+import inspect, logging, traceback
 
 class CommandList (object) :
     """
@@ -44,12 +44,12 @@
         self.func = func
         self.doc = doc
 
-    def invoke (self, config, gallery, args, options) :
+    def setup (self, config, gallery) :
         """
             Run the command with the given context
         """
         
-        return CommandContext(self, config, gallery)(*args, **options)
+        return CommandContext(self, config, gallery)
 
 class CommandContext (object) :
     """
@@ -101,16 +101,27 @@
         print msg
 
     def log_debug (self, msg, *args, **kwargs) :
-        log_msg(logging.DEBUG, msg, *args, **kwargs)
+        self.log_msg(logging.DEBUG, msg, *args, **kwargs)
 
     def log_info (self, msg, *args, **kwargs) :
-        log_msg(logging.INFO, msg, *args, **kwargs)
+        self.log_msg(logging.INFO, msg, *args, **kwargs)
     
     def log_warning (self, msg, *args, **kwargs) :
-        log_msg(logging.WARNING, msg, *args, **kwargs)
+        self.log_msg(logging.WARNING, msg, *args, **kwargs)
 
     def log_error (self, msg, *args, **kwargs) :
-        log_msg(logging.ERROR, msg, *args, **kwargs)
+        self.log_msg(logging.ERROR, msg, *args, **kwargs)
+    
+    def handle_error (self, exc_info=None) :
+        """
+            Do something to handle an error that occured
+        """
+        
+        if exc_info :
+            traceback.print_execption(*exc_info)
+
+        else :
+            traceback.print_exc()
 
 def command (func) :
     """
--- a/degal/commands/main.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/commands/main.py	Fri Jun 05 23:42:03 2009 +0300
@@ -1,4 +1,4 @@
-from degal.commands import command
+from degal.command import command
 from degal import templates
 
 def render_image (ctx, image) :
@@ -7,7 +7,7 @@
     """
     
     # log image path
-    ctx.log_debug("%s", image)
+    ctx.log_info("%s", image)
 
     # render output
     tpl = templates.master(ctx.gallery, image.title, image.html, 
@@ -23,7 +23,7 @@
     """
 
     # log folder path
-    ctx.log_debug("%s", folder)
+    ctx.log_info("%s", folder)
 
     # render folder index
     for page in xrange(folder.page_count) :
--- a/degal/filesystem.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/filesystem.py	Fri Jun 05 23:42:03 2009 +0300
@@ -70,8 +70,8 @@
             # config, either as given, or copy from parent
             if config :
                 self.config = config
-
-            else :
+            
+            elif parent : # XXX: else :
                 self.config = parent.config
      
             # fsname
@@ -206,8 +206,8 @@
                 raise
     
     # alias str/unicode
-    str = path
-    unicode = unicodepath
+    __str__ = path
+    __unicode__ = unicodepath
     
     def __repr__ (self) :
         """
@@ -545,7 +545,10 @@
         """
 
         # abuse Node's concept of a "name" a bit
-        super(Root, self).__init__(None, fspath, config=config)
+        super(Root, self).__init__(None, fspath)
+        
+        # store our config
+        self.config = config
 
     def nodepath (self) :
         """
--- a/degal/folder.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/folder.py	Fri Jun 05 23:42:03 2009 +0300
@@ -4,15 +4,17 @@
 
 import filesystem, image, html
 
-from utils import lazy_load
+from utils import lazy_load, lazy_load_iter
 
-class Folder (filesyste.Directory) :
+import math
+
+class Folder (filesystem.Directory) :
     """
         A Folder is a filesystem Directory that contains any number of other Folders and Images.
     """
 
-    def __init__ (self, node) :
-        super(Folder, self).__init__(node)
+    def __init__ (self, *args, **kwargs) :
+        super(Folder, self).__init__(*args, **kwargs)
 
         # info
         self.title = None
@@ -40,7 +42,7 @@
             Load and return an ordered list of child-Nodes
         """
 
-        return super(Folder, self).subnodes(skip_dotfiles=True, sorted=True)
+        return super(Folder, self).subnodes(skip_dotfiles=True, sort=True)
     
     @lazy_load_iter
     def subfolders (self) :
@@ -61,7 +63,7 @@
         for node in self.subnodes :
             # skip non-relevant ones
             # XXX: node should need to be a File
-            if not isinstance(node, filesystem.File) or not self.config.is_image(node) :
+            if not node.is_file() or not self.config.is_image(filesystem.File(node)) :
                 continue
             
             # create new
@@ -69,6 +71,7 @@
 
             # link up
             if prev :
+                img.prev = prev
                 prev.next = img
 
                 # yield the linked-up prev
@@ -87,7 +90,7 @@
             Returns the number of pages needed to show this folder's images
         """
 
-        return math.ceil(len(self.images) / float(self.config.images_per_page))
+        return int(math.ceil(len(self.images) / float(self.config.images_per_page)))
 
     def images_for_page (self, page) :
         """
--- a/degal/html.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/html.py	Fri Jun 05 23:42:03 2009 +0300
@@ -314,7 +314,7 @@
     
     def _check_encoding (self, encoding) :
         if encoding and encoding != self.xml_encoding :
-            raise ValueError("encoding mismatch: %r should be %r" % (encoding, self.xml_encoding)
+            raise ValueError("encoding mismatch: %r should be %r" % (encoding, self.xml_encoding))
 
     def render_str (self, encoding=None, **render_opts) :
         """
--- a/degal/image.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/image.py	Fri Jun 05 23:42:03 2009 +0300
@@ -5,6 +5,7 @@
 from __future__ import with_statement
 
 import filesystem, render, html
+from utils import lazy_load
 
 import PIL.Image
 from lib import EXIF
@@ -14,7 +15,7 @@
         An Image is a filesystem File that represents an image that can be thumbnailed, and handled.
     """
 
-    def __init__ (self, node, prev) :
+    def __init__ (self, *args, **kwargs) :
         """
             Initialize as an Image based on the given Node, linked with the given previous node
         """
@@ -22,7 +23,7 @@
         super(Image, self).__init__(node)
 
         # links
-        self.prev = prev
+        self.prev = None
         self.next = None
 
         # the .html file for this image
@@ -115,7 +116,7 @@
         render_machine = self.config.get_renderer()
 
         # render if needed
-        return render_machine.render_lazy(self
+        return render_machine.render_lazy(self,
             self.config.thumb_size, self.parent.load_thumb_dir.subnode(self.name)
         )
     
--- a/degal/main.py	Fri Jun 05 23:41:57 2009 +0300
+++ b/degal/main.py	Fri Jun 05 23:42:03 2009 +0300
@@ -16,7 +16,7 @@
     
     # define options
     parser.add_option('-G', "--gallery-path", dest='gallery_path', help="Use DIR as the Gallery path [default: CWD]", metavar='DIR', default=None)
-    parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", default=True)
+    parser.add_option('-R', "--read-only", dest='read_only', help="Do not attempt to modify the gallery", action="store_true", default=False)
     
     return parser
 
@@ -47,21 +47,36 @@
     # read path from config
     return gallery.Gallery(config.gallery_path, config)
 
-def load_command (options, args) :
+def load_command (config, args) :
     """
         Figure out what command to run and with what args
     """
     
     # XXX: hardcoded
-    return commands.main, args
+    return commands.main, args, {}
+
+def run_command (config, gallery, command, args, kwargs) :
+    """
+        Run the given command
+    """
+    
+    # setup the command execution context
+    command_ctx = command.setup(config, gallery)
+    
+    try :
+        # run it
+        return command_ctx(*args, **kwargs)
+    
+    except :
+        command_ctx.handle_error()
 
 def main (argv) :
     """
         Main entry point
     """
 
-    # load commands
-    commands = load_commands()
+    ## load commands
+    #commands = load_commands()
 
     # build optparser
     parser = option_parser(argv[0])
@@ -76,10 +91,10 @@
     gallery = load_gallery(config)
 
     # figure out what command to run
-    command_func, command_args = load_command(options, args)
-    
+    command, args, kwargs = load_command(config, args)
+
     # run the selected command
-    ret = command_func(gallery, *command_args)
+    ret = run_command(config, gallery, command, args, kwargs)
     
     if ret is None :
         # success