diff --git a/snowsaw/util/__init__.py b/snowsaw/util/__init__.py new file mode 100644 index 0000000..2ada516 --- /dev/null +++ b/snowsaw/util/__init__.py @@ -0,0 +1,3 @@ +""" +Provides project utils. +""" \ No newline at end of file diff --git a/snowsaw/util/compat.py b/snowsaw/util/compat.py new file mode 100644 index 0000000..29a29e9 --- /dev/null +++ b/snowsaw/util/compat.py @@ -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', (), {}) diff --git a/snowsaw/util/module.py b/snowsaw/util/module.py new file mode 100644 index 0000000..a7768d1 --- /dev/null +++ b/snowsaw/util/module.py @@ -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() diff --git a/snowsaw/util/singleton.py b/snowsaw/util/singleton.py new file mode 100644 index 0000000..3776cb9 --- /dev/null +++ b/snowsaw/util/singleton.py @@ -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]