Skip to content

Commit

Permalink
Fix config and history wrong paths (#15)
Browse files Browse the repository at this point in the history
There were a couple of leftovers when we renamed the project from
shellai to command-line-assistant, and also, we were handling how to
create the folders and files in the wrong way.
  • Loading branch information
r0x0d authored Oct 31, 2024
1 parent 087c518 commit 3e4b01d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
4 changes: 2 additions & 2 deletions command_line_assistant/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
import os
import sys
from pathlib import Path

from command_line_assistant import utils
from command_line_assistant.config import (
CONFIG_DEFAULT_PATH,
load_config_file,
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_args():
def main():
parser, args = get_args()

config_file = utils.expand_user_path(args.config)
config_file = Path(args.config).expanduser()
config = load_config_file(config_file)

enforce_script_session = config.output.enforce_script
Expand Down
13 changes: 6 additions & 7 deletions command_line_assistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
except ImportError:
import tomli as tomllib

from command_line_assistant import utils

CONFIG_DEFAULT_PATH: Path = Path("~/.config/shellai/config.toml")
CONFIG_DEFAULT_PATH: Path = Path("~/.config/command-line-assistant/config.toml")

# tomllib does not support writting files, so we will create our own.
CONFIG_TEMPLATE = """\
Expand Down Expand Up @@ -47,10 +46,10 @@ class OutputSchema(
def __new__(
cls,
enforce_script: bool = False,
file: str = "/tmp/shellai_output.txt",
file: str = "/tmp/command-line-assistant_output.txt",
prompt_separator: str = "$",
):
file = utils.expand_user_path(file)
file = Path(file).expanduser()
return super(OutputSchema, cls).__new__(
cls, enforce_script, file, prompt_separator
)
Expand All @@ -66,10 +65,10 @@ class HistorySchema(namedtuple("History", ["enabled", "file", "max_size"])):
def __new__(
cls,
enabled: bool = True,
file: str = "~/.local/share/shellai/shellai_history.json",
file: str = "~/.local/share/command-line-assistant/command-line-assistant_history.json",
max_size: int = 100,
):
file = utils.expand_user_path(file)
file = Path(file).expanduser()
return super(HistorySchema, cls).__new__(cls, enabled, file, max_size)


Expand Down Expand Up @@ -146,7 +145,7 @@ def _read_config_file(config_file: Path) -> Config:


def load_config_file(config_file: Path) -> Config:
"""Load configuration file for shellai.
"""Load configuration file for command-line-assistant.
If the user specifies a path where no config file is located, we will create one with default values.
"""
Expand Down
4 changes: 3 additions & 1 deletion command_line_assistant/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def handle_history_write(config: Config, history: list, response: str) -> None:
return

filepath = config.history.file
filepath.makedirs(mode=0o755)

if not filepath.exists():
filepath.parent.mkdir(mode=0o755)

if response:
history.append({"role": "assistant", "content": response})
Expand Down
10 changes: 0 additions & 10 deletions command_line_assistant/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import select
import sys
from pathlib import Path


def read_stdin():
Expand All @@ -22,12 +21,3 @@ def get_payload(query: str) -> dict:
# {"role": "user", "content": "how do I enable selinux?"},
payload = {"query": query}
return payload


def expand_user_path(file_path: str) -> Path:
"""Helper method to expand user provided path."""
path = Path(file_path)
if not path.exists():
raise FileNotFoundError(f"Current file does not exist or was not found: {path}")

return Path(path).expanduser()
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# otherwise recording via script session will be enforced
enforce_script = false
# file with output(s) of regular commands (e.g. ls, echo, etc.)
file = "/tmp/shellai_output.txt"
file = "/tmp/command-line-assistant_output.txt"
# Keep non-empty if your file contains only output of commands (not prompt itself)
prompt_separator = "$"

[history]
enabled = true
file = "~/.local/share/shellai/shellai_history.json"
file = "~/.local/share/command-line-assistant/command-line-assistant_history.json"
# max number of queries in history (including responses)
max_size = 100

Expand Down

0 comments on commit 3e4b01d

Please sign in to comment.