[z3-checkins] r38970 - in z3/deliverance/DeliveranceVHoster/trunk: docs dvhoster tests

ianb at codespeak.net ianb at codespeak.net
Thu Feb 15 22:40:11 CET 2007


Author: ianb
Date: Thu Feb 15 22:40:09 2007
New Revision: 38970

Added:
   z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py   (contents, props changed)
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/util.py   (contents, props changed)
   z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py   (contents, props changed)
Modified:
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
Log:
Added configurable initialization function

Added: z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py	Thu Feb 15 22:40:09 2007
@@ -0,0 +1,37 @@
+# the init_domain setting points to a file or module (a file like this
+# one).  It allows you to define a function that will be run on any
+# newly created domain.  Here you can put in default settings.  This
+# example is what we use for openplans.org
+
+import re
+
+rule_data = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<rules xmlns:xi="http://www.w3.org/2001/XInclude" xmlns="http://www.plone.org/deliverance" >
+  <xi:include href="standardrules.xml" />
+  <copy theme="//*[@id='theme-content']" content="//*[@id='portal-columns']" />
+</rules>
+"""
+
+
+def init_domain(domain_info, app_conf):
+    domain = domain_info.domain
+    match = re.search(r'^(.*)\.openplans\.org$', domain, re.I)
+    if not match:
+        # Don't try to set up domains we don't recognize
+        return
+    project = match.group(1)
+    remote_uri = (
+        '%s/VirtualHostBase/http/%s:80/openplans/projects/%s/VirtualHostRoot'
+        % (app_conf['zope_location'],
+           domain,
+           project))
+    domain_info.remote_uris = [
+        {'path': '',
+         'remote_uri': remote_uri}]
+    domain_info.theme_uri = app_conf['default_theme_uri']
+    domain_info.set_rule_file(
+        'rule.xml', rule_data)
+    
+    
+    

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py	Thu Feb 15 22:40:09 2007
@@ -181,8 +181,6 @@
     remote_uris = descriptors.json_converter(
         persist.file_property('remote_uris.txt'))
     theme_uri = persist.file_property('theme_uri.txt')
-    theme_id = persist.file_property('theme_id.txt')
-    rule_ids = persist.file_property('rule_ids.txt')
     redirects = descriptors.json_converter(
         persist.file_property('redirects.txt', default=()))
 

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	Thu Feb 15 22:40:09 2007
@@ -12,6 +12,7 @@
 from dvhoster.dataprovider import DataProvider, ProviderApp
 from dvhoster import current_environ
 from dvhoster.debuginterp import Renderer
+from dvhoster.util import load_func
 
 def norm_path(urlpath):
     if not urlpath:
@@ -24,6 +25,11 @@
 
     def __init__(self, app_conf):
         data_dir = app_conf['data_dir']
+        init_domain = app_conf.get('init_domain')
+        if init_domain:
+            init_domain = load_func(init_domain, 'init_domain')
+        self.init_domain = init_domain
+        self.app_conf = app_conf
         self.provider = DataProvider(data_dir)
         self.rewrite_links = asbool(app_conf.get('rewrite_links', True))
 
@@ -35,6 +41,11 @@
             domain = domain.split(':', 1)[0]
         domain = self.provider.normalize(domain)
         domain_info = self.provider.domain(domain)
+        print domain_info, domain_info.initialized
+        if not domain_info.initialized:
+            domain_info.initialize()
+            if self.init_domain:
+                self.init_domain(domain_info, self.app_conf)
         environ['dvhoster.domain_info'] = domain_info
         environ['dvhoster.base_url'] = construct_url(
             environ, with_query_string=False,

Added: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/util.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/util.py	Thu Feb 15 22:40:09 2007
@@ -0,0 +1,38 @@
+import os
+import types
+from paste.util.import_string import eval_import
+
+def load_func(description, default_name):
+    """
+    Load a function from the description.  The description can be a
+    Python module/identifier, or a filename (which must end in .py).
+    If a filename, then we look for default_name in the file as the
+    function to load.  The Python module/identifier may be a function
+    itself, but if it is a module then we will again look for the
+    function by name.
+    """
+    if description.endswith('.py'):
+        if not os.path.exists(description):
+            raise OSError(
+                "No file exists by the name %r" % description)
+        f = open(description, 'r')
+        c = f.read()
+        f.close()
+        ns = {'__file__': os.path.abspath(description)}
+        exec c in ns
+        if default_name not in ns:
+            raise NameError(
+                "The file %r must provide a function by the name %s"
+                % (description, default_name))
+        return ns[default_name]
+    else:
+        obj = eval_import(description)
+        if isinstance(obj, types.ModuleType):
+            value = getattr(obj, default_name, None)
+            if not value:
+                raise NameError(
+                    "The module %s must provide a function by the name %s"
+                    % (description, default_name))
+            return value
+        else:
+            return obj

Added: z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py
==============================================================================
--- (empty file)
+++ z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py	Thu Feb 15 22:40:09 2007
@@ -0,0 +1,32 @@
+import os
+import shutil
+import shutil
+from paste.fixture import TestApp
+from dvhoster.wsgiapp import make_app
+import httplib
+import simplejson
+
+example = os.path.join(os.path.dirname(os.path.dirname(__file__)),
+                       'docs', 'example_init_domain.py')
+
+data_filename = os.path.join(os.path.dirname(__file__), 'test-data')
+wsgi_app = make_app({},
+                    init_domain=example,
+                    zope_location='http://localhost:8080',
+                    default_theme_uri='http://yahoo.com',
+                    data_dir=data_filename)
+app = TestApp(wsgi_app)
+
+def test_init_domain():
+    foo_dir = os.path.join(
+        data_filename, 'foo.openplans.org')
+    if os.path.exists(foo_dir):
+        shutil.rmtree(foo_dir)
+    res = app.get('/.deliverance/remote_uris',
+                  extra_environ={'HTTP_HOST': 'foo.openplans.org'})
+    body = simplejson.loads(res.body)
+    expected = 'http://localhost:8080/VirtualHostBase/http/foo.openplans.org:80/openplans/projects/foo/VirtualHostRoot'
+    got = body[0]['remote_uri']
+    print got
+    print expected
+    assert got == expected


More information about the z3-checkins mailing list