Skip to content

Commit 8219d41

Browse files
author
Bryan Sieber
committed
Abosrbing review changes. Linting issues to be resolved in followup commits.
1 parent 79352f8 commit 8219d41

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

src/core/actions.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
# update this dict with additional action contexts
66

77

8+
class ActionError(Exception):
9+
pass
10+
11+
812
def get_action_context_by_key(key):
913
if key not in module_dict:
10-
assert False, "Unknown key requested"
14+
raise ValueError("Unknown key requested")
1115
requested_module = module_dict.get(key)
1216
return get_action_context_from_module(action_module=requested_module)
1317

1418

1519
def get_action_context_from_module(action_module):
1620
context_map = {}
1721
for _, module in getmembers(action_module, ismodule):
18-
# print(module)
1922
methods = getmembers(module, isfunction)
2023
for method_name, method in methods:
21-
assert (
24+
method_name_available = (
2225
method_name
2326
not in context_map.keys() # pylint: disable=consider-iterating-dictionary
24-
), f"ACTION ERROR: Action with name `{method_name}` already exists."
27+
)
28+
if not method_name_available:
29+
raise ActionError(
30+
f"ACTION ERROR: Action with name `{method_name}` already exists."
31+
)
2532
context_map[method_name] = method
2633

2734
return context_map

src/core/configurator.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import glob
21
import json
3-
import os
4-
from typing import Dict, List, Tuple
2+
from pathlib import Path
3+
from typing import Any, Dict, List, Optional, Tuple
54

65
from src.core import actions
76

87

8+
class ConfigError(Exception):
9+
pass
10+
11+
12+
class ProcessError(Exception):
13+
pass
14+
15+
916
def per_file_process(
10-
filename: str, ret_dict: dict, required_keys, action_key, filename_key
11-
) -> Tuple:
17+
filename: str, ret_dict: Dict, required_keys, action_key, filename_key
18+
) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
1219
with open(filename, encoding="utf-8") as file:
1320
data = json.load(file)
1421
ret_dict.update(data)
@@ -19,19 +26,22 @@ def per_file_process(
1926
action = ret_dict.get("action")
2027
known_actions = actions.get_action_context_by_key(key=action_key)
2128
if action not in known_actions:
22-
assert False, f"CONFIG ERROR: Unknown action `{action}`."
29+
raise ConfigError(f"CONFIG ERROR: Unknown action `{action}`.")
2330

2431
for key_str in required_keys:
2532
key_value = ret_dict.get(key_str)
26-
assert (
27-
key_value is not None
28-
), f"CONFIG ERROR: Required field `{key_str}` not found in {filename}."
33+
if not key_value:
34+
raise ConfigError(
35+
f"CONFIG ERROR: Required field `{key_str}` not found in {filename}."
36+
)
2937

3038
ret_key = ret_dict.get(filename_key)
31-
assert ret_key is not None and ret_key in filename, (
32-
f"CONFIG ERROR: Filename should contain value within key `{filename_key}`. The "
33-
f"value {ret_key} from the key is expected to be in the filename. "
34-
)
39+
filename_valid = ret_key is not None and ret_key in filename
40+
if not filename_valid:
41+
raise ConfigError(
42+
f"CONFIG ERROR: Filename should contain value within key `{filename_key}`. The "
43+
f"value {ret_key} from the key is expected to be in the filename. "
44+
)
3545

3646
return ret_key, ret_dict
3747

@@ -47,16 +57,17 @@ def process_all_files_in_path(
4757
if not config_map:
4858
config_map = {}
4959

50-
for filename in glob.glob(os.path.join(folder_path, "*.json")):
60+
for filename in Path(folder_path).glob("*.json"):
5161
try:
52-
if "TEMPLATE" in filename:
62+
filename_s = str(filename)
63+
if "TEMPLATE" in filename_s:
5364
continue
54-
key, value = process(filename)
65+
key, value = process(filename_s)
5566
if key:
5667
config_map[key] = value
57-
except Exception as exception: # pylint: disable=broad-except
68+
except (ValueError, ConfigError, actions.ActionError) as exception:
5869
errors.append(exception)
5970

6071
if errors:
61-
assert False, f"PROCESS ERROR: errors exist: {errors}"
72+
raise ProcessError(f"PROCESS ERROR: errors exist: {errors}")
6273
return config_map

src/jbi/whiteboard_actions/default.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
def default_action(data, context):
6-
print(services.bugzilla)
7-
print(services.jira)
6+
print(services.get_bugzilla())
7+
print(services.get_jira())
88

99

1010
def default_helper():

tests/unit/core/test_configurator.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# pylint: disable=cannot-enumerate-pytest-fixtures
22
import pytest
33

4-
from src.core.configurator import actions, per_file_process, process_all_files_in_path
4+
from src.core.configurator import (
5+
ConfigError,
6+
ProcessError,
7+
actions,
8+
per_file_process,
9+
process_all_files_in_path,
10+
)
511
from tests.unit.core import mock_actions
612

713

@@ -67,7 +73,7 @@ def test_file_processing_enabled_file_unknown_action():
6773
req_keys = ["enabled"]
6874
filename = "tests/unit/core/mock_config_files/unknown_action.json"
6975
actions.module_dict[action_key] = mock_actions
70-
with pytest.raises(AssertionError):
76+
with pytest.raises(ConfigError):
7177
per_file_process(
7278
filename=filename,
7379
required_keys=req_keys,
@@ -79,9 +85,9 @@ def test_file_processing_enabled_file_unknown_action():
7985

8086
def test_config_path_throws_exception():
8187
def raise_except(filename):
82-
raise Exception
88+
raise ConfigError
8389

84-
with pytest.raises(AssertionError):
90+
with pytest.raises(ProcessError):
8591
process_all_files_in_path(
8692
folder_path="tests/unit/core/mock_config_files/", process=raise_except
8793
)

tests/unit/jbi/test_jbi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def test_mock_jbi_files():
9-
with pytest.raises(AssertionError):
9+
with pytest.raises(test_configurator.ProcessError):
1010
test_configurator.process_all_files_in_path(
1111
process=jbi_configuration.jbi_config_process,
1212
folder_path="tests/unit/jbi/mock_jbi_files",

0 commit comments

Comments
 (0)