Skip to content

Commit

Permalink
Auto export controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jul 29, 2016
1 parent 1edd2c1 commit 43b5a14
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
13 changes: 8 additions & 5 deletions malcolm/controllers/__init__.py
@@ -1,7 +1,10 @@
# make the import path nice
from malcolm.controllers.clientcontroller import ClientController # noqa
from malcolm.controllers.countercontroller import CounterController # noqa
from malcolm.controllers.hellocontroller import HelloController # noqa
from malcolm.controllers.scanpointtickercontroller import \
ScanPointTickerController # noqa
from malcolm.util import import_child_packages

class_dict = import_child_packages("controllers")

globals().update(class_dict)
__all__ = list(class_dict)

del class_dict
del import_child_packages
3 changes: 2 additions & 1 deletion malcolm/controllers/clientcontroller.py
Expand Up @@ -3,10 +3,11 @@
from malcolm.core.controller import Controller
from malcolm.core.request import Post, Subscribe
from malcolm.core.response import Return
from malcolm.core.method import Method
from malcolm.core.method import Method, takes
from malcolm.core.serializable import Serializable


@takes()
class ClientController(Controller):
"""Sync a local block with a given remote block"""
REMOTE_BLOCKS_ID = 0
Expand Down
1 change: 1 addition & 0 deletions malcolm/controllers/countercontroller.py
Expand Up @@ -6,6 +6,7 @@
from malcolm.metas import NumberMeta


@takes()
class CounterController(Controller):

def create_attributes(self):
Expand Down
1 change: 1 addition & 0 deletions malcolm/controllers/hellocontroller.py
Expand Up @@ -3,6 +3,7 @@
from malcolm.core.method import takes, returns, REQUIRED


@takes()
class HelloController(Controller):
@takes(StringMeta("name", "a name"), REQUIRED)
@returns(StringMeta("greeting", "a greeting"), REQUIRED)
Expand Down
1 change: 1 addition & 0 deletions malcolm/controllers/scanpointtickercontroller.py
Expand Up @@ -9,6 +9,7 @@


@RunnableDeviceStateMachine.insert
@takes()
class ScanPointTickerController(Controller):

def create_attributes(self):
Expand Down
28 changes: 0 additions & 28 deletions malcolm/core/package.py

This file was deleted.

31 changes: 31 additions & 0 deletions malcolm/util.py
@@ -0,0 +1,31 @@
import os
import importlib
import logging


def import_child_packages(package_name):
"""Prepare a package namespace by importing all subclasses following PEP8
rules that have @takes decorated functions"""
class_dict = {}
# this is the path to the package
package_path = os.path.join(os.path.dirname(__file__), package_name)
for f in os.listdir(package_path):
if f.endswith(".py") and f != "__init__.py":
# import it and see what it produces
module_name = f[:-3]
module = importlib.import_module(
"malcolm.%s.%s" % (package_name, module_name))
for cls in find_decorated_classes(module):
class_dict[cls.__name__] = cls
return class_dict

def find_decorated_classes(module):
for n in dir(module):
cls = getattr(module, n)
if hasattr(cls, "Method"):
module_name = module.__name__.split(".")[-1]
if n.lower() != module_name:
logging.warning("Classname %s when lower cased should be %s" %
(n, module_name))
logging.debug("Found child class %s" % cls)
yield cls

0 comments on commit 43b5a14

Please sign in to comment.