defined 'ConfigType' for configuration file type hinting
This commit is contained in:
parent
056bf4c6b5
commit
bc5e6228a6
@ -7,7 +7,7 @@ import sys
|
||||
import argcomplete
|
||||
import argparse
|
||||
import pathlib
|
||||
from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency
|
||||
from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType
|
||||
from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data
|
||||
from .api_client import ai, openai_api_key, print_models
|
||||
from itertools import zip_longest
|
||||
@ -28,7 +28,7 @@ def read_config(path: str):
|
||||
|
||||
|
||||
def create_question_with_hist(args: argparse.Namespace,
|
||||
config: dict,
|
||||
config: ConfigType,
|
||||
) -> tuple[list[dict[str, str]], str, list[str]]:
|
||||
"""
|
||||
Creates the "SI request", including the question and chat history as determined
|
||||
@ -61,7 +61,7 @@ def create_question_with_hist(args: argparse.Namespace,
|
||||
return chat, full_question, tags
|
||||
|
||||
|
||||
def tag_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
def tag_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||
"""
|
||||
Handler for the 'tag' command.
|
||||
"""
|
||||
@ -69,7 +69,7 @@ def tag_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
print_tags_frequency(get_tags(config, None))
|
||||
|
||||
|
||||
def model_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
def model_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||
"""
|
||||
Handler for the 'model' command.
|
||||
"""
|
||||
@ -77,7 +77,7 @@ def model_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
print_models()
|
||||
|
||||
|
||||
def ask_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||
"""
|
||||
Handler for the 'ask' command.
|
||||
"""
|
||||
@ -90,7 +90,7 @@ def ask_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
print(f"Usage: {usage}")
|
||||
|
||||
|
||||
def hist_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
def hist_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||
"""
|
||||
Handler for the 'hist' command.
|
||||
"""
|
||||
@ -104,7 +104,7 @@ def hist_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
print_chat_hist(chat, args.dump, args.only_source_code)
|
||||
|
||||
|
||||
def print_cmd(args: argparse.Namespace, config: dict) -> None:
|
||||
def print_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||
"""
|
||||
Handler for the 'print' command.
|
||||
"""
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import yaml
|
||||
import io
|
||||
import pathlib
|
||||
from .utils import terminal_width, append_message, message_to_chat
|
||||
from .utils import terminal_width, append_message, message_to_chat, ConfigType
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
|
||||
@ -41,11 +41,11 @@ def save_answers(question: str,
|
||||
answers: list[str],
|
||||
tags: list[str],
|
||||
otags: Optional[list[str]],
|
||||
config: Dict[str, Any]
|
||||
config: ConfigType
|
||||
) -> None:
|
||||
wtags = otags or tags
|
||||
num, inum = 0, 0
|
||||
next_fname = pathlib.Path(config['db']) / '.next'
|
||||
next_fname = pathlib.Path(str(config['db'])) / '.next'
|
||||
try:
|
||||
with open(next_fname, 'r') as f:
|
||||
num = int(f.read())
|
||||
@ -66,14 +66,14 @@ def save_answers(question: str,
|
||||
def create_chat_hist(question: Optional[str],
|
||||
tags: Optional[List[str]],
|
||||
extags: Optional[List[str]],
|
||||
config: Dict[str, Any],
|
||||
config: ConfigType,
|
||||
match_all_tags: bool = False,
|
||||
with_tags: bool = False,
|
||||
with_file: bool = False
|
||||
) -> List[Dict[str, str]]:
|
||||
chat: List[Dict[str, str]] = []
|
||||
append_message(chat, 'system', config['system'].strip())
|
||||
for file in sorted(pathlib.Path(config['db']).iterdir()):
|
||||
append_message(chat, 'system', str(config['system']).strip())
|
||||
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
||||
if file.suffix == '.yaml':
|
||||
with open(file, 'r') as f:
|
||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
||||
@ -97,9 +97,9 @@ def create_chat_hist(question: Optional[str],
|
||||
return chat
|
||||
|
||||
|
||||
def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]:
|
||||
def get_tags(config: ConfigType, prefix: Optional[str]) -> List[str]:
|
||||
result = []
|
||||
for file in sorted(pathlib.Path(config['db']).iterdir()):
|
||||
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
||||
if file.suffix == '.yaml':
|
||||
with open(file, 'r') as f:
|
||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
||||
@ -116,5 +116,5 @@ def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]:
|
||||
return result
|
||||
|
||||
|
||||
def get_tags_unique(config: Dict[str, Any], prefix: Optional[str]) -> List[str]:
|
||||
def get_tags_unique(config: ConfigType, prefix: Optional[str]) -> List[str]:
|
||||
return list(set(get_tags(config, prefix)))
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import shutil
|
||||
from pprint import PrettyPrinter
|
||||
from typing import List, Dict
|
||||
from typing import List, Dict, Union
|
||||
|
||||
ConfigType = Dict[str, Union[str, Dict[str, Union[str, int]]]]
|
||||
|
||||
|
||||
def terminal_width() -> int:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user