-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
129 lines (101 loc) · 3.35 KB
/
utils.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Utility functions
"""
import os
import sys
from typing import Tuple
import cerberus
import yaml
from loguru import logger
def load_text(path: str) -> str:
"""
Loads a text file from path and returns the string.
"""
try:
with open(path, "r", encoding="utf8") as stream:
return stream.read()
except FileNotFoundError:
logger.exception(f"'{path}' not found")
sys.exit(1)
def load_yaml(path: str, loader: yaml.Loader = yaml.SafeLoader) -> dict:
"""
Loads a yaml file from path and returns the data as a dict.
"""
try:
with open(path, "r", encoding="utf8") as stream:
return yaml.load(stream, Loader=loader)
except FileNotFoundError:
logger.exception(f"'{path}' not found")
sys.exit(1)
except yaml.YAMLError:
logger.exception(f"Error parsing '{path}'")
sys.exit(1)
def validate(
data: dict, schema: dict, validator: cerberus.Validator = cerberus.Validator()
) -> tuple[bool, dict]:
"""
Validates data against a schema.
"""
try:
success = validator.validate(data, schema)
except cerberus.schema.SchemaError:
logger.exception("Error parsing schema")
sys.exit(1)
return (success, validator.errors)
def load_yaml_and_validate(
path: str,
schema: dict,
loader: yaml.Loader = yaml.SafeLoader,
validator: cerberus.Validator = cerberus.Validator(),
) -> tuple[bool, dict, dict]:
"""
Loads a yaml file from path and validates it against a schema.
"""
data = load_yaml(path, loader)
success, errors = validate(data, schema, validator)
return (success, errors, data)
def load_yaml_and_validate_handle_errors(
path: str,
schema: dict,
loader: yaml.Loader = yaml.SafeLoader,
validator: cerberus.Validator = cerberus.Validator(),
) -> dict:
"""
Loads a yaml file from path and validates it against a schema while handling the errors.
"""
success, errors, data = load_yaml_and_validate(path, schema, loader, validator)
if not success:
logger.error(f"Error parsing '{path}': {errors}")
sys.exit(1)
return data
def default_file_from_path(path: str, default_file: str) -> str:
"""
Returns the default file from a path if the path does not already specify a file.
"""
if os.path.isdir(path):
path = os.path.join(path, default_file)
if not os.path.isfile(path):
logger.error(f"Template file {path} does not exist")
exit(1)
return path
def default_extension_from_path(path: str, default_ext: str) -> str:
"""
Returns the file with the default extension from a path if the path does not already specify an extension.
"""
if not os.path.isfile(path):
path = f"{path}{default_ext}"
if not os.path.isfile(path):
logger.error(f"Template file {path} does not exist")
exit(1)
return path
def get_type_occurences(dictionary: dict, search_type: type) -> list[Tuple[str, type]]:
"""
Recursively finds all instances of a given type in a dictionary
"""
ref_tags: list[Tuple[str, type]] = []
for name, value in dictionary.items():
if isinstance(value, dict):
ref_tags.extend(get_type_occurences(value, search_type))
if isinstance(value, search_type):
ref_tags.append((name, value))
return ref_tags