From 01e0af6fcb20ebe553ed8af13d4f43fede28c127 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sat, 20 Aug 2016 11:56:48 +0100 Subject: [PATCH] add stubs for pluggable implementations --- dynamic.py | 27 +++++++++++++++++++++++++++ implementation/__init__.py | 13 +++++++++++++ implementation/local.py | 5 +++++ implementation/remote.py | 5 +++++ 4 files changed, 50 insertions(+) create mode 100644 dynamic.py create mode 100644 implementation/__init__.py create mode 100644 implementation/local.py create mode 100644 implementation/remote.py 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