Skip to content

Commit

Permalink
#129 added possibility to map list values on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Apr 9, 2023
1 parent 000d229 commit 3c22339
Show file tree
Hide file tree
Showing 30 changed files with 1,088 additions and 492 deletions.
17 changes: 12 additions & 5 deletions samples/configs/parameterized.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
{
"name": "Simple Int",
"param": "--simple_int",
"type": "int"
"type": "int",
"values_ui_mapping": {
"One": "1"
}
},
{
"name": "Simple Boolean",
Expand All @@ -30,15 +33,16 @@
"param": "--simple_list=",
"same_arg_param": true,
"type": "list",
"default": {
"script": "echo ${Simple Text}"
},
"description": "Parameter Five",
"values": [
"val1",
"val3",
"some long value"
]
],
"values_ui_mapping": {
"val1": "Value 1",
"val3": "Value 3"
}
},
{
"name": "File upload",
Expand Down Expand Up @@ -183,6 +187,9 @@
"type": "int",
"description": "Parameter Nine",
"secure": true,
"values_ui_mapping": {
"qwerty": "2232"
},
"ui": {
"separator_before": {
"type": "line"
Expand Down
7 changes: 2 additions & 5 deletions src/config/script/list_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ def get_required_parameters(self):
def get_values(self, parameter_values):
pass

def map_value(self, user_value):
return user_value


class EmptyValuesProvider(ValuesProvider):

Expand Down Expand Up @@ -88,8 +85,8 @@ def get_required_parameters(self):

def get_values(self, parameter_values):
for param_name in self._required_parameters:
value = parameter_values.get(param_name)
if is_empty(value):
value_wrapper = parameter_values.get(param_name)
if (value_wrapper is None) or is_empty(value_wrapper.mapped_script_value):
return []

parameters = self._parameters_supplier()
Expand Down
7 changes: 2 additions & 5 deletions src/execution/execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ def get_active_executor(self, execution_id, user):

return self._executors.get(execution_id)

def start_script(self, config, values, user: User):
def start_script(self, config, user: User):
audit_name = user.get_audit_name()

config.set_all_param_values(values)
normalized_values = dict(config.parameter_values)

executor = ScriptExecutor(config, normalized_values, self._env_vars)
executor = ScriptExecutor(config, self._env_vars)
execution_id = self._id_generator.next_id()

audit_command = executor.get_secure_command()
Expand Down
53 changes: 3 additions & 50 deletions src/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from model import model_helper
from model.model_helper import read_bool
from model.parameter_config import ParameterModel
from model.script_config import ConfigModel
from react.observable import ObservableBase
from utils import file_utils, process_utils, os_utils, string_utils
from utils.env_utils import EnvVariables
Expand Down Expand Up @@ -44,40 +45,11 @@ def _normalize_working_dir(working_directory):
return file_utils.normalize_path(working_directory)


def _wrap_values(user_values, parameters):
result = {}
for parameter in parameters:
name = parameter.name

if parameter.constant:
value = parameter.default
result[name] = _Value(None, value, value, parameter.value_to_str(value))
continue

if name in user_values:
user_value = user_values[name]

if parameter.no_value:
bool_value = model_helper.read_bool(user_value)
result[name] = _Value(user_value, bool_value, bool_value)
continue

elif user_value:
mapped_value = parameter.map_to_script(user_value)
script_arg = parameter.to_script_args(mapped_value)
secure_value = parameter.get_secured_value(script_arg)
result[name] = _Value(user_value, mapped_value, script_arg, secure_value)
else:
result[name] = _Value(None, None, None)

return result


class ScriptExecutor:
def __init__(self, config, parameter_values, env_vars: EnvVariables):
def __init__(self, config: ConfigModel, env_vars: EnvVariables):
self.config = config
self._env_vars = env_vars
self._parameter_values = _wrap_values(parameter_values, config.parameters)
self._parameter_values = dict(config.parameter_values)
self._working_directory = _normalize_working_dir(config.working_directory)

self.script_base_command = process_utils.split_command(
Expand Down Expand Up @@ -356,25 +328,6 @@ def send_stdin_parameters(
lambda closed_value=value: process_wrapper.write_to_input(closed_value)))


class _Value:
def __init__(self, user_value, mapped_script_value, script_arg, secure_value=None):
self.user_value = user_value
self.mapped_script_value = mapped_script_value
self.script_arg = script_arg
self.secure_value = secure_value

def get_secure_value(self):
if self.secure_value is not None:
return self.secure_value
return self.script_arg

def __str__(self) -> str:
if self.secure_value is not None:
return str(self.secure_value)

return str(self.script_arg)


class _ExpectedTextListener:
def __init__(self, expected_text, callback):
self.expected_text = expected_text
Expand Down
12 changes: 6 additions & 6 deletions src/execution/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def start_logging(self, execution_id,
output_stream,
all_audit_names,
script_config,
parameter_values,
parameter_value_wrappers,
start_time_millis=None):

script_name = str(script_config.name)
Expand All @@ -145,7 +145,7 @@ def start_logging(self, execution_id,
start_time_millis,
script_config.logging_config,
script_config.parameters,
parameter_values)
parameter_value_wrappers)
log_file_path = os.path.join(self._output_folder, log_filename)
log_file_path = file_utils.create_unique_filename(log_file_path)

Expand Down Expand Up @@ -367,7 +367,7 @@ def create_filename(self,
start_time,
custom_logging_config: Optional[LoggingConfig],
parameter_configs,
parameter_values):
parameter_value_wrappers):

audit_name = get_audit_name(all_audit_names)
audit_name = file_utils.to_filename(audit_name)
Expand All @@ -388,7 +388,7 @@ def create_filename(self,
}

filename = self._resolve_filename_template(custom_logging_config).safe_substitute(mapping)
filename = model_helper.fill_parameter_values(parameter_configs, filename, parameter_values)
filename = model_helper.fill_parameter_values(parameter_configs, filename, parameter_value_wrappers)
if not filename.lower().endswith('.log'):
filename += '.log'

Expand Down Expand Up @@ -423,7 +423,7 @@ def started(execution_id, user):
all_audit_names = user.audit_names
output_stream = execution_service.get_anonymized_output_stream(execution_id)
audit_command = execution_service.get_audit_command(execution_id)
parameter_values = execution_service.get_user_parameter_values(execution_id)
parameter_value_wrappers = script_config.parameter_values

logging_service.start_logging(
execution_id,
Expand All @@ -433,7 +433,7 @@ def started(execution_id, user):
output_stream,
all_audit_names,
script_config,
parameter_values)
parameter_value_wrappers)

def finished(execution_id, user):
exit_code = execution_service.get_exit_code(execution_id)
Expand Down
8 changes: 4 additions & 4 deletions src/features/file_download_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _get_paths(self, execution_id, predicate):
paths = [_extract_path(f) for f in config.output_files if predicate(f)]
paths = [p for p in paths if p]

parameter_values = self.execution_service.get_script_parameter_values(execution_id)
parameter_value_wrappers = config.parameter_values
all_audit_names = self.execution_service.get_all_audit_names(execution_id)

audit_name = audit_utils.get_audit_name(all_audit_names)
Expand All @@ -110,7 +110,7 @@ def _get_paths(self, execution_id, predicate):
return substitute_variable_values(
config.parameters,
paths,
parameter_values,
parameter_value_wrappers,
audit_name,
username)

Expand Down Expand Up @@ -238,10 +238,10 @@ def _add_inline_image(self, original_path, download_path):
LOGGER.error('Failed to notify image listener')


def substitute_variable_values(parameter_configs, output_files, values, audit_name, username):
def substitute_variable_values(parameter_configs, output_files, value_wrappers, audit_name, username):
output_file_parsed = []
for _, output_file in enumerate(output_files):
substituted_file = fill_parameter_values(parameter_configs, output_file, values)
substituted_file = fill_parameter_values(parameter_configs, output_file, value_wrappers)
substituted_file = replace_auth_vars(substituted_file, username, audit_name)
output_file_parsed.append(substituted_file)

Expand Down
4 changes: 2 additions & 2 deletions src/model/external_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def parameter_to_external(parameter):
'description': parameter.description,
'withoutValue': parameter.no_value,
'required': parameter.required,
'default': parameter.default,
'default': parameter.create_value_wrapper_for_default().user_value,
'type': parameter.type,
'min': parameter.min,
'max': parameter.max,
'max_length': parameter.max_length,
'regex': parameter.regex,
'values': parameter.values,
'values': parameter.get_ui_values(),
'secure': parameter.secure,
'fileRecursive': parameter.file_recursive,
'fileType': parameter.file_type,
Expand Down
9 changes: 3 additions & 6 deletions src/model/model_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,20 @@ def is_empty(value):
return (not value) and (value != 0) and (value is not False)


def fill_parameter_values(parameter_configs, template, values):
def fill_parameter_values(parameter_configs, template, value_wrappers):
result = template

for parameter_config in parameter_configs:
if parameter_config.secure or parameter_config.no_value:
continue

parameter_name = parameter_config.name
value = values.get(parameter_name)
value_wrapper = value_wrappers.get(parameter_name)
value = value_wrapper.mapped_script_value if value_wrapper else None

if value is None:
value = ''

if not isinstance(value, str):
mapped_value = parameter_config.map_to_script(value)
value = parameter_config.to_script_args(mapped_value)

result = result.replace('${' + parameter_name + '}', str(value))

return result
Expand Down
Loading

0 comments on commit 3c22339

Please sign in to comment.