|  | 
|  | 1 | +# TODO: barret - Set / Load SaveState for Connect. Ex: Connect https://github.com/posit-dev/connect/blob/8de330aec6a61cf21e160b5081d08a1d3d7e8129/R/connect.R#L915 | 
|  | 2 | + | 
|  | 3 | +import os | 
|  | 4 | +from abc import ABC, abstractmethod | 
|  | 5 | +from pathlib import Path | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +class SaveState(ABC): | 
|  | 9 | +    """ | 
|  | 10 | +    Class for saving and restoring state to/from disk. | 
|  | 11 | +    """ | 
|  | 12 | + | 
|  | 13 | +    @abstractmethod | 
|  | 14 | +    async def save_dir( | 
|  | 15 | +        self, | 
|  | 16 | +        id: str, | 
|  | 17 | +        # write_files: Callable[[Path], Awaitable[None]], | 
|  | 18 | +    ) -> Path: | 
|  | 19 | +        """ | 
|  | 20 | +        Construct directory for saving state. | 
|  | 21 | +
 | 
|  | 22 | +        Parameters | 
|  | 23 | +        ---------- | 
|  | 24 | +        id | 
|  | 25 | +            The unique identifier for the state. | 
|  | 26 | +
 | 
|  | 27 | +        Returns | 
|  | 28 | +        ------- | 
|  | 29 | +        Path | 
|  | 30 | +            Directory location for saving state. This directory must exist. | 
|  | 31 | +        """ | 
|  | 32 | +        # write_files | 
|  | 33 | +        #     A async function that writes the state to a serializable location. The method receives a path object and | 
|  | 34 | +        ... | 
|  | 35 | + | 
|  | 36 | +    @abstractmethod | 
|  | 37 | +    async def load_dir( | 
|  | 38 | +        self, | 
|  | 39 | +        id: str, | 
|  | 40 | +        # read_files: Callable[[Path], Awaitable[None]], | 
|  | 41 | +    ) -> Path: | 
|  | 42 | +        """ | 
|  | 43 | +        Construct directory for loading state. | 
|  | 44 | +
 | 
|  | 45 | +        Parameters | 
|  | 46 | +        ---------- | 
|  | 47 | +        id | 
|  | 48 | +            The unique identifier for the state. | 
|  | 49 | +
 | 
|  | 50 | +        Returns | 
|  | 51 | +        ------- | 
|  | 52 | +        Path | None | 
|  | 53 | +            Directory location for loading state. If `None`, state loading will be ignored. If a `Path`, the directory must exist. | 
|  | 54 | +        """ | 
|  | 55 | +        ... | 
|  | 56 | + | 
|  | 57 | + | 
|  | 58 | +class SaveStateLocal(SaveState): | 
|  | 59 | +    """ | 
|  | 60 | +    Function wrappers for saving and restoring state to/from disk when running Shiny | 
|  | 61 | +    locally. | 
|  | 62 | +    """ | 
|  | 63 | + | 
|  | 64 | +    def _local_dir(self, id: str) -> Path: | 
|  | 65 | +        # Try to save/load from current working directory as we do not know where the | 
|  | 66 | +        # app file is located | 
|  | 67 | +        return Path(os.getcwd()) / "shiny_bookmarks" / id | 
|  | 68 | + | 
|  | 69 | +    async def save_dir(self, id: str) -> Path: | 
|  | 70 | +        state_dir = self._local_dir(id) | 
|  | 71 | +        if not state_dir.exists(): | 
|  | 72 | +            state_dir.mkdir(parents=True) | 
|  | 73 | +        return state_dir | 
|  | 74 | + | 
|  | 75 | +    async def load_dir(self, id: str) -> Path: | 
|  | 76 | +        return self._local_dir(id) | 
|  | 77 | + | 
|  | 78 | +    # async def save( | 
|  | 79 | +    #     self, | 
|  | 80 | +    #     id: str, | 
|  | 81 | +    #     write_files: Callable[[Path], Awaitable[None]], | 
|  | 82 | +    # ) -> None: | 
|  | 83 | +    #     state_dir = self._local_dir(id) | 
|  | 84 | +    #     if not state_dir.exists(): | 
|  | 85 | +    #         state_dir.mkdir(parents=True) | 
|  | 86 | + | 
|  | 87 | +    #     await write_files(state_dir) | 
|  | 88 | + | 
|  | 89 | +    # async def load( | 
|  | 90 | +    #     self, | 
|  | 91 | +    #     id: str, | 
|  | 92 | +    #     read_files: Callable[[Path], Awaitable[None]], | 
|  | 93 | +    # ) -> None: | 
|  | 94 | +    #     await read_files(self._local_dir(id)) | 
|  | 95 | +    #     await read_files(self._local_dir(id)) | 
0 commit comments