Skip to content

Commit

Permalink
fix: (setting.py) save default setting file without locking resource
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeikang committed Jun 21, 2023
1 parent 64b9af2 commit c35a630
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/ai_cli/setting.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import json
import logging
from pathlib import Path

from rich.console import Console
from rich.table import Table

from ai_cli import CONFIG_DIR

logger = logging.getLogger("ai_cli")

console = Console()

setting_file = CONFIG_DIR / "setting.json"
Expand Down Expand Up @@ -98,12 +101,19 @@ def read_setting() -> Setting:
return Setting()


def save_setting(setting: Setting):
with setting_file.open("w") as f:
json.dump(setting.__dict__(), f, ensure_ascii=False, indent=2)


setting: Setting = Setting()


if not setting_file.exists():
setting_file.touch()
json.dump(dict(setting), setting_file.open("w"), ensure_ascii=False, indent=2)
# save default setting
logger.info("Save default setting, to %s", setting_file)
save_setting(setting)
else:
setting = read_setting()

Expand All @@ -124,11 +134,6 @@ def view_setting():
console.print(table)


def save_setting(setting: Setting):
with setting_file.open("w") as f:
json.dump(setting.__dict__(), f, ensure_ascii=False, indent=2)


def set_setting(k, v):
global setting
if not hasattr(setting, k):
Expand Down

0 comments on commit c35a630

Please sign in to comment.