Add more typing to org|md_to_entries. Remove redundant f-string wraps

- Add type hints to improve maintainability of stabilzed indexing code
- It shouldn't be necessary to wrap string variables in an f-string

This change aims to improve code quality. It should not affect
functionality.
This commit is contained in:
Debanjum
2024-12-01 23:02:52 -08:00
parent dffdd81345
commit 47c926b0ff
2 changed files with 8 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ import re
from pathlib import Path from pathlib import Path
from typing import Dict, List, Tuple from typing import Dict, List, Tuple
import urllib3 import urllib3.util
from khoj.database.models import Entry as DbEntry from khoj.database.models import Entry as DbEntry
from khoj.database.models import KhojUser from khoj.database.models import KhojUser
@@ -51,11 +51,11 @@ class MarkdownToEntries(TextToEntries):
return num_new_embeddings, num_deleted_embeddings return num_new_embeddings, num_deleted_embeddings
@staticmethod @staticmethod
def extract_markdown_entries(markdown_files, max_tokens=256) -> Tuple[Dict, List[Entry]]: def extract_markdown_entries(markdown_files: Dict[str, str], max_tokens=256) -> Tuple[Dict[str, str], List[Entry]]:
"Extract entries by heading from specified Markdown files" "Extract entries by heading from specified Markdown files"
entries: List[str] = [] entries: List[str] = []
entry_to_file_map: List[Tuple[str, str]] = [] entry_to_file_map: List[Tuple[str, str]] = []
file_to_text_map = dict() file_to_text_map: Dict[str, str] = dict()
for markdown_file in markdown_files: for markdown_file in markdown_files:
try: try:
markdown_content = markdown_files[markdown_file] markdown_content = markdown_files[markdown_file]
@@ -128,7 +128,7 @@ class MarkdownToEntries(TextToEntries):
return entries, entry_to_file_map return entries, entry_to_file_map
@staticmethod @staticmethod
def convert_markdown_entries_to_maps(parsed_entries: List[str], entry_to_file_map) -> List[Entry]: def convert_markdown_entries_to_maps(parsed_entries: List[str], entry_to_file_map: Dict[str, str]) -> List[Entry]:
"Convert each Markdown entries into a dictionary" "Convert each Markdown entries into a dictionary"
entries: List[Entry] = [] entries: List[Entry] = []
for parsed_entry in parsed_entries: for parsed_entry in parsed_entries:
@@ -151,7 +151,7 @@ class MarkdownToEntries(TextToEntries):
compiled=compiled_entry, compiled=compiled_entry,
raw=parsed_entry, raw=parsed_entry,
heading=f"{prefix}{heading}", heading=f"{prefix}{heading}",
file=f"{entry_filename}", file=entry_filename,
) )
) )

View File

@@ -208,7 +208,7 @@ class OrgToEntries(TextToEntries):
compiled += f"\n {parsed_entry.body}" compiled += f"\n {parsed_entry.body}"
# Add the sub-entry contents to the entry # Add the sub-entry contents to the entry
entry_compiled += f"{compiled}" entry_compiled += compiled
entry_raw += f"{parsed_entry}" entry_raw += f"{parsed_entry}"
if not entry_heading: if not entry_heading:
entry_heading = heading entry_heading = heading
@@ -218,8 +218,8 @@ class OrgToEntries(TextToEntries):
Entry( Entry(
compiled=entry_compiled, compiled=entry_compiled,
raw=entry_raw, raw=entry_raw,
heading=f"{entry_heading}", heading=entry_heading,
file=f"{entry_to_file_map[parsed_entry]}", file=entry_to_file_map[parsed_entry],
) )
) )