comparison MoinMoin/config/multiconfig.py @ 3831:4a3c996f2d5f

plugins: allow multiple plugin paths
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 11 Jul 2008 16:19:34 +0200
parents 262738c1f46b
children 682fd34b9422
comparison
equal deleted inserted replaced
3830:3af779616368 3831:4a3c996f2d5f
524 also the spelling of the directory name. 524 also the spelling of the directory name.
525 """ % {'attr': attr, 'path': path, } 525 """ % {'attr': attr, 'path': path, }
526 raise error.ConfigurationError(msg) 526 raise error.ConfigurationError(msg)
527 527
528 def _loadPluginModule(self): 528 def _loadPluginModule(self):
529 """ import plugin module under configname.plugin 529 """
530 import all plugin modules
530 531
531 To be able to import plugin from arbitrary path, we have to load 532 To be able to import plugin from arbitrary path, we have to load
532 the base package once using imp.load_module. Later, we can use 533 the base package once using imp.load_module. Later, we can use
533 standard __import__ call to load plugins in this package. 534 standard __import__ call to load plugins in this package.
534 535
535 Since each wiki has unique plugins, we load the plugin package 536 Since each configured plugin path has unique plugins, we load the
536 under the wiki configuration module, named self.siteid. 537 plugin packages as "moin_plugin_<sha1(path)>.plugin".
537 """ 538 """
538 import imp 539 import imp, sha
539 540
540 name = self.siteid + '.plugin' 541 plugin_dirs = [self.plugin_dir] + self.plugin_dirs
542 self._plugin_modules = []
543
541 try: 544 try:
542 # Lock other threads while we check and import 545 # Lock other threads while we check and import
543 imp.acquire_lock() 546 imp.acquire_lock()
544 try: 547 try:
545 # If the module is not loaded, try to load it 548 for pdir in plugin_dirs:
546 if not name in sys.modules: 549 csum = 'p_%s' % sha.new(pdir).hexdigest()
547 # Find module on disk and try to load - slow! 550 modname = '%s.%s' % (self.siteid, csum)
548 plugin_parent_dir = os.path.abspath(os.path.join(self.plugin_dir, '..')) 551 # If the module is not loaded, try to load it
549 fp, path, info = imp.find_module('plugin', [plugin_parent_dir]) 552 if not modname in sys.modules:
550 try: 553 # Find module on disk and try to load - slow!
551 # Load the module and set in sys.modules 554 abspath = os.path.abspath(pdir)
552 module = imp.load_module(name, fp, path, info) 555 parent_dir, pname = os.path.split(abspath)
553 sys.modules[self.siteid].plugin = module 556 fp, path, info = imp.find_module(pname, [parent_dir])
554 finally: 557 try:
555 # Make sure fp is closed properly 558 # Load the module and set in sys.modules
556 if fp: 559 module = imp.load_module(modname, fp, path, info)
557 fp.close() 560 setattr(sys.modules[self.siteid], 'csum', module)
561 self._plugin_modules.append(modname)
562 finally:
563 # Make sure fp is closed properly
564 if fp:
565 fp.close()
558 finally: 566 finally:
559 imp.release_lock() 567 imp.release_lock()
560 except ImportError, err: 568 except ImportError, err:
561 msg = """ 569 msg = """
562 Could not import plugin package "%(path)s/plugin" because of ImportError: 570 Could not import plugin package "%(path)s/plugin" because of ImportError:
842 'paths': ('Paths', None, ( 850 'paths': ('Paths', None, (
843 ('data_dir', './data/', "Path to the data directory containing your (locally made) wiki pages."), 851 ('data_dir', './data/', "Path to the data directory containing your (locally made) wiki pages."),
844 ('data_underlay_dir', './underlay/', "Path to the underlay directory containing distribution system and help pages."), 852 ('data_underlay_dir', './underlay/', "Path to the underlay directory containing distribution system and help pages."),
845 ('cache_dir', None, "Directory for caching, by default computed from `data_dir`/cache."), 853 ('cache_dir', None, "Directory for caching, by default computed from `data_dir`/cache."),
846 ('user_dir', None, "Directory for user storage, by default computed to be `data_dir`/user."), 854 ('user_dir', None, "Directory for user storage, by default computed to be `data_dir`/user."),
847 ('plugin_dir', None, "Plugin directory, by default computed to be `data_dir`/user."), 855 ('plugin_dir', None, "Plugin directory, by default computed to be `data_dir`/plugin."),
856 ('plugin_dirs', [], "Additional plugin directories."),
848 857
849 ('docbook_html_dir', r"/usr/share/xml/docbook/stylesheet/nwalsh/html/", 858 ('docbook_html_dir', r"/usr/share/xml/docbook/stylesheet/nwalsh/html/",
850 'Path to the directory with the Docbook to HTML XSLT files (optional, used by the docbook parser). The default value is correct for Debian Etch.'), 859 'Path to the directory with the Docbook to HTML XSLT files (optional, used by the docbook parser). The default value is correct for Debian Etch.'),
851 ('shared_intermap', None, 860 ('shared_intermap', None,
852 "Path to a file containing global InterWiki definitions (or a list of such filenames)"), 861 "Path to a file containing global InterWiki definitions (or a list of such filenames)"),