Commits

Mike Orr committed 4924cea

Unfinished module config.py

Comments (0)

Files changed (4)

unfinished/config.py

+"""Helpers for configuration files."""
+
+class ConfigurationError(Exception):
+    pass
+
+def validate_config(config, validator, filename=None):
+    """Validate an application's configuration.
+
+    ``config`` 
+        A dict-like object containing configuration values.
+
+    ``validator``
+        A FormEncode `Schema``.  A ``FancyValidator`` is also acceptable if it
+        operates on a dict of values (not on a single value) and raises
+        ``Invalid`` with a dict of error messages (not a single error message).
+
+    ``filename``
+        The configuration file's path if known.  Paste users should pass
+        ``config.__file__`` here.
+
+    This helper depends on Ian Bicking's FormEncode package.
+    """
+    from formencode import Invalid
+    try:
+        return validator.to_python(config)
+    except Invalid, e:
+        if filename:
+            message = "configuration file '%s'" % filename
+        else:
+            message = "the application configuration"
+        message += " has the following errors: "
+        lines = [message]
+        for key, error in sorted(e.error_dict.iteritems()):
+            message = "    %s: %s" % (key, error)
+            lines.append(message)
+        message = "\n".join(lines)
+        raise ConfigurationError(message)
+        

unfinished/image_size.py

 a web browser do with it?  Should either return None or raise an exception.
 I've never seen an image without valid dimensions in it, so raising an
 exception may be fine.
+
+A dict dimensions cache is also useful.
 """
 
+To resize an image to a certain width and keep the height proportional:
+
+def choose_height(new_width, width, height):
+    """Return the height corresponding to 'new_width' that's proportional
+    to the original size.
+
+    This is useful when resizing an image to a certain width.
+
+    This function is by Mike Orr.
+    """
+    proportion = float(height) / float(width)
+    return int(new_width * proportion)
 #!/usr/bin/env python
 # (c) Copyright 2001-2005 Hewlett-Packard Development Company, L.P.
 #

webhelpers/containers.py

         except KeyError:
             pass
 
+def dict_of_dicts(dicts, key):
+    """Correlate several dicts under one superdict.
+
+    E.g., If you have several dicts each with a 'name' key, this will
+    create a superdict containing each dict keyed by name.
+    """
+    ret = {}
+    i = 0
+    for d in dicts:
+        try:
+            my_key = d[key]
+        except KeyError:
+            msg = "'dicts' element %d contains no key '%s'"
+            tup = i, key 
+            raise KeyError(msg % tup)
+        ret[my_key] = d
+        i += 1
+    return ret
+
+
+def dict_of_objects(objects, attr):
+    """Correlate several dict under one dict.
+
+    E.g., If you have several objects each with a 'name' attribute, this will
+    create a dict containing each object keyed by name.
+    """
+    ret = {}
+    i = 0
+    for obj in objects:
+        try:
+            my_key = getattr(obj, attr)
+        except AttrError:
+            msg = "'%s' object at 'objects[%d]' contains no attribute '%s'"
+            tup = type(obj).__name__, i, attr 
+            raise AttributeError(msg % tup)
+        ret[my_key] = obj
+        i += 1
+    return ret
+
+
 def distribute(lis, columns, horizontal=False, fill=None):
     """Distribute a list into a N-column table (list of lists).
 

webhelpers/misc.py

     except Exception:
         return None
 
+class DeclarativeException(Exception):
+    """A simpler way to define an exception with a fixed message.
+
+    Example:
+    class MyException(DeclarativeException):
+        message="can't frob the bar when foo is enabled"
+    """
+    message=""
+
+    def __init__(self, message=None):
+        Exception.__init__(self, message or self.message)