19
19
# under the License.
20
20
21
21
''' config.py '''
22
+ import string
22
23
23
24
from heron .statemgrs .src .python .config import Config as StateMgrConfig
24
25
@@ -33,6 +34,7 @@ class Config:
33
34
Responsible for reading the yaml config file and
34
35
exposing various tracker configs.
35
36
"""
37
+ FORMATTER_PARAMETERS = {"CLUSTER" , "ENVIRON" , "TOPOLOGY" , "ROLE" , "USER" }
36
38
37
39
def __init__ (self , configs ):
38
40
self .configs = configs
@@ -48,65 +50,51 @@ def load_configs(self):
48
50
for extra_link in self .configs [EXTRA_LINKS_KEY ]:
49
51
self .extra_links .append (self .validate_extra_link (extra_link ))
50
52
51
- def validate_extra_link (self , extra_link ) :
53
+ def validate_extra_link (self , extra_link : dict ) -> None :
52
54
"""validate extra link"""
53
55
if EXTRA_LINK_NAME_KEY not in extra_link or EXTRA_LINK_FORMATTER_KEY not in extra_link :
54
56
raise Exception ("Invalid extra.links format. " +
55
57
"Extra link must include a 'name' and 'formatter' field" )
56
58
57
59
self .validated_formatter (extra_link [EXTRA_LINK_FORMATTER_KEY ])
58
- return extra_link
59
-
60
- # pylint: disable=no-self-use
61
- def validated_formatter (self , url_format ):
62
- """validate visualization url format"""
63
- # We try to create a string by substituting all known
64
- # parameters. If an unknown parameter is present, an error
65
- # will be thrown
66
- valid_parameters = {
67
- "${CLUSTER}" : "cluster" ,
68
- "${ENVIRON}" : "environ" ,
69
- "${TOPOLOGY}" : "topology" ,
70
- "${ROLE}" : "role" ,
71
- "${USER}" : "user" ,
72
- }
73
- dummy_formatted_url = url_format
74
- for key , value in list (valid_parameters .items ()):
75
- dummy_formatted_url = dummy_formatted_url .replace (key , value )
76
60
77
- # All $ signs must have been replaced
78
- if '$' in dummy_formatted_url :
79
- raise Exception ("Invalid viz.url.format: %s" % (url_format ))
61
+ def validated_formatter (self , url_format : str ) -> None :
62
+ """Check visualization url format has no unrecongnised parameters."""
63
+ # collect the parameters which would be interpolated
64
+ formatter_variables = set ()
65
+ class ValidationHelper :
66
+ def __getitem__ (self , key ):
67
+ formatter_variables .add (key )
68
+ return ""
69
+
70
+ string .Template (url_format ).safe_substitute (ValidationHelper ())
80
71
81
- # No error is thrown, so the format is valid.
82
- return url_format
72
+ if not formatter_variables <= self . FORMATTER_PARAMETERS :
73
+ raise Exception ( f"Invalid viz.url.format: { url_format !r } " )
83
74
84
- def get_formatted_url (self , formatter , execution_state ):
75
+ @staticmethod
76
+ def get_formatted_url (formatter : str , execution_state : dict ) -> str :
85
77
"""
86
- @param formatter: The template string to interpolate
87
- @param execution_state: The python dict representing JSON execution_state
88
- @return Formatted viz url
78
+ Format a url string using values from the execution state.
79
+
89
80
"""
90
81
91
- # Create the parameters based on execution state
92
- common_parameters = {
93
- "${CLUSTER}" : execution_state .get ("cluster" , "${CLUSTER}" ),
94
- "${ENVIRON}" : execution_state .get ("environ" , "${ENVIRON}" ),
95
- "${TOPOLOGY}" : execution_state .get ("jobname" , "${TOPOLOGY}" ),
96
- "${ROLE}" : execution_state .get ("role" , "${ROLE}" ),
97
- "${USER}" : execution_state .get ("submission_user" , "${USER}" ),
82
+ subs = {
83
+ var : execution_state [prop ]
84
+ for prop , var in (
85
+ ("cluster" , "CLUSTER" ),
86
+ ("environ" , "ENVIRON" ),
87
+ ("jobname" , "TOPOLOGY" ),
88
+ ("role" , "ROLE" ),
89
+ ("submission_user" , "USER" ))
90
+ if prop in execution_state
98
91
}
99
-
100
- formatted_url = formatter
101
-
102
- for key , value in list (common_parameters .items ()):
103
- formatted_url = formatted_url .replace (key , value )
104
-
105
- return formatted_url
92
+ return string .Template (formatter ).substitute (subs )
106
93
107
94
def __str__ (self ):
108
95
return "" .join ((self .config_str (c ) for c in self .configs [STATEMGRS_KEY ]))
109
96
110
- def config_str (self , config ):
97
+ @staticmethod
98
+ def config_str (config ):
111
99
keys = ("type" , "name" , "hostport" , "rootpath" , "tunnelhost" )
112
- return "" .join ("\t {}: {} \n " . format ( k , config [k ]) for k in keys if k in config ).rstrip ()
100
+ return "" .join ("\t {k }: {config[k]} \n " for k in keys if k in config ).rstrip ()
0 commit comments