Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4c8c7d9
WIP bus servo management
danic85 Jul 12, 2025
d18189c
Update config
danic85 Jul 12, 2025
60d524a
Added SC servo examples + compatibility
danic85 Jul 13, 2025
3e4dfc0
Configure initial positions
danic85 Jul 14, 2025
0be65a3
Allow other poses to be stored and accessed
danic85 Jul 14, 2025
91fac95
Added change_id script
danic85 Aug 3, 2025
69f2d7c
Calibrate servos on boot
danic85 Oct 4, 2025
9a57f8a
adjustments
danic85 Oct 4, 2025
0cf906e
Dynamic servo calibration + center on change_id
danic85 Oct 5, 2025
8d62b54
Controller support and WIP recording
danic85 Dec 13, 2025
f02d0db
Working with Pi
danic85 Dec 13, 2025
b052ed0
Adjust config
danic85 Dec 13, 2025
6841f63
Cleanup
danic85 Dec 14, 2025
c1ed384
Working input recorder
danic85 Dec 14, 2025
5e503e5
Support servo control (WIP - calibration needed)
danic85 Dec 14, 2025
008c369
Callibration + allow modules to be disabled on test environments
danic85 Dec 15, 2025
af0fdb2
Disable test
danic85 Dec 15, 2025
40040c8
Add documentation and tests
danic85 Dec 16, 2025
f645caa
Fix test
danic85 Dec 16, 2025
8a22781
Timeout controller connection after 30 seconds
danic85 Dec 16, 2025
d81c07c
Enable on robot
danic85 Dec 16, 2025
6a84d43
Fix for environment input to resolve pi issue
danic85 Dec 17, 2025
4803d58
Calibrate new leg position
danic85 Dec 17, 2025
dc4cb27
Improvements to gamepad controller modules. Temperature alert in disp…
danic85 Dec 18, 2025
d48ccd8
Fix tests
danic85 Dec 18, 2025
4a5a0ee
Fixes and improvements to loop and temperature management
danic85 Dec 21, 2025
5349956
Fix tests
danic85 Dec 21, 2025
cd4331b
Resolve PR comments
danic85 Dec 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add documentation and tests
  • Loading branch information
danic85 committed Dec 16, 2025
commit 40040c8269501c6ece2f6cb9ad8d2f17a7a65f8b
77 changes: 77 additions & 0 deletions docs/ModuleLoader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ModuleLoader Documentation

## Overview

The `ModuleLoader` class is responsible for dynamically loading and instantiating modules in the project based on YAML configuration files. It supports environment-based filtering, allowing selective loading of modules depending on the specified environment (e.g., 'robot', 'dev'). This approach enables modular, configurable, and environment-aware system composition.

## Key Features

1. **Dynamic Module Loading**: Loads Python modules and classes at runtime using paths specified in YAML config files.
2. **Environment Filtering**: Only loads modules enabled for the current environment, supporting both string and list-based environment fields.
3. **Configurable Instantiation**: Passes configuration parameters from YAML directly to module constructors via `**kwargs`.
4. **Messaging Service Integration**: Optionally injects a messaging service into loaded modules for inter-module communication.
5. **Multiple Instances**: Supports creating multiple instances of a module with different configurations.

## Configuration File Example

A typical module configuration file (e.g., `config/buzzer.yml`):

```yaml
buzzer:
enabled: true
path: "modules.audio.buzzer.Buzzer"
config:
pin: 27
name: 'buzzer'
environment: ['robot', 'dev'] # Optional: restricts loading to these environments
```

## Class Reference

### `ModuleLoader.__init__(config_folder='config', environment=None)`
- **config_folder**: Directory containing YAML config files for modules.
- **environment**: The current environment (string). Only modules matching this environment are loaded. Defaults to 'robot' if not specified.

### `load_yaml_files()`
- Scans the config folder for `.yml` files.
- Loads and parses each YAML file.
- For each module config:
- Skips if `enabled` is not `true`.
- If `environment` is specified, only includes the module if the current environment matches (supports string or list).
- Returns a list of enabled module configs for the current environment.

### `load_modules()`
- Iterates over the filtered module configs.
- Dynamically loads the specified Python class using the `path` field.
- Instantiates each module, passing config as `**kwargs`.
- Supports multiple instances per module if the `instances` field is present.
- Returns a dictionary of module instances, keyed by a combination of class and instance name.

### `set_messaging_service(module_instances, messaging_service)`
- Injects a messaging service into all loaded modules (except those named 'MessagingService').
- Sets the `messaging_service` attribute on each module instance.

## Environment Filtering Logic

- If a module config includes an `environment` field:
- If it is a string, the module is loaded only if it matches the current environment.
- If it is a list, the module is loaded if the current environment is in the list.
- If not present, the module is loaded for all environments.

## Example: Environment Filtering

Given the following config:

```yaml
example_module:
enabled: true
path: modules.example.ExampleModule
environment: 'dev'
```

### Usage Example

```bash
./startup.sh dev
./startup.sh # defaults to 'robot' environment
```
199 changes: 199 additions & 0 deletions tests/test_module_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import unittest
from unittest.mock import Mock, patch, MagicMock
Comment thread
danic85 marked this conversation as resolved.
Resolved
import sys
import os
from module_loader import ModuleLoader
Comment thread
danic85 marked this conversation as resolved.
Resolved

# Add parent directory to path to import module_loader
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))




class TestModuleLoader(unittest.TestCase):
"""Test cases for ModuleLoader class"""

def setUp(self):
self.patcher_os_listdir = patch('os.listdir')
self.patcher_open = patch('builtins.open', create=True)
self.patcher_yaml = patch('yaml.safe_load')
self.patcher_importlib_spec = patch('importlib.util.spec_from_file_location')
self.patcher_importlib_mod = patch('importlib.util.module_from_spec')
self.mock_listdir = self.patcher_os_listdir.start()
self.mock_open = self.patcher_open.start()
self.mock_yaml = self.patcher_yaml.start()
self.mock_spec = self.patcher_importlib_spec.start()
self.mock_mod = self.patcher_importlib_mod.start()

def tearDown(self):
patch.stopall()

def _setup_module_config(self, enabled=True, env=None, name='TestModule', path='modules.test.TestModule', config=None, instances=None):
module_config = {
'enabled': enabled,
'path': path,
'config': config or {'name': name},
}
if env is not None:
module_config['environment'] = env
if instances is not None:
module_config['instances'] = instances
return {'test_module': module_config}

def test_initialization_default_env(self):
# Should default to 'robot' environment
self.mock_listdir.return_value = []
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config')
self.assertEqual(loader.environment, 'robot')

def test_load_yaml_files_enabled_and_env_string(self):
# Only enabled modules with matching env are loaded
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
self.mock_yaml.return_value = self._setup_module_config(enabled=True, env='robot')
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
modules = loader.load_yaml_files()
self.assertEqual(len(modules), 1)
# Should skip if env does not match
self.mock_yaml.return_value = self._setup_module_config(enabled=True, env='dev')
loader = ModuleLoader(config_folder='config', environment='robot')
modules = loader.load_yaml_files()
self.assertEqual(len(modules), 0)

def test_load_yaml_files_env_list(self):
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
self.mock_yaml.return_value = self._setup_module_config(enabled=True, env=['robot', 'dev'])
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
modules = loader.load_yaml_files()
self.assertEqual(len(modules), 1)
loader = ModuleLoader(config_folder='config', environment='other')
modules = loader.load_yaml_files()
self.assertEqual(len(modules), 0)

def test_load_yaml_files_disabled(self):
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
self.mock_yaml.return_value = self._setup_module_config(enabled=False)
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
modules = loader.load_yaml_files()
self.assertEqual(len(modules), 0)

def test_load_yaml_files_yaml_error(self):
import yaml
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
self.mock_yaml.side_effect = yaml.YAMLError('YAML error')
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
# Should not raise, just print error
modules = loader.load_yaml_files()
self.assertEqual(modules, [])
self.mock_yaml.side_effect = None

def test_load_modules_success_and_instance_naming(self):
# Setup for dynamic loading
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
# One module, with two instances
instances = [
{'name': 'foo', 'param': 1},
{'name': 'bar', 'param': 2}
]
self.mock_yaml.return_value = self._setup_module_config(enabled=True, instances=instances)
# Mock importlib
mock_spec = MagicMock()
self.mock_spec.return_value = mock_spec
mock_mod = MagicMock()
self.mock_mod.return_value = mock_mod
# The class to instantiate
class Dummy:
def __init__(self, **kwargs):
self.kwargs = kwargs
setattr(mock_mod, 'TestModule', Dummy)
mock_spec.loader.exec_module.side_effect = lambda mod: mod.__setattr__('TestModule', Dummy)
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
loader.modules = loader.load_yaml_files()
# Patch module_from_spec to return our mock_mod
self.mock_mod.return_value = mock_mod
self.mock_mod.side_effect = lambda spec: mock_mod
# Actually test load_modules
with patch('importlib.util.module_from_spec', return_value=mock_mod):
instances_dict = loader.load_modules()
self.assertIn('TestModule_foo', instances_dict)
self.assertIn('TestModule_bar', instances_dict)
self.assertIsInstance(instances_dict['TestModule_foo'], Dummy)
self.assertEqual(instances_dict['TestModule_foo'].kwargs['name'], 'foo')

def test_load_modules_no_config(self):
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
# config is None
self.mock_yaml.return_value = self._setup_module_config(enabled=True, config=None)
mock_spec = MagicMock()
self.mock_spec.return_value = mock_spec
mock_mod = MagicMock()
class Dummy:
def __init__(self, **kwargs):
self.kwargs = kwargs
setattr(mock_mod, 'TestModule', Dummy)
mock_spec.loader.exec_module.side_effect = lambda mod: mod.__setattr__('TestModule', Dummy)
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
loader.modules = loader.load_yaml_files()
with patch('importlib.util.module_from_spec', return_value=mock_mod):
instances_dict = loader.load_modules()
# When config is None, instance_name becomes 'TestModule_TestModule' (module_name + '_' + None)
self.assertIn('TestModule_TestModule', instances_dict)
self.assertIsInstance(instances_dict['TestModule_TestModule'], Dummy)

def test_load_modules_import_error(self):
self.mock_listdir.return_value = ['mod1.yml']
mock_file = MagicMock()
self.mock_open.return_value.__enter__.return_value = mock_file
self.mock_yaml.return_value = self._setup_module_config(enabled=True)
mock_spec = MagicMock()
self.mock_spec.return_value = mock_spec
mock_mod = MagicMock()
setattr(mock_mod, 'TestModule', lambda **kwargs: None)
# Simulate import error
def raise_import(mod):
raise Exception('Import error')
mock_spec.loader.exec_module.side_effect = raise_import
from module_loader import ModuleLoader
loader = ModuleLoader(config_folder='config', environment='robot')
loader.modules = loader.load_yaml_files()
with patch('importlib.util.module_from_spec', return_value=mock_mod):
# Should not raise, just print error
instances_dict = loader.load_modules()
self.assertIsInstance(instances_dict, dict)

def test_set_messaging_service(self):
from module_loader import ModuleLoader
class Dummy:
def __init__(self):
self.messaging_service = None
modules = {
'TestModule': Dummy(),
'MessagingService': Dummy()
}
ms = object()
ModuleLoader().set_messaging_service(modules, ms)
self.assertIs(modules['TestModule'].messaging_service, ms)
# MessagingService should not be set
self.assertIsNot(modules['MessagingService'].messaging_service, ms)


if __name__ == '__main__':
unittest.main()
Loading