|
7 | 7 | namespace. Blocks reassignment of ``pdv_tree`` and ``pdv``. |
8 | 8 |
|
9 | 9 | - :class:`PDVApp`: the ``pdv`` object injected into the namespace. |
10 | | - Exposes ``pdv.save()``, ``pdv.help()`` to users. |
| 10 | + Exposes ``pdv.save()``, ``pdv.save_project()``, |
| 11 | + ``pdv.save_project_as()``, ``pdv.open_project()``, |
| 12 | + ``pdv.help()`` to users. |
11 | 13 |
|
12 | 14 | - :func:`pdv_namespace`: returns a snapshot of the current kernel |
13 | 15 | namespace for display in the Namespace panel, excluding PDV internals |
@@ -127,16 +129,87 @@ def working_dir(self) -> Path: |
127 | 129 | def save(self) -> None: |
128 | 130 | """Trigger a project save. Equivalent to File -> Save in the UI. |
129 | 131 |
|
130 | | - Sends a ``pdv.project.save`` comm message to the app. The app |
| 132 | + Sends a ``pdv.project.save_request`` push to the app. The app |
131 | 133 | will prompt for a save location if no project is currently open. |
132 | 134 | """ |
133 | 135 | try: |
134 | 136 | from pdv.comms import send_message # noqa: PLC0415 |
135 | 137 |
|
136 | | - send_message("pdv.project.save", {}) |
| 138 | + send_message("pdv.project.save_request", {}) |
137 | 139 | except RuntimeError: |
138 | 140 | print("PDV: No comm channel open. Cannot trigger save.") |
139 | 141 |
|
| 142 | + def save_project(self, path: str | None = None) -> None: |
| 143 | + """Save the current project to a directory. |
| 144 | +
|
| 145 | + Sends a request to the app to perform a full project save |
| 146 | + (tree serialization, code cells, manifest). Equivalent to |
| 147 | + File -> Save with an explicit directory. |
| 148 | +
|
| 149 | + Parameters |
| 150 | + ---------- |
| 151 | + path : str or None |
| 152 | + Absolute or ``~``-prefixed path to the project directory. |
| 153 | + If None, saves to the current project location (equivalent |
| 154 | + to :meth:`save`). |
| 155 | + """ |
| 156 | + try: |
| 157 | + import os # noqa: PLC0415 |
| 158 | + |
| 159 | + from pdv.comms import send_message # noqa: PLC0415 |
| 160 | + |
| 161 | + if path is not None: |
| 162 | + resolved = os.path.realpath(os.path.expanduser(path)) |
| 163 | + send_message("pdv.project.save_request", {"save_dir": resolved}) |
| 164 | + else: |
| 165 | + send_message("pdv.project.save_request", {}) |
| 166 | + except RuntimeError: |
| 167 | + print("PDV: No comm channel open. Cannot trigger save.") |
| 168 | + |
| 169 | + def save_project_as(self, path: str) -> None: |
| 170 | + """Save the project to a new directory (Save As). |
| 171 | +
|
| 172 | + Like :meth:`save_project`, but the given path becomes the new |
| 173 | + active project directory. Equivalent to File -> Save As with |
| 174 | + an explicit directory. |
| 175 | +
|
| 176 | + Parameters |
| 177 | + ---------- |
| 178 | + path : str |
| 179 | + Absolute or ``~``-prefixed path to the new project directory. |
| 180 | + """ |
| 181 | + try: |
| 182 | + import os # noqa: PLC0415 |
| 183 | + |
| 184 | + from pdv.comms import send_message # noqa: PLC0415 |
| 185 | + |
| 186 | + resolved = os.path.realpath(os.path.expanduser(path)) |
| 187 | + send_message("pdv.project.save_as_request", {"save_dir": resolved}) |
| 188 | + except RuntimeError: |
| 189 | + print("PDV: No comm channel open. Cannot trigger save.") |
| 190 | + |
| 191 | + def open_project(self, path: str) -> None: |
| 192 | + """Open a project from a directory. |
| 193 | +
|
| 194 | + Sends a request to the app to load the project at the given |
| 195 | + path. The current tree and code cells will be replaced with |
| 196 | + the contents of the loaded project. |
| 197 | +
|
| 198 | + Parameters |
| 199 | + ---------- |
| 200 | + path : str |
| 201 | + Absolute or ``~``-prefixed path to the project directory. |
| 202 | + """ |
| 203 | + try: |
| 204 | + import os # noqa: PLC0415 |
| 205 | + |
| 206 | + from pdv.comms import send_message # noqa: PLC0415 |
| 207 | + |
| 208 | + resolved = os.path.realpath(os.path.expanduser(path)) |
| 209 | + send_message("pdv.project.open_request", {"save_dir": resolved}) |
| 210 | + except RuntimeError: |
| 211 | + print("PDV: No comm channel open. Cannot open project.") |
| 212 | + |
140 | 213 | def add_file(self, source_path: str) -> "PDVFile": |
141 | 214 | """Import an arbitrary file into the tree as a :class:`PDVFile`. |
142 | 215 |
|
@@ -261,6 +334,9 @@ def help(self, topic: str | None = None) -> None: |
261 | 334 | " pdv_tree.run_script('path') — run a script node\n" |
262 | 335 | " pdv.working_dir — Path to the session working dir (for data files)\n" |
263 | 336 | " pdv.save() — save the project\n" |
| 337 | + " pdv.save_project('path') — save project to a directory\n" |
| 338 | + " pdv.save_project_as('path') — save project to a new directory (Save As)\n" |
| 339 | + " pdv.open_project('path') — open a project from a directory\n" |
264 | 340 | " pdv.add_file('path/to/file') — import a file into the tree\n" |
265 | 341 | " pdv.new_note('path', title='My Note') — create a markdown note\n" |
266 | 342 | " pdv.help('pdv_tree') — help on a specific topic\n" |
|
0 commit comments