Skip to content

Commit 6fdaad7

Browse files
krisctlprabhakk-mw
authored andcommitted
Introduces the notion of mandatory keys in the configuration of entrypoints.
1 parent 4dd0224 commit 6fdaad7

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

matlab_proxy/default_configuration.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
11
# Copyright 2020-2024 The MathWorks, Inc.
2+
from enum import Enum
3+
from typing import List
4+
25
import matlab_proxy
36

7+
8+
class ConfigKeys(Enum):
9+
"""Enumeration for configuration keys used in the MATLAB proxy setup."""
10+
11+
DOC_URL = "doc_url"
12+
EXT_NAME = "extension_name"
13+
EXT_NAME_DESC = "extension_name_short_description"
14+
SHOW_SHUTDOWN_BUTTON = "should_show_shutdown_button"
15+
16+
417
# Configure matlab_proxy
518
config = {
619
# Link the documentation url here. This will show up on the website UI
720
# where users can create issue's or make enhancement requests
8-
"doc_url": "https://github.com/mathworks/matlab-proxy/",
21+
ConfigKeys.DOC_URL.value: "https://github.com/mathworks/matlab-proxy/",
922
# Use a single word for extension_name
1023
# It will be used as a flag when launching the integration.
1124
# NOTE: This name must be used when setting the entrypoint for matlab_proxy in setup.py
1225
# Use '-' or '_' seperated values if more than 1 word is used.
1326
# Ex: Hello-World, Alice_Bob.
14-
"extension_name": matlab_proxy.get_default_config_name(),
27+
ConfigKeys.EXT_NAME.value: matlab_proxy.get_default_config_name(),
1528
# This value will be used in various places on the website UI.
1629
# Ensure that this is not more than 3 words.
17-
"extension_name_short_description": "MATLAB Desktop",
30+
ConfigKeys.EXT_NAME_DESC.value: "MATLAB Desktop",
1831
# Show the shutdown button in the UI for matlab-proxy
19-
"should_show_shutdown_button": True,
32+
ConfigKeys.SHOW_SHUTDOWN_BUTTON.value: True,
2033
}
34+
35+
36+
def get_required_config() -> List[str]:
37+
"""Get the list of required configuration keys.
38+
39+
This function returns a list of keys that are required for
40+
the MATLAB proxy configuration. These keys are used to
41+
ensure that the configuration dictionary contains all the
42+
necessary entries.
43+
44+
Returns:
45+
list: A list of strings representing the required
46+
configuration keys.
47+
"""
48+
required_keys: List[str] = [
49+
ConfigKeys.DOC_URL,
50+
ConfigKeys.EXT_NAME,
51+
ConfigKeys.EXT_NAME_DESC,
52+
]
53+
return [key.value for key in required_keys]

matlab_proxy/util/mwi/validators.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,31 @@ def validate_env_config(config):
179179
Returns:
180180
Dict: Containing data specific to the environment in which MATLAB proxy is being used in.
181181
"""
182-
available_configs = __get_configs()
182+
from matlab_proxy.default_configuration import get_required_config
183+
184+
available_configs: dict = __get_configs()
183185
config = config.lower()
184186

185187
# Check if supplied config is present in the available configs
186188
if config in available_configs:
187-
# Check if all keys are present in the supplied config
188-
default_config_keys = available_configs[
189-
matlab_proxy.get_default_config_name()
190-
].keys()
191189
env_config = available_configs[config]
190+
required_keys = get_required_config()
192191

193-
for key in default_config_keys:
194-
if not key in env_config:
195-
error_message = f"{key} missing in the provided {config} configuration"
196-
logger.error(error_message)
197-
raise FatalError(error_message)
192+
# Check if all required keys are present in the supplied config
193+
valid = all(key in env_config for key in required_keys)
194+
if not valid:
195+
error_message = (
196+
f"Required key/s missing in the provided {config} configuration"
197+
)
198+
logger.error(error_message)
199+
raise FatalError(error_message)
198200

199-
logger.debug(f"Successfully validated provided {config} configuration")
201+
logger.debug("Successfully validated provided %s configuration", config)
200202
return env_config
201203

202-
else:
203-
error_message = f"{config} is not a valid config. Available configs are : {list(available_configs.keys())}"
204-
logger.error(error_message)
205-
raise FatalError(error_message)
204+
error_message = f"{config} is not a valid config. Available configs are : {list(available_configs.keys())}"
205+
logger.error(error_message)
206+
raise FatalError(error_message)
206207

207208

208209
def __get_configs():

tests/unit/util/mwi/test_validators.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# Copyright 2020-2023 The MathWorks, Inc.
1+
# Copyright 2020-2024 The MathWorks, Inc.
22

3-
"""Tests for functions in matlab_proxy/util/mwi_validators.py
4-
"""
3+
"""Tests for functions in matlab_proxy/util/mwi_validators.py"""
54

65
import os
76
import random
@@ -185,8 +184,8 @@ def test_validate_env_config_true():
185184
def test_validate_env_config_false():
186185
"""Passing a non existent config should raise FatalError exception"""
187186

188-
with pytest.raises(FatalError) as e:
189-
config = validators.validate_env_config(str(random.randint(10, 100)))
187+
with pytest.raises(FatalError):
188+
validators.validate_env_config(str(random.randint(10, 100)))
190189

191190

192191
def test_get_configs():

0 commit comments

Comments
 (0)