From 1b61e731e3c6459b1e3d204888c7c950c194c5a5 Mon Sep 17 00:00:00 2001 From: Anthony FRANCISCO Date: Tue, 30 Aug 2022 13:16:04 +0000 Subject: [PATCH 1/3] Add YAML runner config support --- src/config/config_service.py | 14 ++++++++++---- src/utils/custom_yaml.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100755 src/utils/custom_yaml.py diff --git a/src/config/config_service.py b/src/config/config_service.py index 7a3ba7ab..31a8755f 100644 --- a/src/config/config_service.py +++ b/src/config/config_service.py @@ -10,7 +10,7 @@ from model import script_config from model.model_helper import InvalidFileException from model.script_config import get_sorted_config -from utils import os_utils, file_utils, process_utils, custom_json +from utils import os_utils, file_utils, process_utils, custom_json, custom_yaml from utils.file_utils import to_filename from utils.string_utils import is_blank, strip @@ -170,7 +170,10 @@ def list_configs(self, user, mode=None): def load_script(path, content): try: - json_object = custom_json.loads(content) + if path.endswith('.yaml'): + json_object = custom_yaml.loads(content) + else: + json_object = custom_json.loads(content) short_config = script_config.read_short(path, json_object) if short_config is None: @@ -205,7 +208,7 @@ def _visit_script_configs(self, visitor): configs_dir = self._script_configs_folder files = os.listdir(configs_dir) - configs = [file for file in files if file.lower().endswith(".json")] + configs = [file for file in files if file.lower().endswith(".json") or file.lower().endswith(".yaml")] result = [] @@ -231,7 +234,10 @@ def _visit_script_configs(self, visitor): def _find_config(self, name) -> Union[ConfigSearchResult, None]: def find_and_load(path, content): try: - json_object = custom_json.loads(content) + if path.endswith('.yaml'): + json_object = custom_yaml.loads(content) + else: + json_object = custom_json.loads(content) short_config = script_config.read_short(path, json_object) if short_config is None: diff --git a/src/utils/custom_yaml.py b/src/utils/custom_yaml.py new file mode 100755 index 00000000..6a79335e --- /dev/null +++ b/src/utils/custom_yaml.py @@ -0,0 +1,10 @@ +import json +import re +import yaml + +def loads (content, **args): + contents='' + for line in content.split('\n'): + if not re.match (r'\s*//.*', line): + contents += line + "\n" + return yaml.safe_load(contents, **args) From 8775db991a6e3b507069015c35f8bef81a5c98ee Mon Sep 17 00:00:00 2001 From: Anthony FRANCISCO Date: Tue, 6 Sep 2022 10:14:45 +0000 Subject: [PATCH 2/3] Improve pull request about YAML support --- src/config/config_service.py | 24 +++++++++++++----------- src/utils/custom_yaml.py | 7 +------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/config/config_service.py b/src/config/config_service.py index 31a8755f..c1182374 100644 --- a/src/config/config_service.py +++ b/src/config/config_service.py @@ -160,6 +160,14 @@ def _save_config(self, config, path): sorted_config = get_sorted_config(config) config_json = json.dumps(sorted_config, indent=2) file_utils.write_file(path, config_json) + + def load_config_file(self, path, content): + if path.endswith('.yaml'): + config_object = custom_yaml.loads(content) + else: + config_object = custom_json.loads(content) + + return config_object def list_configs(self, user, mode=None): edit_mode = mode == 'edit' @@ -170,11 +178,8 @@ def list_configs(self, user, mode=None): def load_script(path, content): try: - if path.endswith('.yaml'): - json_object = custom_yaml.loads(content) - else: - json_object = custom_json.loads(content) - short_config = script_config.read_short(path, json_object) + config_object = self.load_config_file(path, content) + short_config = script_config.read_short(path, config_object) if short_config is None: return None @@ -234,11 +239,8 @@ def _visit_script_configs(self, visitor): def _find_config(self, name) -> Union[ConfigSearchResult, None]: def find_and_load(path, content): try: - if path.endswith('.yaml'): - json_object = custom_yaml.loads(content) - else: - json_object = custom_json.loads(content) - short_config = script_config.read_short(path, json_object) + config_object = self.load_config_file(path, content) + short_config = script_config.read_short(path, config_object) if short_config is None: return None @@ -249,7 +251,7 @@ def find_and_load(path, content): if short_config.name != name.strip(): return None - raise StopIteration(ConfigSearchResult(short_config, path, json_object)) + raise StopIteration(ConfigSearchResult(short_config, path, config_object)) configs = self._visit_script_configs(find_and_load) if not configs: diff --git a/src/utils/custom_yaml.py b/src/utils/custom_yaml.py index 6a79335e..7c1addbd 100755 --- a/src/utils/custom_yaml.py +++ b/src/utils/custom_yaml.py @@ -1,10 +1,5 @@ -import json import re import yaml def loads (content, **args): - contents='' - for line in content.split('\n'): - if not re.match (r'\s*//.*', line): - contents += line + "\n" - return yaml.safe_load(contents, **args) + return yaml.safe_load(content, **args) From 111da179851295085982e0aaefed386e530ddd60 Mon Sep 17 00:00:00 2001 From: Anthony FRANCISCO Date: Wed, 7 Sep 2022 12:11:54 +0000 Subject: [PATCH 3/3] Push the import yaml declaration inside the loads function. --- src/utils/custom_yaml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/custom_yaml.py b/src/utils/custom_yaml.py index 7c1addbd..58a6089d 100755 --- a/src/utils/custom_yaml.py +++ b/src/utils/custom_yaml.py @@ -1,5 +1,5 @@ import re -import yaml def loads (content, **args): + import yaml return yaml.safe_load(content, **args)