Skip to content

Commit

Permalink
Added Controller.create_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jun 21, 2016
1 parent a46cab8 commit 28f4189
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
11 changes: 5 additions & 6 deletions malcolm/controllers/countercontroller.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from malcolm.core.controller import Controller
from malcolm.core.attribute import Attribute
from malcolm.core.numbermeta import NumberMeta
from malcolm.core.mapmeta import MapMeta
from malcolm.core.method import Method, takes
from malcolm.core.method import takes

import numpy as np

class CounterController(Controller):

def __init__(self, block):
super(CounterController, self).__init__(block)
self.counter = Attribute("counter",
NumberMeta("counter", "A counter", np.int32))
def create_attributes(self):
self.counter = Attribute(NumberMeta("counter", "A counter", np.int32))
self.counter.set_put_function(self.counter.set_value)
self.counter.set_value(0)
yield self.counter

@takes()
def reset(self):
Expand Down
21 changes: 17 additions & 4 deletions malcolm/core/controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from malcolm.core.loggable import Loggable
import inspect

from malcolm.core.loggable import Loggable


class Controller(Loggable):
"""Implement the logic that takes a Block through its statemachine"""
Expand All @@ -13,15 +14,17 @@ def __init__(self, block):
logger_name = "%s.controller" % block.name
super(Controller, self).__init__(logger_name)
self.block = block
for attribute in self.create_attributes():
block.add_attribute(attribute)
for method in self.create_methods():
block.add_method(method)

def create_methods(self):
"""Abstract method that should provide Method instances for Block
Returns:
list: List or iterator of Method instances. Each one will be
attached to the Block by calling block.add_method(method)
Yields:
Method: Each one will be attached to the Block by calling
block.add_method(method)
"""

members = [value[1] for value in
Expand All @@ -31,3 +34,13 @@ def create_methods(self):
if hasattr(member, "Method"):
member.Method.set_function(member)
yield member.Method


def create_attributes(self):
"""Abstract method that should provide Attribute instances for Block
Yields:
Attribute: Each one will be attached to the Block by calling
block.add_attribute(attribute)
"""
return iter(())
8 changes: 8 additions & 0 deletions tests/test_controllers/test_countercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ def test_reset_calls_on_changed(self):
c.reset()
c.counter.on_changed.assert_called_once_with([["value"], 0])

def test_put_changes_value(self):
c = CounterController(Mock())
c.counter.parent = c.block
c.counter.put(32)
self.assertEqual(c.counter.value, 32)
c.block.on_changed.assert_called_once_with([["counter", "value"], 32])


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 28f4189

Please sign in to comment.