You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bloodhound.apache.org by ju...@apache.org on 2013/01/08 13:55:37 UTC

svn commit: r1430262 - in /incubator/bloodhound/branches/bep_0003_multiproduct: bloodhound_multiproduct/multiproduct/hooks.py trac/trac/hooks.py trac/trac/web/main.py

Author: jure
Date: Tue Jan  8 12:55:37 2013
New Revision: 1430262

URL: http://svn.apache.org/viewvc?rev=1430262&view=rev
Log:
Towards #322, #323, support for environment factory/global hooks added to trac

Added:
    incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
    incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py
Modified:
    incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py

Added: incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
URL: http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py?rev=1430262&view=auto
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py (added)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py Tue Jan  8 12:55:37 2013
@@ -0,0 +1,26 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+from trac.hooks import EnvironmentFactoryBase, GlobalHooksBase
+
+class MultiProductEnvironmentFactory(EnvironmentFactoryBase):
+    def open_environment(self, environ, env_path, use_cache=False):
+        return None
+
+class MultiProductGlobalHooks(GlobalHooksBase):
+    def install_hooks(self, environ, env_path):
+        return

Added: incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py
URL: http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py?rev=1430262&view=auto
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py (added)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py Tue Jan  8 12:55:37 2013
@@ -0,0 +1,68 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+import os
+import imp
+import inspect
+
+from config import Configuration
+
+__all__ = ['environment_factory', 'install_global_hooks']
+
+class EnvironmentFactoryBase(object):
+    def open_environment(self, environ, env_path, use_cache=False):
+        return None
+
+class GlobalHooksBase(object):
+    def install_hooks(self, environ, env_path):
+        return
+
+def _get_plugins_dir(env_path):
+    return os.path.normcase(os.path.realpath(os.path.join(env_path, 'plugins')))
+
+def _get_config(env_path):
+    return Configuration(os.path.join(env_path, 'conf', 'trac.ini'),
+                         {'envname': os.path.basename(env_path)})
+
+def _hook_load(env_path, hook_path):
+    hook_name = os.path.basename(hook_path[:-3])
+    plugins_dir = _get_plugins_dir(env_path)
+    load_path = os.path.join(plugins_dir, hook_path)
+    module = imp.load_source(hook_name, load_path)
+    return module
+
+def _get_hook_class(env_path, hook_path, class_type):
+    module = _hook_load(env_path, hook_path)
+    for (name, cls) in inspect.getmembers(module, inspect.isclass):
+        if issubclass(cls, class_type):
+            return cls
+    return None
+
+def environment_factory(environ, env_path):
+    config = _get_config(env_path)
+    hook_path = config.get('hooks', 'environment_factory', default=None)
+    return _get_hook_class(env_path, hook_path, EnvironmentFactoryBase) if hook_path else None
+
+def install_global_hooks(environ, env_path):
+    config = _get_config(env_path)
+    hook_paths = config.get('hooks', 'global_hooks', default=None)
+    if hook_paths:
+        for hook_path in hook_paths.split(','):
+            cls = _get_hook_class(env_path, hook_path, GlobalHooksBase)
+            if cls:
+                cls().install_hooks(environ, env_path)
+    return

Modified: incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py
URL: http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py?rev=1430262&r1=1430261&r2=1430262&view=diff
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py (original)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py Tue Jan  8 12:55:37 2013
@@ -435,7 +435,11 @@ def dispatch_request(environ, start_resp
 
     env = env_error = None
     try:
-        env = open_environment(env_path, use_cache=not run_once)
+        from trac.hooks import environment_factory, install_global_hooks
+        install_global_hooks(environ, env_path)
+        factory = environment_factory(environ, env_path)
+        env = factory().open_environment(environ, env_path, use_cache=not run_once) if factory \
+                else open_environment(env_path, use_cache=not run_once)
         if env.base_url_for_redirect:
             environ['trac.base_url'] = env.base_url