changeset 88:a3a7ac9102dc

better choosing of types
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 17 Nov 2011 15:18:13 -0800
parents 67dd8e0aa6da
children 7f7f7313b4c4
files bitsyblog/bitsyblog.py
diffstat 1 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py
+++ b/bitsyblog/bitsyblog.py
@@ -45,43 +45,54 @@ class BitsyBlog(object):
     ### class level variables
     defaults = { 'date_format': '%H:%M %F', # default date format
                  'file_dir': os.path.dirname(__file__), # where the blog is kept
                  'subject': '[ %(date)s ]:', # default subject
                  'n_links': 5, # number of links for navigation
                  'site_name': 'bitsyblog', # name of the site (needed?)
                  'header': None, # text to insert as first child of body'
                  'template_directories': '', # space separated template_directories
-                 'auto_reload': 'True', # reload the genshi templates
+                 'auto_reload': True, # reload the genshi templates
                  'help_file': None, # help to display
                  'feed_items': 10, # number of RSS/atom items to display
                  'post_handlers': '' # post handlers
                  }
 
     cooked_bodies = {}
 
     def __init__(self, kw, handler_args):
-        for key in self.defaults:
-            setattr(self, key, kw.get(key, self.defaults[key]))
+
+        # set values from defaults and kw
+        for key, value in self.defaults.items():
+            kw_value = kw.get(key, value)
+
+            # convert kw_value to the proper type
+            _type = type(value)
+            if isinstance(kw_value, basestring) and not issubclass(_type, basestring) and value is not None:
+                if _type == bool:
+                    kw_value = kw_value.lower() == 'true'
+                else:
+                    kw_value = _type(kw_value)
+            setattr(self, key, kw_value)
+
         self.n_links = int(self.n_links) # could be a string from the .ini
         self.response_functions = {'GET': self.get,
                                    'POST': self.post,
                                    'PUT': self.put
                                    }
 
         # abstract attributes
         from user import FilespaceUsers
 
         self.users = FilespaceUsers(self.file_dir)
         self.blog = FileBlog(self.file_dir)
         self.cooker = self.restructuredText
         self.feed_items = int(self.feed_items)
 
         # template renderer
-        self.auto_reload = self.auto_reload.lower()=='true'
         self.template_directories = self.template_directories.split() # no spaces in directory names, for now
 
         for directory in self.template_directories:
             assert os.path.isdir(directory), "Bitsyblog template directory %s does not exist!" % directory
 
         self.template_directories.append(resource_filename(__name__, 'templates'))
         self.loader = TemplateLoader(self.template_directories,
                                      auto_reload=self.auto_reload)