file_settings_store.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. import json
  3. from dataclasses import dataclass
  4. from openhands.core.config.app_config import AppConfig
  5. from openhands.server.settings import Settings
  6. from openhands.storage import get_file_store
  7. from openhands.storage.files import FileStore
  8. from openhands.storage.settings_store import SettingsStore
  9. from openhands.utils.async_utils import call_sync_from_async
  10. @dataclass
  11. class FileSettingsStore(SettingsStore):
  12. file_store: FileStore
  13. path: str = 'settings.json'
  14. async def load(self) -> Settings | None:
  15. try:
  16. json_str = await call_sync_from_async(self.file_store.read, self.path)
  17. kwargs = json.loads(json_str)
  18. settings = Settings(**kwargs)
  19. return settings
  20. except FileNotFoundError:
  21. return None
  22. async def store(self, settings: Settings):
  23. json_str = json.dumps(settings.__dict__)
  24. await call_sync_from_async(self.file_store.write, self.path, json_str)
  25. @classmethod
  26. async def get_instance(cls, config: AppConfig, token: str | None):
  27. file_store = get_file_store(config.file_store, config.file_store_path)
  28. return FileSettingsStore(file_store)