Skip to content

Commit e58e658

Browse files
committed
riotctrl.ctrl: provide abstract factory and factory for BOARD env
1 parent 4f4facd commit e58e658

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

riotctrl/ctrl.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Define class to abstract a node over the RIOT build system.
44
"""
55

6+
import abc
67
import os
78
import time
89
import logging
@@ -188,3 +189,67 @@ def make_command(self, targets):
188189
command.extend(dir_cmd)
189190
command.extend(targets)
190191
return command
192+
193+
194+
class RIOTCtrlFactoryBase(abc.ABC):
195+
# pylint: disable=too-few-public-methods
196+
# A factory usually does not have more methods than one.
197+
"""Abstract factory to create different RIOTCtrl."""
198+
199+
@abc.abstractmethod
200+
def get_ctrl(self, application_directory='.', env=None):
201+
"""
202+
Returns a RIOTCtrl object of a class specified by the Factory
203+
204+
:param application_directory: `application_directory` initialization
205+
parameter for the RIOTCtrl object
206+
:param env: `env` initialization parameter for
207+
the RIOTCtrl object.
208+
"""
209+
raise NotImplementedError
210+
211+
212+
class RIOTCtrlBoardFactory(RIOTCtrlFactoryBase):
213+
# pylint: disable=too-few-public-methods
214+
# A factory usually does not have more methods than one.
215+
"""Factory mixin to create different RIOTCtrl types based on
216+
the BOARD environment variable.
217+
218+
:param board_cls: A dict that maps the `BOARD` environment variable to a
219+
RIOTCtrl class.
220+
"""
221+
DEFAULT_CLS = RIOTCtrl
222+
BOARD_CLS = {}
223+
224+
def __init__(self, board_cls=None):
225+
self.board_cls = {}
226+
self.board_cls.update(self.BOARD_CLS)
227+
if board_cls is not None:
228+
self.board_cls.update(board_cls)
229+
230+
def get_ctrl(self, application_directory='.', env=None):
231+
"""
232+
Returns a RIOTCtrl object of a class as specified in `board_cls` on
233+
initialization.
234+
235+
:param application_directory: `application_directory` initialization
236+
parameter for the RIOTCtrl object
237+
:param env: `env` initialization parameter for
238+
the RIOTCtrl object. This will also be
239+
used to determine the actual class of
240+
the return value.
241+
242+
When `BOARD` is set in the environment variables when `env` is provided
243+
in `env`, that value is used to look-up the RIOTCtrl class in the
244+
factory's `board_cls` for that specific `BOARD` value.
245+
"""
246+
the_env = {}
247+
the_env.update(os.environ)
248+
if env:
249+
the_env.update(env)
250+
if 'BOARD' not in the_env or the_env['BOARD'] not in self.board_cls:
251+
cls = self.DEFAULT_CLS
252+
else:
253+
cls = self.board_cls[the_env['BOARD']]
254+
# cls does its own fetching of `os.environ` so only provide `env` here
255+
return cls(application_directory=application_directory, env=env)

0 commit comments

Comments
 (0)