forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
165 lines (133 loc) · 5.11 KB
/
config.py
File metadata and controls
165 lines (133 loc) · 5.11 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Copyright 2019 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from os.path import expanduser, join
import logging
import os
import sys
from typing import Dict
from urllib.parse import urlparse
from urllib.parse import ParseResult
import toml
_logger = logging.getLogger(__name__)
feast_configuration_properties = {"core_url": "URL", "serving_url": "URL"}
CONFIGURATION_FILE_DIR = os.environ.get("FEAST_CONFIG", ".feast")
CONFIGURATION_FILE_NAME = "config.toml"
def _get_or_create_config() -> Dict:
"""Get user configuration file or create it and return"""
user_config_file_dir, user_config_file_path = _get_config_file_locations()
user_config_file_dir = user_config_file_dir.rstrip("/") + "/"
if not os.path.exists(os.path.dirname(user_config_file_dir)):
os.makedirs(os.path.dirname(user_config_file_dir))
if not os.path.isfile(user_config_file_path):
_save_config(user_config_file_path, _props_to_dict())
try:
return toml.load(user_config_file_path)
except FileNotFoundError:
_logger.error(
"Could not find Feast configuration file " + user_config_file_path
)
sys.exit(1)
except toml.decoder.TomlDecodeError:
_logger.error(
"Could not decode Feast configuration file " + user_config_file_path
)
sys.exit(1)
except Exception as e:
_logger.error(e)
sys.exit(1)
def set_property(prop: str, value: str):
"""
Sets a single property in the Feast users local configuration file
Args:
prop: Feast property name
value: Feast property value
"""
if _is_valid_property(prop, value):
active_feast_config = _get_or_create_config()
active_feast_config[prop] = value
_, user_config_file_path = _get_config_file_locations()
_save_config(user_config_file_path, active_feast_config)
print("Updated property [%s]" % prop)
else:
_logger.error("Invalid property selected")
sys.exit(1)
def get_config_property_or_fail(prop: str, force_config: Dict[str, str] = None) -> str:
"""
Gets a single property in the users configuration
Args:
prop: Property to retrieve
force_config: Configuration dictionary containing properties that should
be overridden. This will take precedence over file based properties.
Returns:
Returns a string property
"""
if (
isinstance(force_config, dict)
and prop in force_config
and force_config[prop] is not None
):
return force_config[prop]
active_feast_config = _get_or_create_config()
if _is_valid_property(prop, active_feast_config[prop]):
return active_feast_config[prop]
_logger.error("Could not load Feast property from configuration: %s" % prop)
sys.exit(1)
def _props_to_dict() -> Dict[str, str]:
"""Create empty dictionary of all Feast properties"""
prop_dict = {}
for prop in feast_configuration_properties:
prop_dict[prop] = ""
return prop_dict
def _is_valid_property(prop: str, value: str) -> bool:
"""
Validates both a Feast property as well as value
Args:
prop: Feast property name
value: Feast property value
Returns:
Returns True if property and value are valid
"""
if prop not in feast_configuration_properties:
_logger.error("You are trying to set an invalid property")
sys.exit(1)
prop_type = feast_configuration_properties[prop]
if prop_type == "URL":
if "//" not in value:
value = "%s%s" % ("grpc://", value)
parsed_value = urlparse(value) # type: ParseResult
if parsed_value.netloc:
return True
_logger.error("The property you are trying to set could not be identified")
sys.exit(1)
def _save_config(user_config_file_path: str, config_string: Dict[str, str]):
"""
Saves Feast configuration
Args:
user_config_file_path: Local file system path to save configuration
config_string: Contents in dictionary format to save to path
"""
try:
with open(user_config_file_path, "w+") as f:
toml.dump(config_string, f)
except Exception as e:
_logger.error("Could not update configuration file for Feast")
print(e)
sys.exit(1)
def _get_config_file_locations() -> (str, str):
"""Gets the local user configuration directory and file path"""
user_config_file_dir = join(expanduser("~"), CONFIGURATION_FILE_DIR)
user_config_file_path = join(user_config_file_dir, CONFIGURATION_FILE_NAME)
return user_config_file_dir, user_config_file_path