Skip to content

Commit 5f9b868

Browse files
authored
Update typing and return values for handler consistency. (#198)
1 parent cc9a4cf commit 5f9b868

File tree

8 files changed

+69
-40
lines changed

8 files changed

+69
-40
lines changed

aws_doc_sdk_examples_tools/lliam/domain/commands.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
from typing import List
44

55

6+
@dataclass(frozen=True)
67
class Command:
7-
pass
8+
@property
9+
def name(self):
10+
return self.__class__.__name__
811

912

10-
@dataclass
13+
@dataclass(frozen=True)
1114
class CreatePrompts(Command):
1215
doc_gen_root: str
1316
system_prompts: List[str]
1417
out_dir: str
1518

1619

17-
@dataclass
20+
@dataclass(frozen=True)
1821
class RunAilly(Command):
1922
batches: List[str]
2023
packages: List[str]
2124

2225

23-
@dataclass
26+
@dataclass(frozen=True)
2427
class UpdateReservoir(Command):
2528
root: Path
2629
batches: List[str]
2730
packages: List[str]
2831

2932

30-
@dataclass
33+
@dataclass(frozen=True)
3134
class DedupeReservoir(Command):
3235
root: Path
3336
packages: List[str]
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from dataclasses import dataclass
22

33

4-
@dataclass
4+
@dataclass(frozen=True)
55
class DomainError:
6-
pass
6+
message: str
77

88

9-
@dataclass
9+
@dataclass(frozen=True)
1010
class CommandExecutionError(DomainError):
1111
command_name: str
12-
message: str
12+
13+
def __repr__(self):
14+
return f"[{self.command_name}] {self.message}"

aws_doc_sdk_examples_tools/lliam/entry_points/lliam_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import List, Optional
2+
from typing import List, Optional, Sequence
33
from typing_extensions import Annotated
44
from datetime import datetime
55
import logging
@@ -98,10 +98,10 @@ def dedupe_reservoir(
9898
handle_domain_errors(errors)
9999

100100

101-
def handle_domain_errors(errors: List[errors.DomainError]):
101+
def handle_domain_errors(errors: Sequence[errors.DomainError]):
102102
if errors:
103103
for error in errors:
104-
logger.error(error)
104+
logger.error(error.message)
105105
typer.Exit(code=1)
106106

107107

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import logging
2+
from typing import List
23

34
from aws_doc_sdk_examples_tools.lliam.domain.operations import build_ailly_config
45
from aws_doc_sdk_examples_tools.lliam.domain.commands import CreatePrompts
6+
from aws_doc_sdk_examples_tools.lliam.domain.errors import DomainError
57
from aws_doc_sdk_examples_tools.lliam.service_layer.unit_of_work import FsUnitOfWork
68

79
logger = logging.getLogger(__name__)
810

911

10-
def create_prompts(cmd: CreatePrompts, uow: FsUnitOfWork):
12+
def create_prompts(cmd: CreatePrompts, uow: FsUnitOfWork) -> List[DomainError]:
1113
with uow:
1214
system_prompts = uow.prompts.get_all(cmd.system_prompts)
1315
ailly_config = build_ailly_config(system_prompts)
@@ -16,3 +18,5 @@ def create_prompts(cmd: CreatePrompts, uow: FsUnitOfWork):
1618
uow.prompts.add(ailly_config)
1719
uow.prompts.set_partition(cmd.out_dir)
1820
uow.commit()
21+
# TODO: Catch and return any errors
22+
return []

aws_doc_sdk_examples_tools/lliam/service_layer/dedupe_reservoir.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
from collections import Counter
55
from dataclasses import replace
66
from pathlib import Path
7-
from typing import Dict, Iterable, List
7+
from typing import Dict, List, Sequence
88

99
from aws_doc_sdk_examples_tools.doc_gen import DocGen
1010
from aws_doc_sdk_examples_tools.lliam.domain.commands import DedupeReservoir
11+
from aws_doc_sdk_examples_tools.lliam.domain.errors import (
12+
DomainError,
13+
CommandExecutionError,
14+
)
1115
from aws_doc_sdk_examples_tools.metadata import Example
1216
from aws_doc_sdk_examples_tools.yaml_writer import prepare_write, write_many
1317
from aws_doc_sdk_examples_tools.project_validator import ValidationConfig
@@ -83,7 +87,9 @@ def write_examples(examples: Dict[str, Example], root: Path):
8387
write_many(root, writes)
8488

8589

86-
def handle_dedupe_reservoir(cmd: DedupeReservoir, uow: None):
90+
def handle_dedupe_reservoir(cmd: DedupeReservoir, uow: None) -> Sequence[DomainError]:
8791
doc_gen = DocGen.from_root(cmd.root, validation=ValidationConfig(check_aws=False))
8892
examples = dedupe_examples(doc_gen.examples, cmd.packages)
8993
write_examples(examples, cmd.root)
94+
# TODO: Catch an return any errors
95+
return []

aws_doc_sdk_examples_tools/lliam/service_layer/messagebus.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Callable, Dict, Optional, Type
1+
from typing import Any, Callable, Dict, List, Optional, Sequence, Type
22

3-
from aws_doc_sdk_examples_tools.lliam.domain import commands
3+
from aws_doc_sdk_examples_tools.lliam.domain import commands, errors
44
from aws_doc_sdk_examples_tools.lliam.service_layer import (
55
create_prompts,
66
dedupe_reservoir,
@@ -13,24 +13,35 @@
1313
Message = commands.Command
1414

1515

16-
def handle(message: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork] = None):
17-
queue = [message]
16+
def handle(
17+
message: Any, uow: Optional[unit_of_work.FsUnitOfWork] = None
18+
) -> Sequence[errors.DomainError]:
19+
if isinstance(message, commands.Command):
20+
return handle_command(message, uow)
1821

19-
while queue:
20-
message = queue.pop(0)
21-
if isinstance(message, commands.Command):
22-
return handle_command(message, uow)
23-
else:
24-
raise Exception(f"{message} was not a Command")
22+
return [
23+
errors.CommandExecutionError(
24+
command_name="Unknown", message=f"{message} was not a Command"
25+
)
26+
]
2527

2628

27-
def handle_command(command: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork]):
28-
handler = COMMAND_HANDLERS[type(command)]
29-
errors = handler(command, uow)
30-
return errors
29+
def handle_command(
30+
command: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork]
31+
) -> Sequence[errors.DomainError]:
32+
handler = COMMAND_HANDLERS.get(type(command))
33+
if not handler:
34+
return [
35+
errors.CommandExecutionError(
36+
command_name=command.name, message="Handler for not found."
37+
)
38+
]
39+
return handler(command, uow)
3140

3241

33-
COMMAND_HANDLERS: Dict[Type[commands.Command], Callable] = {
42+
COMMAND_HANDLERS: Dict[
43+
Type[commands.Command], Callable[..., Sequence[errors.DomainError]]
44+
] = {
3445
commands.CreatePrompts: create_prompts.create_prompts,
3546
commands.RunAilly: run_ailly.handle_run_ailly,
3647
commands.UpdateReservoir: update_doc_gen.handle_update_reservoir,

aws_doc_sdk_examples_tools/lliam/service_layer/run_ailly.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import logging
33
import time
44
from collections import defaultdict
5-
from dataclasses import dataclass
65
from datetime import timedelta
76
from pathlib import Path
87
from subprocess import run
9-
from typing import Any, Dict, List, Optional, Set
8+
from typing import Any, Dict, List, Optional, Set, Sequence
109

1110
from aws_doc_sdk_examples_tools.lliam.domain.commands import RunAilly
1211
from aws_doc_sdk_examples_tools.lliam.domain.errors import (
@@ -30,7 +29,7 @@
3029
logger = logging.getLogger(__file__)
3130

3231

33-
def handle_run_ailly(cmd: RunAilly, uow: None):
32+
def handle_run_ailly(cmd: RunAilly, uow: None) -> Sequence[DomainError]:
3433
resolved_batches = resolve_requested_batches(cmd.batches)
3534

3635
errors: List[DomainError] = []
@@ -43,9 +42,7 @@ def handle_run_ailly(cmd: RunAilly, uow: None):
4342
run_ailly_single_batch(batch, cmd.packages)
4443
except FileNotFoundError as e:
4544
errors.append(
46-
CommandExecutionError(
47-
command_name=cmd.__class__.__name__, message=str(e)
48-
)
45+
CommandExecutionError(message=str(e), command_name=cmd.name)
4946
)
5047

5148
total_end_time = time.time()
@@ -192,7 +189,7 @@ def parse_ailly_file(
192189
return result
193190

194191

195-
def parse_package_name(policy_update: Dict[str, str]) -> Optional[str]:
192+
def parse_package_name(policy_update: Any) -> Optional[str]:
196193
if not policy_update:
197194
return None
198195

aws_doc_sdk_examples_tools/lliam/service_layer/update_doc_gen.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import logging
44
from pathlib import Path
5-
from typing import Dict, Iterable, List
5+
from typing import Dict, Iterable, List, Sequence
66

77
from aws_doc_sdk_examples_tools.lliam.adapters.repository import DEFAULT_METADATA_PREFIX
88
from aws_doc_sdk_examples_tools.yaml_writer import prepare_write, write_many
@@ -12,6 +12,10 @@
1212
BATCH_PREFIX,
1313
)
1414
from aws_doc_sdk_examples_tools.lliam.domain.commands import UpdateReservoir
15+
from aws_doc_sdk_examples_tools.lliam.domain.errors import (
16+
DomainError,
17+
CommandExecutionError,
18+
)
1519
from aws_doc_sdk_examples_tools.doc_gen import DocGen, Example
1620

1721
logger = logging.getLogger(__name__)
@@ -96,7 +100,7 @@ def merge_updates(a: Updates, b: Updates) -> Updates:
96100
return merged
97101

98102

99-
def handle_update_reservoir(cmd: UpdateReservoir, uow: None):
103+
def handle_update_reservoir(cmd: UpdateReservoir, uow: None) -> Sequence[DomainError]:
100104
update_files = (
101105
[AILLY_DIR_PATH / f"updates_{batch}.json" for batch in cmd.batches]
102106
if cmd.batches
@@ -105,7 +109,7 @@ def handle_update_reservoir(cmd: UpdateReservoir, uow: None):
105109

106110
if not update_files:
107111
logger.warning("No IAM update files found to process")
108-
return
112+
return []
109113

110114
doc_gen = DocGen.from_root(cmd.root)
111115

@@ -133,3 +137,5 @@ def handle_update_reservoir(cmd: UpdateReservoir, uow: None):
133137
updated_examples = update_doc_gen(doc_gen=doc_gen, updates=combined_updates)
134138
writes = prepare_write(updated_examples)
135139
write_many(cmd.root, writes)
140+
# TODO Catch and return any errors
141+
return []

0 commit comments

Comments
 (0)