add stubs for pluggable implementations
This commit is contained in:
27
dynamic.py
Normal file
27
dynamic.py
Normal file
@@ -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()
|
||||
13
implementation/__init__.py
Normal file
13
implementation/__init__.py
Normal file
@@ -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
|
||||
5
implementation/local.py
Normal file
5
implementation/local.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from . import ibuddy
|
||||
|
||||
class local(ibuddy):
|
||||
def test(self):
|
||||
print('test local')
|
||||
5
implementation/remote.py
Normal file
5
implementation/remote.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from . import ibuddy
|
||||
|
||||
class remote(ibuddy):
|
||||
def test(self):
|
||||
print('test remote')
|
||||
Reference in New Issue
Block a user