forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
150 lines (121 loc) · 4.68 KB
/
Copy pathconfig.py
File metadata and controls
150 lines (121 loc) · 4.68 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
#
# 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:
"""
Creates or gets the Feast users active configuration
:return: dictionary of Feast properties
"""
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
:param prop: Feast property name
:param 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, cli_config: Dict[str, str] = None):
if (
isinstance(cli_config, dict)
and prop in cli_config
and cli_config[prop] is not None
):
return cli_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]:
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
:param prop: Feast property name
:param value: Feast property value
:return: 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
:param user_config_file_path: Local file system path to save configuration
:param 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):
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