From ab75a63b173792a6b89215d6e2cf743649b05057 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sat, 7 Jan 2017 13:51:22 +0100 Subject: [PATCH 1/3] GHI-#4 Implement the "with_metaclass(meta, *bases)" method --- snowsaw/util/__init__.py | 3 +++ snowsaw/util/compat.py | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 snowsaw/util/__init__.py create mode 100644 snowsaw/util/compat.py 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', (), {}) From b85c0d3b7fb89716609edcd07c95f36495c68136 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sat, 7 Jan 2017 13:52:12 +0100 Subject: [PATCH 2/3] GHI-#4 Implement the "load(path)" method --- snowsaw/util/module.py | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 snowsaw/util/module.py 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() From 30241545d256eaba2eaa5195d706061b68aa93c0 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sat, 7 Jan 2017 13:52:55 +0100 Subject: [PATCH 3/3] =?UTF-8?q?GHI-#4=C2=B7Implement=C2=B7the=20"Singleton?= =?UTF-8?q?(type)"=C2=B7class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snowsaw/util/singleton.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 snowsaw/util/singleton.py 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]