Spaces:
Running
Running
File size: 1,480 Bytes
cb5b71d dc92053 cb5b71d fe3ba5f 8c11dd4 cb5b71d 8c11dd4 bbea1cc cb5b71d fe3ba5f 636f5d2 cb5b71d dc92053 fe3ba5f 6a31b9a 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 42 43 44 45 46 47 48 |
import logging
import pickle
from etils import epath
import streamlit as st
from core.constants import PAST_PROJECTS_PATH
from core.query_params import set_project
from core.state import CurrentProject
from core.state import FileObject
from core.state import get_user
from core.state import Metadata
def load_past_projects_paths() -> list[epath.Path]:
user = get_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: 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)
set_project(project)
# FileObjects should have a folder.
for resource in metadata.distribution:
if isinstance(resource, FileObject):
resource.folder = project.path
try:
pickled = pickle.dumps(metadata)
_pickle_file(project.path).write_bytes(pickled)
except pickle.PicklingError as e:
logging.error("Could not pickle metadata.", exc_info=True)
def open_project(path: epath.Path) -> Metadata:
with _pickle_file(path).open("rb") as file:
return pickle.load(file)
|