plugin_setup_info.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » pylucid_project » system » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » pylucid_project » system » plugin_setup_info.py
# coding: utf-8

"""
    Stuff needed to expand settings.INSTALLED_APPS and settings.TEMPLATE_DIRS
    
    Important: We can't use any django things here. Because this module
    would be imported in settings.py
"""


from pylucid_project.utils.python_tools import has_init_file
import os
import warnings
import sys


class Plugin(object):
    def __init__(self, pkg_path, section, pkg_dir):
        self.pkg_path = pkg_path
        self.section = section
        self.pkg_dir = pkg_dir


class PyLucidPluginSetupInfo(dict):
    def __init__(self, plugin_package_list, verbose=True):
        super(PyLucidPluginSetupInfo, self).__init__()
        self.verbose = verbose

        # for expand: settings.TEMPLATE_DIRS
        self.template_dirs = []
        # for expand: settings.INSTALLED_APPS
        self.installed_plugins = []

        for base_path, section, pkg_dir in plugin_package_list:
            # e.g.: (PYLUCID_BASE_PATH, "pylucid_project", "pylucid_plugins")
            self.add(base_path, section, pkg_dir)

        self.template_dirs = tuple(self.template_dirs)
        self.installed_plugins = tuple(self.installed_plugins)

#        print " *** template dirs:\n", "\n".join(self.template_dirs)
#        print " *** installed plugins:\n", "\n".join(self.installed_plugins)

    def _isdir(self, path):
        if os.path.isdir(path):
            return True
        if self.verbose:
            warnings.warn("path %r doesn't exist." % path)
        return False

    def add(self, base_path, section, pkg_dir):
        """
        Add all plugins in one filesystem path/packages.
        e.g.: (PYLUCID_BASE_PATH, "pylucid_project", "pylucid_plugins")
        """
        if not self._isdir(base_path):
            return

        pkg_path = os.path.join(base_path, pkg_dir)
        if not self._isdir(pkg_path):
            return

        if not has_init_file(pkg_path):
            if self.verbose:
                warnings.warn("plugin path %r doesn't contain a __init__.py file, skip." % pkg_path)
            return

        if pkg_path not in sys.path: # settings imported more than one time!
#            print "append to sys.path: %r" % pkg_path
            sys.path.append(pkg_path)

        for plugin_name in os.listdir(pkg_path):
            if plugin_name.startswith(".") or plugin_name.startswith("_"): # e.g. svn dir or __init__.py file
                continue

            if plugin_name in self:
                warnings.warn("Plugin %r exist more than one time." % plugin_name)
                continue

            plugin_pkg = ".".join([section, pkg_dir, plugin_name])

            if not has_init_file(os.path.join(pkg_path, plugin_name)):
                if self.verbose:
                    warnings.warn("plugin '%s' doesn't contain a __init__.py file, skip." % plugin_pkg)
                continue

            if self.verbose:
                print "add plugin %r" % plugin_pkg

            self.installed_plugins.append(plugin_pkg)

            abs_template_path = os.path.join(pkg_path, plugin_name, "templates")
            if os.path.isdir(abs_template_path):
                self.template_dirs.append(abs_template_path)

            self[plugin_name] = (pkg_path, section, pkg_dir)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.