|
8 | 8 | import sys |
9 | 9 | from importlib import import_module |
10 | 10 | from pathlib import Path |
11 | | -from typing import Any, Dict, List, Optional |
| 11 | +from typing import Any, Dict, List, Optional, Tuple |
12 | 12 |
|
13 | 13 | import nbformat |
14 | 14 | from nbdev.export import nb_export |
@@ -117,41 +117,23 @@ def __extract_user_defined_imports(self, notebook_path) -> List[str]: |
117 | 117 |
|
118 | 118 | return list(user_imports) |
119 | 119 |
|
120 | | - def _is_user_defined_module(self, module_name: str, notebook_path: Path) -> bool: |
121 | | - """ |
122 | | - Check if a module is user-defined |
123 | | -
|
124 | | - Args: |
125 | | - notebook_path: Path to Jupyter notebook. |
126 | | - """ |
127 | | - notebook_dir = notebook_path.parent |
128 | | - module_path = notebook_dir / f"{module_name}.py" |
129 | | - |
130 | | - module_dir = notebook_dir / module_name |
131 | | - |
132 | | - if (module_path.exists() and module_path.is_file()) or module_dir.exists(): |
133 | | - return True |
134 | | - |
135 | | - return False |
136 | | - |
137 | 120 | def __copy_user_defined_modules(self, module_names: List[str], notebook_path: Path) -> None: |
138 | 121 | """ |
139 | 122 | Copies user-defined modules/packages to the workspace's src directory |
140 | 123 |
|
141 | 124 | Args: |
142 | | - module_name: List of module name to copy. |
| 125 | + module_names: List of module names. |
143 | 126 | notebook_path: Path to Jupyter notebook. |
144 | 127 | """ |
145 | 128 | src_dir = self.script_path.parent |
146 | 129 | for module_name in module_names: |
147 | | - module_file = notebook_path.parent / f"{module_name}.py" |
148 | | - module_dir = notebook_path.parent / module_name |
149 | | - if module_file.exists() and module_file.is_file(): |
150 | | - shutil.copy(module_file, src_dir) |
151 | | - print(f"Copied used-defined module: {module_name}.py") |
| 130 | + module_path, module_dir = self._get_module_paths(module_name, notebook_path) |
| 131 | + if module_path.exists() and module_path.is_file(): |
| 132 | + shutil.copy(module_path, src_dir) |
| 133 | + print(f"Copied user-defined module: {module_name}.py") |
152 | 134 | elif module_dir.exists() and module_dir.is_dir(): |
153 | 135 | shutil.copytree(module_dir, src_dir / module_name, dirs_exist_ok=True) |
154 | | - print(f"Copied used-defined directory: {module_name}/") |
| 136 | + print(f"Copied user-defined directory: {module_name}/") |
155 | 137 |
|
156 | 138 | def __modify_experiment_script(self) -> None: |
157 | 139 | """Modifies the given python script by commenting out following code: |
@@ -361,6 +343,34 @@ def _clean_value(self, value: str) -> str: |
361 | 343 | value = value.lstrip("[").rstrip("]") |
362 | 344 | return value |
363 | 345 |
|
| 346 | + def _is_user_defined_module(self, module_name: str, notebook_path: Path) -> bool: |
| 347 | + """ |
| 348 | + Check if a module is user-defined |
| 349 | +
|
| 350 | + Args: |
| 351 | + module_name: Name of the module. |
| 352 | + notebook_path: Path to Jupyter notebook. |
| 353 | + """ |
| 354 | + if not isinstance(module_name, str) or not module_name.strip(): |
| 355 | + return False |
| 356 | + |
| 357 | + module_path, module_dir = self._get_module_paths(module_name, notebook_path) |
| 358 | + |
| 359 | + return (module_path.exists() and module_path.is_file()) or module_dir.exists() |
| 360 | + |
| 361 | + def _get_module_paths(self, module_name: str, notebook_path: Path) -> Tuple: |
| 362 | + """ |
| 363 | + Get the file and directory paths for a user-defined module |
| 364 | +
|
| 365 | + Args: |
| 366 | + module_name: Name of the module. |
| 367 | + notebook_path: Path to the Jupyter notebook. |
| 368 | + """ |
| 369 | + notebook_dir = notebook_path.parent |
| 370 | + module_path = notebook_dir / f"{module_name}.py" |
| 371 | + module_dir = notebook_dir / module_name |
| 372 | + return module_path, module_dir |
| 373 | + |
364 | 374 | def _get_requirements(self) -> List[str]: |
365 | 375 | """Extract pip libraries from the script |
366 | 376 |
|
|
0 commit comments