Skip to content

Commit

Permalink
config: add a Config class
Browse files Browse the repository at this point in the history
This class also parses the new tools and images sections (with relative
path support). Also rename the remaining .env files .yaml.

Signed-off-by: Jan Luebbe <[email protected]>
  • Loading branch information
jluebbe committed Jan 17, 2017
1 parent 82faf69 commit 7d06a9b
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 106 deletions.
11 changes: 0 additions & 11 deletions examples/barebox/local-usb.env

This file was deleted.

12 changes: 12 additions & 0 deletions examples/barebox/local-usb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
targets:
main:
resources:
USBSerialPort:
match:
ID_SERIAL_SHORT: 'P-00-01084'
drivers:
ManualPowerDriver:
name: "example"
SerialDriver: {}
BareboxDriver:
prompt: 'barebox@[^:]+:[^ ]+ '
10 changes: 0 additions & 10 deletions examples/barebox/local.env

This file was deleted.

11 changes: 11 additions & 0 deletions examples/barebox/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
targets:
main:
resources:
RawSerialPort:
port: "/dev/ttyUSB0"
drivers:
ManualPowerDriver:
name: "example"
SerialDriver: {}
BareboxDriver:
prompt: 'barebox@[^:]+:[^ ]+ '
13 changes: 7 additions & 6 deletions examples/infoprotocol/env.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
main:
resources:
RawSerialPort: { 'port': '/dev/ttyUSB0' }
drivers:
SerialDriver: { }
ShellDriver: { 'prompt': 'root@DistroKit:~ ', 'login_prompt': 'DistroKit login: ', 'username': 'root', 'keyfile': 'riot.pub'}
targets:
main:
resources:
RawSerialPort: { 'port': '/dev/ttyUSB0' }
drivers:
SerialDriver: { }
ShellDriver: { 'prompt': 'root@DistroKit:~ ', 'login_prompt': 'DistroKit login: ', 'username': 'root', 'keyfile': 'riot.pub'}
12 changes: 0 additions & 12 deletions examples/shell/local.env

This file was deleted.

13 changes: 13 additions & 0 deletions examples/shell/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
targets:
main:
resources:
RawSerialPort:
port: "/dev/ttyUSB0"
drivers:
ManualPowerDriver:
name: "example"
SerialDriver: {}
ShellDriver:
prompt: 'root@\w+:[^ ]+ '
login_prompt: ' login: '
username: 'root'
15 changes: 0 additions & 15 deletions examples/strategy/local.env

This file was deleted.

16 changes: 16 additions & 0 deletions examples/strategy/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
targets:
main:
resources:
RawSerialPort:
port: "/dev/ttyUSB0"
drivers:
ManualPowerDriver:
name: "example"
SerialDriver: {}
BareboxDriver:
prompt: 'barebox@[^:]+:[^ ]+ '
ShellDriver:
prompt: 'root@\w+:[^ ]+ '
login_prompt: ' login: '
username: 'root'
BareboxStrategy: {}
33 changes: 17 additions & 16 deletions examples/usbstick/env.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
main:
resources:
RawSerialPort:
port: '/dev/ttyUSB0'
NetworkService:
address: '192.168.24.75'
username: 'root'
drivers:
SerialDriver: {}
ShellDriver:
prompt: 'root@DistroKit:~ '
login_prompt: 'DistroKit login: '
username: 'root'
keyfile: 'riot.pub'
SSHDriver:
keyfile: 'riot'
targets:
main:
resources:
RawSerialPort:
port: '/dev/ttyUSB0'
NetworkService:
address: '192.168.24.75'
username: 'root'
drivers:
SerialDriver: {}
ShellDriver:
prompt: 'root@DistroKit:~ '
login_prompt: 'DistroKit login: '
username: 'root'
keyfile: 'riot.pub'
SSHDriver:
keyfile: 'riot'
1 change: 0 additions & 1 deletion labgrid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .target import Target
from .environment import Environment
from .exceptions import NoConfigFoundError
from .config import load_config

from .factory import target_factory
from .step import step, steps
Expand Down
63 changes: 47 additions & 16 deletions labgrid/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
from collections import OrderedDict

import yaml


def _dict_representer(dumper, data):
return dumper.represent_dict(data.items())
import attr
import os


def _dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))


yaml.add_representer(OrderedDict, _dict_representer)
yaml.add_constructor(
yaml.SafeLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor
)


def load_config(filename):
try:
with open(filename) as file:
return yaml.load(file)
except FileNotFoundError:
raise NoConfigFoundError(
"{} could not be found".format(self.config_file)
)
@attr.s
class Config:
filename = attr.ib(validator=attr.validators.instance_of(str))

def __attrs_post_init__(self):
self.base = os.path.dirname(self.filename)
try:
with open(self.filename) as file:
self.data = yaml.load(file, yaml.SafeLoader)
except FileNotFoundError:
raise NoConfigFoundError(
"{} could not be found".format(self.filename)
)

def resolve_path(self, path):
if os.path.isabs(path):
return path
else:
return os.path.join(self.base, path)

def get_tool(self, tool):
try:
path = str(self.data['tools'][tool])
return self.resolve_path(path)
except KeyError:
return None

def get_image_path(self, kind):
try:
path = str(self.data['images'][kind])
return self.resolve_path(path)
except KeyError:
logging.exception("no path configured for image {}".format(kind))
raise

def get_option(self, name):
try:
return str(self.data['options'][name])
except KeyError:
logging.exception("no such option {}".format(name))
raise

def get_targets(self):
return self.data.get('targets', {})
11 changes: 4 additions & 7 deletions labgrid/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .exceptions import NoConfigFoundError
from .target import Target
from .config import Config


@attr.s
Expand All @@ -14,23 +15,19 @@ class Environment:
interact = attr.ib(default=input)

def __attrs_post_init__(self):
from . import load_config
from . import target_factory

self.targets = {} #pylint: disable=attribute-defined-outside-init

try:
self.config = load_config(
self.config_file
) #pylint: disable=attribute-defined-outside-init
self.config = Config(self.config_file)
except:
raise NoConfigFoundError(
"{} is not a valid yaml file".format(self.config_file)
)

for name, config in self.config.items():
target = target_factory(name, config)
target.env = self
for name, config in self.config.get_targets().items():
target = target_factory(name, config, env=self)
self.targets[name] = target

def get_target(self, role: str='main') -> Target:
Expand Down
4 changes: 2 additions & 2 deletions labgrid/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def reg_driver(self, cls):
self.drivers[cls.__name__] = cls
return cls

def __call__(self, name, config):
def __call__(self, name, config, *, env=None):
from .target import Target

role = config.get('role', name)
target = Target(name)
target = Target(name, env=env)
for resource, args in config.get('resources', {}).items():
assert isinstance(args, dict)
r = self.resources[resource](target, **args)
Expand Down
22 changes: 12 additions & 10 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ def test_instance(self, tmpdir):
p = tmpdir.join("config.yaml")
p.write(
"""
test1:
drivers: {}
test2:
role: foo
resources: {}
targets:
test1:
drivers: {}
test2:
role: foo
resources: {}
"""
)
e = Environment(str(p))
Expand All @@ -26,11 +27,12 @@ def test_get_target(self, tmpdir):
p = tmpdir.join("config.yaml")
p.write(
"""
test1:
drivers: {}
test2:
role: foo
resources: {}
targets:
test1:
drivers: {}
test2:
role: foo
resources: {}
"""
)
e = Environment(str(p))
Expand Down

0 comments on commit 7d06a9b

Please sign in to comment.