file_settings_store.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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. @dataclass
  10. class FileSettingsStore(SettingsStore):
  11. file_store: FileStore
  12. path: str = 'settings.json'
  13. async def load(self) -> Settings | None:
  14. try:
  15. json_str = self.file_store.read(self.path)
  16. kwargs = json.loads(json_str)
  17. settings = Settings(**kwargs)
  18. return settings
  19. except FileNotFoundError:
  20. return None
  21. async def store(self, settings: Settings):
  22. json_str = json.dumps(settings.__dict__)
  23. self.file_store.write(self.path, json_str)
  24. @classmethod
  25. async def get_instance(cls, config: AppConfig, token: str | None):
  26. file_store = get_file_store(config.file_store, config.file_store_path)
  27. return FileSettingsStore(file_store)