Skip to content

Commit

Permalink
Merge branch 'feature/ghi-#4-utils' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
arcticicestudio committed Jan 7, 2017
2 parents c56a719 + 3024154 commit 695f1fd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions snowsaw/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Provides project utils.
"""
5 changes: 5 additions & 0 deletions snowsaw/util/compat.py
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', (), {})
48 changes: 48 additions & 0 deletions snowsaw/util/module.py
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()
7 changes: 7 additions & 0 deletions snowsaw/util/singleton.py
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]

0 comments on commit 695f1fd

Please sign in to comment.