46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import json
|
|
from dataclasses import asdict
|
|
from pathlib import Path
|
|
|
|
from pydantic_ai.providers.openai_codex import (
|
|
OpenAICodexCredentials,
|
|
OpenAICodexCredentialSource,
|
|
)
|
|
|
|
|
|
class FileCodexCredentionalSource(OpenAICodexCredentialSource):
|
|
def __init__(self,
|
|
path: str| Path,
|
|
encoding: str="utf-8",
|
|
ensure_ascii: bool=False,
|
|
indent: int=2):
|
|
super().__init__()
|
|
self._path = Path(path)
|
|
self._encoding=encoding
|
|
self._ensure_ascii=ensure_ascii
|
|
self._indent=indent
|
|
|
|
@property
|
|
def path(self):
|
|
return self._path
|
|
|
|
async def load(self) -> OpenAICodexCredentials:
|
|
return OpenAICodexCredentials(
|
|
**json.loads(
|
|
self._path.read_text(encoding=self._encoding)
|
|
)
|
|
)
|
|
|
|
async def save(self, credentials: OpenAICodexCredentials) -> None:
|
|
self.path.parent.mkdir(parents=True , exist_ok=True)
|
|
|
|
self._path.write_text(
|
|
json.dumps(
|
|
asdict(credentials),
|
|
ensure_ascii=self._ensure_ascii,
|
|
indent=self._indent
|
|
),
|
|
encoding=self._encoding
|
|
)
|
|
|
|
|