Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create __repr__ for site_config classes #335

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions ocs/site_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def from_dict(cls, data):

"""
self = cls()
self.data = data
for k, v in data.get('hosts', {}).items():
assert (k not in self.hosts) # duplicate host name in config file!
self.hosts[k] = HostConfig.from_dict(v, parent=self, name=k)
Expand All @@ -62,6 +63,10 @@ def from_yaml(cls, filename):
self.source_file = filename
return self

def __repr__(self):
repr_ = f'SiteConfig.from_dict({self.data})'
return repr_


class HostConfig:
def __init__(self, name=None):
Expand Down Expand Up @@ -111,6 +116,11 @@ def from_dict(cls, data, parent=None, name=None):
self.log_dir = data.get('log-dir', None)
return self

def __repr__(self):
repr_ = f'HostConfig.from_dict({self.data}, ' \
f'name={self.name})'
return repr_


class CrossbarConfig:
@classmethod
Expand All @@ -134,6 +144,7 @@ def from_dict(cls, data, parent=None):
if data is None:
return None
self = cls()
self.data = data
self.parent = parent
self.binary = data.get('bin', shutil.which('crossbar'))
self.cbdir = data.get('config-dir')
Expand All @@ -158,6 +169,13 @@ def summary(self):
'config-dir': self.cbdir,
})

def __eq__(self, other):
return self.binary == other.binary and self.cbdir == other.cbdir

def __repr__(self):
repr_ = f'CrossbarConfig.from_dict({self.data})'
return repr_


class HubConfig:
@classmethod
Expand Down Expand Up @@ -199,6 +217,9 @@ def from_dict(cls, data, parent=None):
def summary(self):
return summarize_dict(self.data)

def __repr__(self):
return f"HubConfig.from_dict({self.data})"


class InstanceConfig:
def __init__(self):
Expand Down Expand Up @@ -248,6 +269,9 @@ def from_dict(cls, data, parent=None):
self.manage = "yes"
return self

def __repr__(self):
return f"InstanceConfig.from_dict({self.data})"


def summarize_dict(d):
output = '\n'.join([' %s: %s,' % (repr(k), repr(v))
Expand Down Expand Up @@ -306,6 +330,10 @@ def to_list(self):

return arg_list

def __repr__(self):
args = str(self.to_list())
return f'ArgContainer({args})'


def add_arguments(parser=None):
"""
Expand Down
65 changes: 64 additions & 1 deletion tests/test_site_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
from unittest.mock import MagicMock, patch

import pytest
from ocs.site_config import get_control_client, CrossbarConfig
import yaml

from ocs.site_config import (ArgContainer, CrossbarConfig, HostConfig,
HubConfig, InstanceConfig, SiteConfig,
get_control_client)

# Used for testing all Config objects
HUB_CFG = {'wamp_server': 'ws://127.0.0.1:18001/ws',
'wamp_http': 'http://127.0.0.1:18001/call',
'wamp_realm': 'test_realm',
'address_root': 'observatory'}
CROSSBAR_CFG = {'config-dir': '/simonsobs/ocs/dot_crossbar/',
'bin': '/path/to/crossbar'}
INSTANCE_CFG = {'agent-class': 'FakeDataAgent',
'instance-id': 'fake-data1',
'arguments': ['--mode', 'acq',
'--num-channels', '16',
'--sample-rate', '5',
'--frame-length', '10'],
'manage': None}
HOST_1_NAME = 'ocs-docker'
HOST_1_CFG = {'log-dir': '/simonsobs/log/ocs/',
'crossbar': CROSSBAR_CFG,
'agent-paths': ['/path/to/ocs/agents/'],
'agent-instances': [INSTANCE_CFG]}
SITE_CFG = {'hub': HUB_CFG,
'hosts': {HOST_1_NAME: HOST_1_CFG}}


def test_site_config_from_yaml(tmp_path):
cfg_file = tmp_path / "default.yaml"
with open(cfg_file, 'w') as f:
yaml.dump(SITE_CFG, f)
cfg = SiteConfig.from_yaml(cfg_file)
assert cfg.data == SITE_CFG


def test_crossbar_config_from_dict():
cfg = CrossbarConfig.from_dict(CROSSBAR_CFG)
assert cfg.data == CROSSBAR_CFG


def test_hub_config_from_dict():
cfg = HubConfig.from_dict(HUB_CFG)
assert cfg.data == HUB_CFG


def test_host_config_from_dict():
cfg = HostConfig.from_dict(HOST_1_CFG, name=HOST_1_NAME)
assert cfg.data == HOST_1_CFG
assert cfg.name == HOST_1_NAME
assert cfg.crossbar == CrossbarConfig.from_dict(CROSSBAR_CFG)


def test_instance_config_from_dict():
cfg = InstanceConfig.from_dict(INSTANCE_CFG)
assert cfg.data == INSTANCE_CFG


def test_arg_container_update():
arg1 = ArgContainer(['--arg1', 'foo', '--arg2', 'bar'])
arg2 = ArgContainer(['--arg2', 'baz'])
arg1.update(arg2)
assert arg1.to_list() == ['--arg1', 'foo', '--arg2', 'baz']


class TestGetControlClient:
Expand Down