diff --git a/dynamic.py b/dynamic.py new file mode 100644 index 0000000..bbc7cd3 --- /dev/null +++ b/dynamic.py @@ -0,0 +1,27 @@ +import sys +import importlib +import configparser + +from implementation import * +from implementation import ibuddy + +# Read chosen implementation from config +try: + config = configparser.ConfigParser() + config.read('config.ini') + implementation = config.get('general', 'implementation') +except: + sys.exit('[error] Failed to read config file!') + +# Load implementation +implementations = [subclass.__name__ for subclass in ibuddy.__subclasses__()] +try: + if implementation in implementations: + ibuddy = getattr(globals()[implementation], implementation)() + else: + raise +except: + sys.exit('[error] Failed to load implementation!') + +# Fun! +ibuddy.test() \ No newline at end of file diff --git a/implementation/__init__.py b/implementation/__init__.py new file mode 100644 index 0000000..99aa988 --- /dev/null +++ b/implementation/__init__.py @@ -0,0 +1,13 @@ +import os +import glob +import abc + +modules = glob.glob(os.path.dirname(__file__) + '/*.py') +__all__ = [os.path.basename(f)[:-3] for f in modules] + +class ibuddy: + __metaclass__ = abc.ABCMeta + + @abc.abstractmethod + def test(self): + return \ No newline at end of file diff --git a/implementation/local.py b/implementation/local.py new file mode 100644 index 0000000..e18a690 --- /dev/null +++ b/implementation/local.py @@ -0,0 +1,5 @@ +from . import ibuddy + +class local(ibuddy): + def test(self): + print('test local') \ No newline at end of file diff --git a/implementation/remote.py b/implementation/remote.py new file mode 100644 index 0000000..5b2cef7 --- /dev/null +++ b/implementation/remote.py @@ -0,0 +1,5 @@ +from . import ibuddy + +class remote(ibuddy): + def test(self): + print('test remote') \ No newline at end of file