forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook_config.py
More file actions
95 lines (71 loc) · 2.72 KB
/
Copy pathhook_config.py
File metadata and controls
95 lines (71 loc) · 2.72 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
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Hook Package Config"""
import json
from pathlib import Path
from typing import Dict, NamedTuple, Optional, cast
import jsonschema
from samcli.lib.hook.exceptions import InvalidHookPackageConfigException
class HookFunctionality(NamedTuple):
"""
A class to represent a hook functionality (e.g. prepare)
"""
entry_method: Dict[str, str]
@property
def module(self) -> str:
return self.entry_method["module"]
@property
def method(self) -> str:
return self.entry_method["method"]
class HookPackageConfig:
"""
A class to represent a hook package. Upon instantiation, it also validate the config against a json schema.
"""
_package_dir: Path
_config: Dict
CONFIG_FILENAME = "Config.json"
JSON_SCHEMA_PATH = Path(__file__).parent / "hook_config_schema.json"
def __init__(self, package_dir: Path):
"""
Parameters
----------
package_dir: Path
The path of the hook package directory
"""
self._package_dir = package_dir
config_loc = package_dir / self.CONFIG_FILENAME
if not config_loc.is_file():
raise InvalidHookPackageConfigException(f"{config_loc} is not a file or does not exist")
with config_loc.open("r", encoding="utf-8") as f:
config_dict = json.load(f)
try:
jsonschema.validate(config_dict, self.jsonschema)
except jsonschema.ValidationError as e:
raise InvalidHookPackageConfigException(f"Invalid Config.json - {e}") from e
for func, func_dict in config_dict["functionalities"].items():
config_dict["functionalities"][func] = HookFunctionality(func_dict["entry_method"])
self._config = config_dict
@property
def jsonschema(self) -> Dict:
with HookPackageConfig.JSON_SCHEMA_PATH.open("r", encoding="utf-8") as f:
jsonschema_dict = json.load(f)
return cast(Dict, jsonschema_dict)
@property
def name(self) -> str:
return cast(str, self._config["hook_name"])
@property
def use_case(self) -> str:
return cast(str, self._config["hook_use_case"])
@property
def version(self) -> str:
return cast(str, self._config["version"])
@property
def specification(self) -> str:
return cast(str, self._config["hook_specification"])
@property
def description(self) -> Optional[str]:
return cast(str, self._config.get("description"))
@property
def functionalities(self) -> Dict[str, HookFunctionality]:
return cast(Dict[str, HookFunctionality], self._config["functionalities"])
@property
def iac_framework(self) -> str:
return cast(str, self._config.get("iac_framework", ""))