-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/ghi-#4-utils' into develop
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Provides project utils. | ||
""" |
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,5 @@ | ||
def with_metaclass(meta, *bases): | ||
class metaclass(meta): | ||
def __new__(cls, name, this_bases, d): | ||
return meta(name, bases, d) | ||
return type.__new__(metaclass, 'metaclass', (), {}) |
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,48 @@ | ||
import os.path | ||
import sys | ||
|
||
loaded_modules = [] | ||
|
||
|
||
def load(path): | ||
""" | ||
Loads the module from the specified path. | ||
:param path: The path to load the module of | ||
:return: The loaded module | ||
""" | ||
basename = os.path.basename(path) | ||
module_name, extension = os.path.splitext(basename) | ||
plugin = load_module(module_name, path) | ||
loaded_modules.append(plugin) | ||
|
||
|
||
if sys.version_info >= (3, 5): | ||
import importlib.util | ||
|
||
|
||
def load_module(module_name, path): | ||
""" | ||
Loads the module with the specified name from the path. | ||
:param module_name: The name of the module to load | ||
:param path: The path to load the module of | ||
:return: The loaded module | ||
""" | ||
spec = importlib.util.spec_from_file_location(module_name, path) | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
return module | ||
elif sys.version_info >= (3, 3): | ||
from importlib.machinery import SourceFileLoader | ||
|
||
|
||
def load_module(module_name, path): | ||
""" | ||
Loads the module with the specified name from the path. | ||
:param module_name: The name of the module to load | ||
:param path: The path to load the module of | ||
:return: The loaded module | ||
""" | ||
return SourceFileLoader(module_name, path).load_module() |
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,7 @@ | ||
class Singleton(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) | ||
return cls._instances[cls] |