-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
82 lines (66 loc) · 2.16 KB
/
config.py
File metadata and controls
82 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by pat on 1/19/20
"""
Configuration objects.
.. currentmodule:: lambda_layer.config
.. moduleauthor:: Pat Daburu <[email protected]>
"""
from pathlib import Path
from typing import Any, Mapping, NamedTuple, Tuple, Union
import toml
class LayerConfig(NamedTuple):
"""A layer configuration."""
name: str #: the name of the layer
version: str #: the layer version
packages: Tuple[str] #: the installed packages
@classmethod
def load(cls, data: Mapping[str, Any]) -> 'LayerConfig':
"""
Load a layer configuration from a mapping of simple types.
:param data: the mapping
:return: the layer configuration object
"""
return LayerConfig(**{
**data,
'packages': tuple(data.get('packages', []))
})
class Config(NamedTuple):
"""A lambda layer configuration."""
environment: Mapping[str, str] #: environment settings
layers: Tuple[LayerConfig] #: the layers
@classmethod
def load(cls, data: Mapping[str, Any]) -> 'Config':
"""
Load a configuration from a mapping of simple types.
:param data: the mapping
:return: the configuration object
"""
return Config(**{
'environment': dict(data.get('environment', {})),
'layers': tuple([
LayerConfig.load(layer) for layer in data.get('layers', [])
])
})
@classmethod
def loadf(
cls,
path: Union[str, Path]
) -> 'Config':
"""
Load a configuration from a configuration file.
:param path: the file path
:return: the configuration object
"""
_path = (
path if isinstance(path, Path) else Path(path)
).expanduser().resolve()
# If the file doesn't exist, there isn't much more we can do here.
if not _path.exists():
raise FileNotFoundError(_path)
elif _path.is_dir():
raise IsADirectoryError(_path)
# Read the contents.
data = toml.loads(_path.read_text())
# Parse it out.
return cls.load(data)