|
12 | 12 | # See LICENSE.rst for license information. |
13 | 13 | # |
14 | 14 | ############################################################################## |
15 | | - |
| 15 | +import shutil |
16 | 16 | from importlib.resources import as_file |
17 | 17 | from pathlib import Path |
18 | 18 | from typing import List, Union |
@@ -149,8 +149,71 @@ def copy_examples( |
149 | 149 | Target directory to copy examples into. Defaults to current |
150 | 150 | working directory. |
151 | 151 | """ |
| 152 | + self._target_dir = target_dir.resolve() if target_dir else Path.cwd() |
| 153 | + |
| 154 | + if "all" in examples_to_copy: |
| 155 | + self._copy_all() |
| 156 | + return |
| 157 | + |
| 158 | + for item in examples_to_copy: |
| 159 | + if item in self.available_examples(): |
| 160 | + self._copy_pack(item) |
| 161 | + elif self._is_example_name(item): |
| 162 | + self._copy_example(item) |
| 163 | + else: |
| 164 | + self._not_found_error(item) |
| 165 | + del self._target_dir |
152 | 166 | return |
153 | 167 |
|
| 168 | + def _copy_all(self): |
| 169 | + """Copy all packs and examples.""" |
| 170 | + for pack_name in self.available_examples(): |
| 171 | + self._copy_pack(pack_name) |
| 172 | + |
| 173 | + def _copy_pack(self, pack_name): |
| 174 | + """Copy all examples in a single pack.""" |
| 175 | + examples = self.available_examples().get(pack_name, []) |
| 176 | + for ex_name, ex_path in examples: |
| 177 | + self._copy_tree_to_target(pack_name, ex_name, ex_path) |
| 178 | + |
| 179 | + def _copy_example(self, example_name): |
| 180 | + """Copy a single example by its name.""" |
| 181 | + example_found = False |
| 182 | + for pack_name, examples in self.available_examples().items(): |
| 183 | + for ex_name, ex_path in examples: |
| 184 | + if ex_name == example_name: |
| 185 | + self._copy_tree_to_target(pack_name, ex_name, ex_path) |
| 186 | + example_found = True |
| 187 | + if not example_found: |
| 188 | + self._not_found_error(example_name) |
| 189 | + |
| 190 | + def _is_example_name(self, name): |
| 191 | + """Return True if the given name matches any known example.""" |
| 192 | + for pack_name, examples in self.available_examples().items(): |
| 193 | + for example_name, _ in examples: |
| 194 | + if example_name == name: |
| 195 | + return True |
| 196 | + return False |
| 197 | + |
| 198 | + def _copy_tree_to_target(self, pack_name, example_name, src_path): |
| 199 | + """Helper to handle the actual filesystem copy.""" |
| 200 | + dest_dir = self._target_dir / pack_name / example_name |
| 201 | + if dest_dir.exists(): |
| 202 | + plog.warning( |
| 203 | + f"Example directory(ies): '{dest_dir.stem}' already exist. " |
| 204 | + " Current versions of existing files have " |
| 205 | + "been left unchanged. To overwrite, please rerun " |
| 206 | + "and specify --force." |
| 207 | + ) |
| 208 | + return |
| 209 | + dest_dir.parent.mkdir(parents=True, exist_ok=True) |
| 210 | + shutil.copytree(src_path, dest_dir) |
| 211 | + |
| 212 | + def _not_found_error(self, name): |
| 213 | + raise FileNotFoundError( |
| 214 | + f"No examples or packs found for input: '{name}'" |
| 215 | + ) |
| 216 | + |
154 | 217 | def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path: |
155 | 218 | """Resolve a pack identifier to an absolute .txt path. |
156 | 219 |
|
|
0 commit comments