Skip to content

Commit 88ab2fb

Browse files
authored
feat: add CLI operation hook (#22)
1 parent fbf21b5 commit 88ab2fb

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/git_draft/__main__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import optparse
66
import sys
77

8-
from .bots import load_bot
8+
from .bots import Operation, load_bot
99
from .common import Config, PROGRAM, Store, ensure_state_home, open_editor
1010
from .manager import Manager
1111

@@ -23,6 +23,11 @@ def new_parser() -> optparse.OptionParser:
2323
help="show log path and exit",
2424
action="store_true",
2525
)
26+
parser.add_option(
27+
"--root",
28+
help="path used to locate repository",
29+
dest="root",
30+
)
2631

2732
def add_command(name: str, **kwargs) -> None:
2833
def callback(_option, _opt, _value, parser) -> None:
@@ -81,6 +86,10 @@ def callback(_option, _opt, _value, parser) -> None:
8186
return parser
8287

8388

89+
def print_operation(op: Operation) -> None:
90+
print(op)
91+
92+
8493
def main() -> None:
8594
config = Config.load()
8695
(opts, _args) = new_parser().parse_args()
@@ -91,7 +100,11 @@ def main() -> None:
91100
return
92101
logging.basicConfig(level=config.log_level, filename=str(log_path))
93102

94-
manager = Manager.create(Store.persistent())
103+
manager = Manager.create(
104+
store=Store.persistent(),
105+
path=opts.root,
106+
operation_hook=print_operation,
107+
)
95108
command = getattr(opts, "command", "generate")
96109
if command == "generate":
97110
bot = load_bot(opts.bot, {})

src/git_draft/bots/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
from typing import Any, Mapping
77

8-
from .common import Action, Bot, Toolbox
8+
from .common import Action, Bot, Operation, OperationHook, Toolbox
99

1010
__all__ = [
1111
"Action",
1212
"Bot",
13+
"Operation",
14+
"OperationHook",
1315
"Toolbox",
1416
]
1517

src/git_draft/bots/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Toolbox:
1919
# including inferring the most important files, and allowing returning
2020
# signature-only versions.
2121

22+
# TODO: Support a diff-based edit method.
23+
# https://gist.github.com/noporpoise/16e731849eb1231e86d78f9dfeca3abc
24+
2225
def __init__(self, hook: OperationHook | None = None) -> None:
2326
self.operations = list[Operation]()
2427
self._operation_hook = hook

src/git_draft/manager.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import time
1212
from typing import Match, Sequence, override
1313

14-
from .bots import Bot, Toolbox
14+
from .bots import Bot, OperationHook, Toolbox
1515
from .common import Store, random_id, sql
1616

1717

@@ -54,8 +54,8 @@ class _Toolbox(Toolbox):
5454
concurrent editing without interference.
5555
"""
5656

57-
def __init__(self, repo: git.Repo) -> None:
58-
super().__init__()
57+
def __init__(self, repo: git.Repo, hook: OperationHook | None) -> None:
58+
super().__init__(hook)
5959
self._repo = repo
6060

6161
@override
@@ -85,15 +85,27 @@ def _write(self, path: PurePosixPath, contents: str) -> None:
8585
class Manager:
8686
"""Draft state manager"""
8787

88-
def __init__(self, store: Store, repo: git.Repo) -> None:
88+
def __init__(
89+
self, store: Store, repo: git.Repo, hook: OperationHook | None = None
90+
) -> None:
8991
with store.cursor() as cursor:
9092
cursor.executescript(sql("create-tables"))
9193
self._store = store
9294
self._repo = repo
95+
self._operation_hook = hook
9396

9497
@classmethod
95-
def create(cls, store: Store, path: str | None = None) -> Manager:
96-
return cls(store, git.Repo(path, search_parent_directories=True))
98+
def create(
99+
cls,
100+
store: Store,
101+
path: str | None = None,
102+
operation_hook: OperationHook | None = None,
103+
) -> Manager:
104+
return cls(
105+
store,
106+
git.Repo(path, search_parent_directories=True),
107+
operation_hook,
108+
)
97109

98110
def _create_branch(self, sync: bool) -> _Branch:
99111
if not self._repo.active_branch:
@@ -155,7 +167,7 @@ def generate_draft(
155167
)
156168

157169
start_time = time.perf_counter()
158-
toolbox = _Toolbox(self._repo)
170+
toolbox = _Toolbox(self._repo, self._operation_hook)
159171
action = bot.act(prompt, toolbox)
160172
end_time = time.perf_counter()
161173

0 commit comments

Comments
 (0)