Skip to content

Commit aea36a1

Browse files
committed
Refactor to use command pattern
1 parent 4f5823f commit aea36a1

File tree

11 files changed

+268
-215
lines changed

11 files changed

+268
-215
lines changed

aws_doc_sdk_examples_tools/agent/bin/main.py

Lines changed: 0 additions & 194 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from dataclasses import dataclass
2+
from pathlib import Path
3+
from typing import List
4+
5+
class Command:
6+
pass
7+
8+
@dataclass
9+
class CreatePrompts(Command):
10+
root: Path
11+
system_prompts: List[str]
12+
out_dir: Path
13+
language: str
14+
15+
@dataclass
16+
class RunAilly(Command):
17+
batches: List[str]
18+
19+
@dataclass
20+
class UpdateReservoir(Command):
21+
root: Path
22+
batches: List[str]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from pathlib import Path
2+
from typing import List
3+
from typing_extensions import Annotated
4+
from datetime import datetime
5+
import logging
6+
import typer
7+
8+
from aws_doc_sdk_examples_tools.agent.shared_constants import BATCH_PREFIX, AILLY_DIR_PATH
9+
from aws_doc_sdk_examples_tools.agent.domain import commands
10+
from aws_doc_sdk_examples_tools.agent.service_layer import messagebus
11+
12+
logging.basicConfig(
13+
level=logging.INFO,
14+
filename=f"lliam-run-{datetime.now().strftime('%Y%m%d_%H%M%S')}.log",
15+
filemode="w",
16+
)
17+
18+
logger = logging.getLogger(__name__)
19+
app = typer.Typer(name="Lliam")
20+
21+
@app.command()
22+
def create_prompts(iam_tributary_root: str, system_prompts: List[str] = []):
23+
doc_gen_root = Path(iam_tributary_root)
24+
cmd = commands.CreatePrompts(
25+
root=doc_gen_root,
26+
system_prompts=system_prompts,
27+
out_dir=AILLY_DIR_PATH,
28+
language="IAMPolicyGrammar"
29+
)
30+
messagebus.handle(cmd)
31+
32+
@app.command()
33+
def run_ailly(
34+
batch_nums: Annotated[
35+
str,
36+
typer.Option(
37+
help="Batch numbers to process (e.g., '33', '33..35', '33,35,37')"
38+
),
39+
] = None,
40+
) -> None:
41+
"""
42+
Run ailly to generate IAM policy content and process the results.
43+
If batch_nums is specified, only those batches will be processed.
44+
If batch_nums is omitted, all batches will be processed.
45+
"""
46+
requested_batches = parse_batch_names(batch_nums)
47+
cmd = commands.RunAilly(batches=requested_batches)
48+
messagebus.handle(cmd)
49+
50+
@app.command()
51+
def update_reservoir(
52+
iam_tributary_root: str,
53+
batch_nums: Annotated[
54+
str,
55+
typer.Option(
56+
help="Batch numbers to process (e.g., '33', '33..35', '33,35,37')"
57+
),
58+
] = None,
59+
) -> None:
60+
"""
61+
Update the doc_gen reservoir with processed IAM policy updates.
62+
If batch_nums is specified, only those batches will be processed.
63+
If batch_nums is omitted, all available update files will be processed.
64+
"""
65+
doc_gen_root = Path(iam_tributary_root)
66+
batch_names = parse_batch_names(batch_nums) if batch_nums else None
67+
cmd = commands.UpdateReservoir(
68+
root=doc_gen_root, batches=batch_names
69+
)
70+
messagebus.handle(cmd)
71+
72+
73+
def parse_batch_names(batch_nums_str: str) -> List[str]:
74+
"""
75+
Parse batch numbers from a string.
76+
"""
77+
if not batch_nums_str:
78+
return []
79+
80+
result = []
81+
parts = batch_nums_str.split(",")
82+
83+
for part in parts:
84+
part = part.strip()
85+
if ".." in part:
86+
start, end = part.split("..")
87+
start_num = int(start.strip())
88+
end_num = int(end.strip())
89+
result.extend(range(start_num, end_num + 1))
90+
else:
91+
result.append(int(part))
92+
93+
batch_nums = sorted(list(set(result)))
94+
return map(lambda b: f"{BATCH_PREFIX}{b:03}", batch_nums)
95+
96+
if __name__ == "__main__":
97+
app()

aws_doc_sdk_examples_tools/agent/service_layer/__init__.py

Whitespace-only changes.

aws_doc_sdk_examples_tools/agent/make_prompts.py renamed to aws_doc_sdk_examples_tools/agent/service_layer/make_prompts.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88
from typing import Any, Generator, Iterable, List, Tuple
99

10+
from aws_doc_sdk_examples_tools.agent.domain.commands import CreatePrompts
1011
from aws_doc_sdk_examples_tools.agent.shared_constants import BATCH_PREFIX
1112
from aws_doc_sdk_examples_tools.doc_gen import DocGen, Example
1213

@@ -116,12 +117,12 @@ def validate_root_path(doc_gen_root: Path):
116117

117118

118119
def make_prompts(
119-
doc_gen_root: Path, system_prompts: List[str], out_dir: Path, language: str
120+
create_prompts_cmd: CreatePrompts
120121
) -> None:
121122
"""Generate prompts and configuration files for Ailly."""
122-
validate_root_path(doc_gen_root)
123-
out_dir.mkdir(parents=True, exist_ok=True)
123+
validate_root_path(create_prompts_cmd.root)
124+
create_prompts_cmd.out_dir.mkdir(parents=True, exist_ok=True)
124125
system_prompts = read_files(system_prompts)
125-
setup_ailly(system_prompts, out_dir)
126-
doc_gen = make_doc_gen(doc_gen_root)
127-
write_prompts(doc_gen=doc_gen, out_dir=out_dir, language=language)
126+
setup_ailly(system_prompts, create_prompts_cmd.out_dir)
127+
doc_gen = make_doc_gen(create_prompts_cmd.doc_gen_root)
128+
write_prompts(doc_gen=doc_gen, out_dir=create_prompts_cmd.out_dir, language=create_prompts_cmd.language)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
from aws_doc_sdk_examples_tools.agent.domain import commands
4+
from aws_doc_sdk_examples_tools.agent.service_layer import make_prompts, update_doc_gen, run_ailly
5+
6+
# Only handling Commands for now.
7+
Message = commands.Command
8+
9+
def handle(message: commands.Command):
10+
queue = [message]
11+
12+
while queue:
13+
message = queue.pop(0)
14+
if isinstance(message, commands.Command):
15+
# Pass queue and UoW here if we implement the UoW pattern.
16+
handle_command(message)
17+
else:
18+
raise Exception(f"{message} was not a Command")
19+
20+
def handle_command(command: commands.Command):
21+
handler = COMMAND_HANDLERS[type(command)]
22+
handler(command)
23+
24+
COMMAND_HANDLERS = {
25+
commands.CreatePrompts: make_prompts.make_prompts,
26+
commands.RunAilly: run_ailly.handle_run_ailly,
27+
commands.UpdateReservoir: update_doc_gen.handle_update_reservoir
28+
}

0 commit comments

Comments
 (0)