mirror of https://github.com/corca-ai/EVAL
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
"""
|
|
write protocol:
|
|
|
|
<filepath>
|
|
<content>
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from env import settings
|
|
|
|
from .verify import verify
|
|
|
|
|
|
class WriteCommand:
|
|
separator = "\n"
|
|
|
|
def __init__(self, filepath: str, content: int):
|
|
self.filepath: str = filepath
|
|
self.content: str = content
|
|
self.mode: str = "w"
|
|
|
|
def with_mode(self, mode: str) -> "WriteCommand":
|
|
self.mode = mode
|
|
return self
|
|
|
|
@verify
|
|
def execute(self) -> str:
|
|
with open(self.filepath, self.mode) as f:
|
|
f.write(self.content)
|
|
return self.content
|
|
|
|
@staticmethod
|
|
def from_str(command: str) -> "WriteCommand":
|
|
filepath = command.split(WriteCommand.separator)[0]
|
|
return WriteCommand(filepath, command[len(filepath) + 1 :])
|
|
|
|
|
|
class CodeWriter:
|
|
@staticmethod
|
|
def write(command: str) -> str:
|
|
return WriteCommand.from_str(command).with_mode("w").execute()
|
|
|
|
@staticmethod
|
|
def append(command: str) -> str:
|
|
return WriteCommand.from_str(command).with_mode("a").execute()
|