File size: 1,203 Bytes
cb5b71d
 
 
 
 
 
 
 
041af8a
cb5b71d
 
 
 
041af8a
bbea1cc
 
 
cb5b71d
 
 
 
 
 
 
 
636f5d2
 
 
 
cb5b71d
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import logging
import pickle

from etils import epath
import streamlit as st

from core.constants import PAST_PROJECTS_PATH
from core.state import CurrentProject
from core.state import get_cached_user
from core.state import Metadata


def load_past_projects_paths() -> list[epath.Path]:
    user = get_cached_user()
    past_projects_path = PAST_PROJECTS_PATH(user)
    past_projects_path.mkdir(parents=True, exist_ok=True)
    return sorted(list(past_projects_path.iterdir()), reverse=True)


def _pickle_file(path: epath.Path) -> epath.Path:
    return path / ".metadata.pkl"


def save_current_project():
    metadata = st.session_state[Metadata]
    project = st.session_state.get(CurrentProject)
    if not project:
        project = CurrentProject.create_new()
        st.session_state[CurrentProject] = project
    project.path.mkdir(parents=True, exist_ok=True)
    with _pickle_file(project.path).open("wb") as file:
        try:
            pickle.dump(metadata, file)
        except pickle.PicklingError:
            logging.error("Could not pickle metadata.")


def open_project(path: epath.Path) -> Metadata:
    with _pickle_file(path).open("rb") as file:
        return pickle.load(file)