Commits

Mike Bayer committed 676f00d

- Add support for coercion of string config values
to Python objects - ints, "false", "true", "None".
#4

Comments (0)

Files changed (4)

   with python-memcached backend.  Tests courtesy
   Justin Azoff #3
 
+- Add support for coercion of string config values
+  to Python objects - ints, "false", "true", "None".
+  #4
+
 0.2.2
 =====
 - add Redis backend, courtesy Ollie Rutherfurd

dogpile/cache/region.py

 from dogpile.core.nameregistry import NameRegistry
 
 from .util import function_key_generator, PluginLoader, \
-    memoized_property
+    memoized_property, coerce_string_conf
 from .api import NO_VALUE, CachedValue
 import time
 from functools import wraps
                                                 "cache.memcached.")
 
         """
+        config_dict = coerce_string_conf(config_dict)
         return self.configure(
             config_dict["%sbackend" % prefix],
             expiration_time = config_dict.get(

dogpile/cache/util.py

 from hashlib import sha1
 import inspect
 import sys
+import re
 
 try:
     import threading
 else:
     tounicode = unicode
 
+if py3k:
+    import configparser
+else:
+    import ConfigParser as configparser
+
+def coerce_string_conf(d):
+    result = {}
+    for k, v in d.items():
+        if not isinstance(v, basestring):
+            result[k] = v
+            continue
+
+        v = v.strip()
+        if re.match(r'^\d+$', v):
+            result[k] = int(v)
+        elif v.lower() in ('false', 'true'):
+            result[k] = v.lower() == 'true'
+        elif v == 'None':
+            result[k] = None
+        else:
+            result[k] = v
+    return result
+
 class PluginLoader(object):
     def __init__(self, group):
         self.group = group

tests/cache/test_region.py

 from unittest import TestCase
 from dogpile.cache.api import CacheBackend, CachedValue, NO_VALUE
-from dogpile.cache import make_region, register_backend, CacheRegion
+from dogpile.cache import make_region, register_backend, CacheRegion, util
 from . import eq_, assert_raises_message
 import time
 import itertools
         assert isinstance(my_region.backend, MockBackend) is True
         eq_(my_region.backend.arguments, {'url': '127.0.0.1'})
 
+    def test_instance_from_config_string(self):
+        my_conf = \
+            '[xyz]\n'\
+            'cache.example.backend=mock\n'\
+            'cache.example.expiration_time=600\n'\
+            'cache.example.arguments.url=127.0.0.1\n'\
+            'cache.example.arguments.dogpile_lockfile=false\n'\
+            'cache.example.arguments.xyz=None\n'
+
+        my_region = make_region()
+        import StringIO
+        config = util.configparser.ConfigParser()
+        config.readfp(StringIO.StringIO(my_conf))
+
+        my_region.configure_from_config(dict(config.items('xyz')), 'cache.example.')
+        eq_(my_region.expiration_time, 600)
+        assert isinstance(my_region.backend, MockBackend) is True
+        eq_(my_region.backend.arguments, {'url': '127.0.0.1', 
+                            'dogpile_lockfile':False, 'xyz':None})
+
     def test_key_mangler_argument(self):
         reg = self._region(init_args={"key_mangler":key_mangler})
         assert reg.key_mangler is key_mangler
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.