Skip to content

Commit 72e8547

Browse files
committed
Add directory initialization to CLI
1 parent 6c665ec commit 72e8547

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,5 @@ dmypy.json
134134
.pyre/
135135

136136
# DevChat
137-
.chat/store.graphml
137+
.chat/prompts.graphml
138+
.chat/prompts.db

devchat/_cli.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
This module contains the main function for the DevChat CLI.
33
"""
4-
import os
5-
from typing import Optional
4+
from contextlib import contextmanager
65
import json
6+
import os
77
import sys
8-
from contextlib import contextmanager
8+
from typing import Optional, Tuple
99
import rich_click as click
1010
from devchat.store import Store
1111
from devchat.openai import OpenAIChatConfig, OpenAIChat
@@ -31,7 +31,12 @@ def handle_errors():
3131
sys.exit(os.EX_SOFTWARE)
3232

3333

34-
def load_config_data(chat_dir: str) -> dict:
34+
def init_dir() -> Tuple[dict, Store]:
35+
git_root = find_git_root()
36+
chat_dir = os.path.join(git_root, ".chat")
37+
if not os.path.exists(chat_dir):
38+
os.makedirs(chat_dir)
39+
3540
default_config_data = {
3641
'llm': 'OpenAI',
3742
'OpenAI': {
@@ -46,7 +51,9 @@ def load_config_data(chat_dir: str) -> dict:
4651
except FileNotFoundError:
4752
config_data = default_config_data
4853

49-
return config_data
54+
store = Store(chat_dir)
55+
git_ignore(git_root, store.graph_path, store.db_path)
56+
return config_data, store
5057

5158

5259
@main.command()
@@ -120,12 +127,7 @@ def prompt(content: Optional[str], parent: Optional[str], reference: Optional[st
120127
```
121128
122129
"""
123-
git_root = find_git_root()
124-
chat_dir = os.path.join(git_root, ".chat")
125-
if not os.path.exists(chat_dir):
126-
os.makedirs(chat_dir)
127-
128-
config_data = load_config_data(chat_dir)
130+
config, store = init_dir()
129131

130132
with handle_errors():
131133
if content is None:
@@ -137,13 +139,9 @@ def prompt(content: Optional[str], parent: Optional[str], reference: Optional[st
137139
instruct_contents = parse_files(instruct)
138140
context_contents = parse_files(context)
139141

140-
store = Store(chat_dir)
141-
git_ignore(git_root, store.graph_path)
142-
git_ignore(git_root, store.db_path)
143-
144-
llm = config_data.get('llm')
142+
llm = config.get('llm')
145143
if llm == 'OpenAI':
146-
openai_config = OpenAIChatConfig(**config_data['OpenAI'])
144+
openai_config = OpenAIChatConfig(**config['OpenAI'])
147145
chat = OpenAIChat(openai_config)
148146

149147
openai_asisstant = Assistant(chat, store)
@@ -164,12 +162,8 @@ def log(skip, max_count):
164162
"""
165163
Show the prompt history.
166164
"""
167-
git_root = find_git_root()
168-
chat_dir = os.path.join(git_root, ".chat")
169-
if not os.path.exists(chat_dir):
170-
os.makedirs(chat_dir)
165+
_, store = init_dir()
171166

172-
store = Store(chat_dir)
173167
recent_prompts = store.select_recent(skip, skip + max_count)
174168

175169
for record in recent_prompts:

devchat/store.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88

99
class Store:
10-
def __init__(self, folder: str):
10+
def __init__(self, store_dir: str):
1111
"""
1212
Initializes a Store instance.
1313
1414
Args:
1515
path (str): The folder to store the files containing the store.
1616
"""
17-
folder = os.path.expanduser(folder)
18-
if not os.path.isdir(folder):
19-
os.makedirs(folder)
20-
self._graph_path = os.path.join(folder, 'prompts.graphml')
21-
self._db_path = os.path.join(folder, 'prompts.db')
17+
store_dir = os.path.expanduser(store_dir)
18+
if not os.path.isdir(store_dir):
19+
os.makedirs(store_dir)
20+
self._graph_path = os.path.join(store_dir, 'prompts.graphml')
21+
self._db_path = os.path.join(store_dir, 'prompts.db')
2222

2323
if os.path.isfile(self._graph_path):
2424
try:

devchat/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ def find_git_root():
1717
raise RuntimeError("Not inside a Git repository") from error
1818

1919

20-
def git_ignore(git_root_dir, ignore_entry):
20+
def git_ignore(git_root_dir, *ignore_entries):
2121
gitignore_path = os.path.join(git_root_dir, '.gitignore')
2222

2323
if os.path.exists(gitignore_path):
2424
with open(gitignore_path, 'r', encoding='utf-8') as gitignore_file:
2525
gitignore_content = gitignore_file.read()
2626

27-
if ignore_entry not in gitignore_content:
27+
new_entries = []
28+
for entry in ignore_entries:
29+
relative_entry = os.path.relpath(entry, git_root_dir)
30+
if relative_entry not in gitignore_content:
31+
new_entries.append(relative_entry)
32+
33+
if new_entries:
2834
with open(gitignore_path, 'a', encoding='utf-8') as gitignore_file:
29-
gitignore_file.write(f'\n# DevChat\n{ignore_entry}\n')
35+
gitignore_file.write('\n# DevChat\n')
36+
for entry in new_entries:
37+
gitignore_file.write(f'{entry}\n')
3038
else:
3139
with open(gitignore_path, 'w', encoding='utf-8') as gitignore_file:
32-
gitignore_file.write(f'# DevChat\n{ignore_entry}\n')
40+
gitignore_file.write('# DevChat\n')
41+
for entry in ignore_entries:
42+
relative_entry = os.path.relpath(entry, git_root_dir)
43+
gitignore_file.write(f'{relative_entry}\n')
3344

3445

3546
def unix_to_local_datetime(unix_time) -> datetime.datetime:

0 commit comments

Comments
 (0)