Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
enciyo committed Jun 9, 2024
1 parent e600948 commit 7dc8566
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions che/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

from che.intercept.conversation import Conversation

__AUTHOR_NAME = subprocess.run(["git", "config", "user.name"], capture_output=True).stdout.decode("utf-8").strip()
__SAMPLE_MD = os.path.join(os.path.dirname(__file__), "sample.md")
__CURRENT_BRANCH = subprocess.run(["git", "branch", "--show-current"], capture_output=True).stdout.decode(
"utf-8").strip().replace("/", "-")
__PREF_DIR = "ai/copilot"


def __author_name():
return subprocess.run(["git", "config", "user.name"], capture_output=True).stdout.decode("utf-8").strip()


def __current_branch():
return subprocess.run(["git", "branch", "--show-current"], capture_output=True).stdout.decode(
"utf-8").strip().replace("/", "-")


def create_or_open_file(file_path, mode):
directory, filename = os.path.split(file_path)
if not os.path.exists(directory):
Expand All @@ -33,31 +39,31 @@ def create_md_block_from_sample(conversation: Conversation) -> str:
with create_or_open_file(__SAMPLE_MD, "r") as f:
block = f.readlines()
block = "".join(block)
block = block.replace("{{author_name}}", __AUTHOR_NAME)
block = block.replace("{{author_name}}", __author_name())
block = block.replace("{{prompt}}", conversation.prompt)
block = block.replace("{{answer}}", conversation.answer)
block = block.replace("{{rating}}", conversation.rating)
return block


def create_md_file(conversations: dict):
output_file = os.path.join(get_output_dir(), __CURRENT_BRANCH + ".md")
block = "# " + __CURRENT_BRANCH + "\n\n"
output_file = os.path.join(get_output_dir(), __current_branch() + ".md")
block = "# " + __current_branch() + "\n\n"
for c in conversations:
block += create_md_block_from_sample(Conversation.from_dict(c))
with create_or_open_file(output_file, "w") as f:
f.write(block)


def get_json_files():
output_file = os.path.join(get_output_dir(), __CURRENT_BRANCH + ".json")
output_file = os.path.join(get_output_dir(), __current_branch() + ".json")
with create_or_open_file(output_file, "r") as f:
conversations = json.load(f)
return conversations


def write_to_json_file(conversations: dict):
output_file = os.path.join(get_output_dir(), __CURRENT_BRANCH + ".json")
output_file = os.path.join(get_output_dir(), __current_branch() + ".json")
with create_or_open_file(output_file, "w") as f:
json.dump(conversations, f, indent=2, default=str, ensure_ascii=False)

Expand All @@ -81,4 +87,3 @@ def save_config(config):
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path, "w") as f:
json.dump(config, f, indent=2, default=str, ensure_ascii=False)

0 comments on commit 7dc8566

Please sign in to comment.