Skip to content

Commit

Permalink
Adds ability to write to json file
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-russell committed Jul 26, 2024
1 parent e26d097 commit c065f2c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions fractal/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import re
from getpass import getpass
Expand All @@ -14,21 +15,26 @@ class InvalidMatrixIdException(Exception):
pass


def write_user_data(data: Dict[str, Any], filename: str) -> None:
def write_user_data(data: Dict[str, Any], filename: str, format: str = "yaml") -> str:
"""
Write data to yaml file <filename> in user's appdir (ie ~/.local/share/fractal)
"""
makedirs(data_dir, exist_ok=True)

try:
data_to_write = yaml.dump(data)
except yaml.YAMLError as error:
raise error
match format:
case "yaml":
data_to_write = yaml.dump(data)
case "json":
data_to_write = json.dumps(data)
case _:
raise ValueError(f"Invalid format: {format}")

user_data = os.path.join(data_dir, filename)
with open(user_data, "w") as file:
data_file = os.path.join(data_dir, filename)
with open(data_file, "w") as file:
file.write(data_to_write)

return data_file


def read_user_data(filename: str) -> Tuple[Dict[str, Any], str]:
"""
Expand Down

0 comments on commit c065f2c

Please sign in to comment.