Skip to content

Commit e15b27e

Browse files
1.19.0
1 parent 6b0c06e commit e15b27e

19 files changed

+431
-189
lines changed

dedlin/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def document_inputter(prompt: str, text: str = "") -> Generator[str, None, None]
127127
if not vim_mode:
128128
break
129129
except Exception as the_exception:
130-
dedlin.save_on_crash(the_exception, None, None)
130+
dedlin.save_on_crash(type(the_exception), the_exception, None)
131131
print(traceback.format_exc())
132132
break
133133
dedlin.final_report()

dedlin/ai_interface.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""
22
Code for AI
33
"""
4+
import asyncio
5+
6+
import dotenv
47
from openai import AsyncOpenAI
58
from openai.types.chat import ChatCompletionMessageParam
69

@@ -19,33 +22,39 @@
1922

2023

2124
class AiClient:
25+
"""Client for AI"""
26+
2227
def __init__(self):
28+
"""Initialize the client"""
2329
self.client = AsyncOpenAI()
2430
self.model = "gpt-3.5-turbo"
2531

26-
async def completion(self, messages: list[ChatCompletionMessageParam]):
32+
async def completion(self, messages: list[ChatCompletionMessageParam]) -> str:
33+
"""Get a completion from the AI"""
2734
# [{"role": "user", "content": "Hello world"}]
2835

2936
completion = await self.client.chat.completions.create(model=self.model, messages=messages)
3037
# print(completion.model_dump_json(indent=2))
3138
# print(dict(completion).get('usage'))
3239
choice = completion.choices[0]
3340
print(choice.message.content)
34-
return completion
41+
return choice.message.content
3542

3643

3744
if __name__ == "__main__":
38-
import asyncio
3945

40-
import dotenv
46+
def run() -> None:
47+
"""Example"""
48+
49+
dotenv.load_dotenv()
4150

42-
dotenv.load_dotenv()
51+
async def main():
52+
client = AiClient()
53+
content = PROLOGUE + "Tell me about edlin."
54+
ask = ChatCompletionMessageParam(content=content, role="user")
55+
await client.completion([ask])
4356

44-
async def main():
45-
client = AiClient()
46-
content = PROLOGUE + "Tell me about edlin."
47-
ask = ChatCompletionMessageParam(content=content, role="user")
48-
await client.completion([ask])
57+
# Python 3.7+
58+
asyncio.run(main())
4959

50-
# Python 3.7+
51-
asyncio.run(main())
60+
run()

dedlin/basic_types.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,19 @@ class Printable(Protocol):
296296
"""Something that acts like print()"""
297297

298298
def __call__(self, text: Optional[str], end: str = "\n") -> None:
299-
...
299+
"""Signature of a printable"""
300300

301301

302-
def null_printer(text: str, end: str = "") -> None:
303-
"""
304-
Do nothing implementation of Printable
305-
"""
302+
class NullPrinter:
303+
"""Something that acts like print()"""
304+
305+
def __call__(self, text: Optional[str], end: str = "\n") -> None:
306+
"""
307+
Do nothing implementation of Printable
308+
"""
309+
310+
311+
# def null_printer(text: str, end: str = "") -> None:
306312

307313

308314
@runtime_checkable
@@ -317,7 +323,6 @@ def generate(
317323
self,
318324
) -> Generator[Command, None, None]:
319325
"""Generate commands"""
320-
...
321326

322327

323328
@runtime_checkable
@@ -327,6 +332,8 @@ class StringGeneratorProtocol(Protocol):
327332
prompt: str
328333
default: str
329334

335+
# current_line:int # will we need this?
336+
330337
def generate(
331338
self,
332339
) -> Generator[str, None, None]:

dedlin/command_sources.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ def questionary_command_handler(prompt: str = "*") -> Generator[str, None, None]
8383
yield answer
8484

8585

86+
class CommandGeneratorProtocol:
87+
def __init__(self, path: Path):
88+
"""Initialize the generator"""
89+
self.current_line: int = 0
90+
self.document_length: int = 0
91+
self.macro_path: Path = path
92+
self.prompt: str = "> "
93+
94+
def generate(
95+
self,
96+
) -> Generator[Command, None, None]:
97+
"""Turn a file into a bunch of commands"""
98+
99+
86100
class CommandGenerator:
87101
"""Get a typed command from a file"""
88102

dedlin/document.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def edit(self, line_number: int) -> EditStatus:
263263
# end up with a reference to a static, past state of the prompt and line number
264264
input_generator = self.edit_inputter.generate()
265265
try:
266-
self.edit_inputter.current_line = f" {line_number} : "
266+
# does this next line have any impact? If so, all inputters need this property.
267+
# self.edit_inputter.current_line = f" {line_number} : "
267268
self.edit_inputter.default = line_text
268269
new_line = next(input_generator)
269270
except StopIteration:
@@ -339,7 +340,7 @@ def insert(
339340
self.current_line = line_number
340341
line_number += 1
341342
logger.debug(f"Inserted at {line_number}")
342-
return Phrases(accumulated_lines)
343+
return Phrases(tuple(accumulated_lines))
343344

344345
def lorem(self, line_range: Optional[LineRange]) -> None:
345346
"""Add lorem ipsum to lines"""
@@ -400,3 +401,13 @@ def backup(self) -> None:
400401
# TODO: call a mutator method instead of assigning to self.previous_lines
401402
self.previous_lines = self.lines.copy()
402403
self.previous_current_line = self.current_line
404+
405+
def print(self, line_range: Optional[LineRange]) -> Generator[tuple[str, str], None, None]:
406+
"""For handing lines off to a print() function"""
407+
for line in self.lines[line_range.start - 1 : line_range.end]:
408+
if line.endswith("\n"):
409+
line = line[:-1]
410+
end = "\n"
411+
else:
412+
end = ""
413+
yield line, end

dedlin/document_sources.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import win32console
2020

2121
PROBABLY_WINDOWS = True
22-
STANDARD_IN = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
2322

2423

2524
# alternate windows: https://stackoverflow.com/a/11616477
2625
if PROBABLY_WINDOWS:
26+
STANDARD_IN_WINDOWS = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
2727

2828
def input_with_prefill(prompt: str, default: str = "") -> str:
2929
"""Show prompt and prefill input text with default value.
@@ -39,20 +39,20 @@ def input_with_prefill(prompt: str, default: str = "") -> str:
3939
evt.KeyDown = True
4040
keys.append(evt)
4141

42-
STANDARD_IN.WriteConsoleInput(keys)
42+
STANDARD_IN_WINDOWS.WriteConsoleInput(keys)
4343
return input(prompt)
4444

4545
else:
4646

47-
def input_with_prefill(prompt: str, text: str = "") -> str:
47+
def input_with_prefill(prompt: str, default: str = "") -> str:
4848
"""Show prompt and prefill input text with default value.
4949
5050
Linux/Mac Version
5151
"""
5252

5353
# ref https://stackoverflow.com/a/8505387
5454
def hook() -> None:
55-
readline.insert_text(text)
55+
readline.insert_text(default)
5656
readline.redisplay()
5757

5858
readline.set_pre_input_hook(hook)

dedlin/file_converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def write_to_markdown(filename: str, lines: list[str]) -> str:
2020
path = Path(filename)
2121
if path.suffix == ".md":
2222
path.rename(path.with_suffix(".html"))
23-
path.write_text(html_text)
23+
path.write_text(html_text, encoding="utf-8")
2424
else:
2525
print("Not markdown!")
2626
return html_text

dedlin/file_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dedlin.tools.export import export_markdown
1010

1111

12-
def read_or_create_file(path: Path) -> list[str]:
12+
def read_or_create_file(path: Optional[Path]) -> list[str]:
1313
"""Attempt to read file, create if it doesn't exist"""
1414

1515
if path:
@@ -26,7 +26,7 @@ def read_file(path: Optional[Path]) -> list[str]:
2626
"""Read a file and return a list_doc of lines"""
2727
lines: list[str] = []
2828

29-
with open(path, encoding="utf-8") as file:
29+
with open(str(path), encoding="utf-8") as file:
3030
for line in file:
3131
if line.endswith("\n"):
3232
lines.append(line[:-1])

dedlin/history_feature.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Manage history on file system
33
"""
44
from pathlib import Path
5+
from typing import Optional
56

67
from dedlin.utils.file_utils import locate_file
78

@@ -20,9 +21,11 @@ def __init__(self, persist: bool = True) -> None:
2021
"""Initialize the history log"""
2122
self.persist = persist
2223
if self.persist:
23-
self.history_file = self.initialize_history_folder() / self.make_sequential_history_file_name()
24+
self.history_file: Optional[Path] = (
25+
self.initialize_history_folder() / self.make_sequential_history_file_name()
26+
)
2427
else:
25-
self.history_file = None
28+
self.history_file: Optional[Path] = None
2629

2730
def initialize_history_folder(self) -> Path:
2831
"""

0 commit comments

Comments
 (0)