Skip to content

Commit

Permalink
Dump pathlib to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Jun 25, 2024
1 parent a7bf127 commit 5a9c9bf
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions activity_browser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import shutil
from pathlib import Path
from typing import Optional
from typing import Optional, Any

import platformdirs
from PySide2.QtWidgets import QMessageBox
Expand All @@ -12,6 +12,13 @@
from activity_browser.mod import bw2data as bd


def pathlib_encoder(value: Any) -> Any:
if isinstance(value, Path):
return str(value)
else:
return value


class BaseSettings(object):
"""Base Class for handling JSON settings files."""

Expand Down Expand Up @@ -48,7 +55,7 @@ def load_settings(self) -> None:

def write_settings(self) -> None:
with open(self.settings_file, "w") as outfile:
json.dump(self.settings, outfile, indent=4, sort_keys=True)
json.dump(self.settings, outfile, indent=4, sort_keys=True, default=pathlib_encoder)


class ABSettings(BaseSettings):
Expand Down Expand Up @@ -92,7 +99,7 @@ def update_old_settings(directory: str, filename: str) -> None:
"startup_project": current_settings["startup_project"],
}
with open(file, "w") as new_file:
json.dump(new_settings_content, new_file)
json.dump(new_settings_content, new_file, default=pathlib_encoder)

@classmethod
def get_default_settings(cls) -> dict:
Expand Down

0 comments on commit 5a9c9bf

Please sign in to comment.