-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from AGrigis/ag-extplugins2
External plugins mechanism
- Loading branch information
Showing
50 changed files
with
139 additions
and
3,976 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################## | ||
# pySAP - Copyright (C) CEA, 2017 - 2019 | ||
# Distributed under the terms of the CeCILL-B license, as published by | ||
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to | ||
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html | ||
# for details. | ||
########################################################################## | ||
|
||
# System import | ||
import sys | ||
import re | ||
import os | ||
import imp | ||
|
||
|
||
class PluginsMetaImportHook(object): | ||
""" A class that import a module like normal except for the plugins that | ||
monted on the pysap plugins module. The return value from the hack call | ||
is put into sys.modules. | ||
""" | ||
def __init__(self): | ||
self.module = None | ||
|
||
def find_module(self, name, path=None): | ||
""" This method is called by Python if this class is on sys.path. | ||
'name' is the fully-qualified name of the module to look for, and | ||
'path' is either __path__ (for submodules and subpackages) or None (for | ||
a top-level module/package). | ||
Note that this method will be called every time an import statement | ||
is detected (or __import__ is called), before Python's built-in | ||
package/module-finding code kicks in. | ||
""" | ||
# Use this loader only on registered modules | ||
if re.match("pysap.plugins..*", name) is None: | ||
return None | ||
name = name.replace("pysap.plugins.", "") | ||
if (len(name.split(".")) == 1): | ||
path = None | ||
|
||
# Get parent module and associated sub module names | ||
self.sub_name = name.split(".")[-1] | ||
self.mod_name = name.rpartition(".")[0] | ||
|
||
# Find the sub module and build the module path | ||
try: | ||
self.file, self.filename, self.stuff = imp.find_module( | ||
self.sub_name, path) | ||
self.path = [self.filename] | ||
except ImportError: | ||
return None | ||
|
||
# Return The loader, here the object itself | ||
return self | ||
|
||
def load_module(self, name): | ||
""" This method is called by Python if PluginsMetaImportHook | ||
'find_module' does not return None. 'name' is the fully-qualified name | ||
of the module/package that was requested. | ||
""" | ||
# Load the module | ||
module = imp.load_module(name, self.file, self.filename, | ||
self.stuff) | ||
if self.file: | ||
self.file.close() | ||
|
||
# Update the module required information | ||
module.__path__ = self.path | ||
module.__loader__ = self | ||
module.__package__ = name | ||
module.__name__ = name | ||
if self.stuff[0] == ".py": | ||
module.__file__ = self.path[0] | ||
else: | ||
module.__file__ = os.path.join(self.path[0], "__init__.py") | ||
|
||
return module | ||
|
||
|
||
sys.meta_path.insert(0, PluginsMetaImportHook()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.