|
22 | 22 |
|
23 | 23 | import logging |
24 | 24 | from pathlib import Path |
| 25 | +import py_compile |
25 | 26 | import shutil |
26 | 27 | from typing import Tuple, Union |
27 | 28 |
|
|
38 | 39 | get_library_path, |
39 | 40 | get_refentry, |
40 | 41 | import_handler, |
| 42 | + is_valid_method, |
41 | 43 | ) |
42 | 44 | import regex as re |
43 | 45 | from tqdm import tqdm |
@@ -236,7 +238,7 @@ def write_global__init__file(library_path: Path, config_path: Path) -> None: |
236 | 238 |
|
237 | 239 | init_path = init_folder / "__init__.py" |
238 | 240 |
|
239 | | - with open(init_path, "w") as fid: |
| 241 | + with open(init_path, "w", encoding="utf-8") as fid: |
240 | 242 | fid.write(f"from .{initial_imports} import (\n") |
241 | 243 | for dir in library_path.iterdir(): |
242 | 244 | if dir.is_dir(): |
@@ -265,7 +267,7 @@ def write__init__file(library_path: Path) -> None: |
265 | 267 | if dir.is_dir(): |
266 | 268 | listdir = list(dir.iterdir()) |
267 | 269 | if len(listdir) > 0: |
268 | | - with open(dir / "__init__.py", "w") as fid: |
| 270 | + with open(dir / "__init__.py", "w", encoding="utf-8") as fid: |
269 | 271 | fid.write(f"from . import (\n") |
270 | 272 | for file in listdir: |
271 | 273 | if file.name.endswith(".py"): |
@@ -415,15 +417,15 @@ def write_source( |
415 | 417 | python_name = name_map[initial_command_name] |
416 | 418 | path = library_path / f"{python_name}.py" |
417 | 419 | python_method = command_obj.to_python(custom_functions, comment_command_dict, indent="") |
418 | | - try: |
419 | | - exec(python_method) |
| 420 | + # Check the Python method is valid before writing it to the file |
| 421 | + if is_valid_method(python_method): |
420 | 422 | with open(path, "w", encoding="utf-8") as fid: |
421 | 423 | fid.write(f"{python_method}\n") |
422 | | - except Exception as e: |
423 | | - raise RuntimeError(f"Failed to execute {python_name}.py") from e |
424 | | - |
| 424 | + else: |
| 425 | + logging.warning( |
| 426 | + f"Invalid Python method for {initial_command_name}: {python_method}" |
| 427 | + ) |
425 | 428 | else: |
426 | | - import subprocess |
427 | 429 |
|
428 | 430 | package_structure = {} |
429 | 431 | all_commands = [] |
@@ -490,7 +492,7 @@ def write_source( |
490 | 492 | for class_name, _ in package_structure[module_name].items(): |
491 | 493 | file_path = library_path / module_name / f"{class_name}.py" |
492 | 494 | try: |
493 | | - subprocess.run(["python", str(file_path)]) |
| 495 | + py_compile.compile(str(file_path)) |
494 | 496 | except Exception as e: |
495 | 497 | raise RuntimeError( |
496 | 498 | f"Failed to execute '{python_method}' from '{file_path}'." |
@@ -553,7 +555,7 @@ def write_docs( |
553 | 555 |
|
554 | 556 | # Write the main doc file |
555 | 557 | doc_src = doc_package_path / "docs.rst" |
556 | | - with open(doc_src, "w") as fid: |
| 558 | + with open(doc_src, "w", encoding="utf-8") as fid: |
557 | 559 | fid.write(doc_src_content) |
558 | 560 |
|
559 | 561 | if package_structure is not None: |
@@ -588,7 +590,7 @@ def write_docs( |
588 | 590 | module_folder = doc_package_path / module_folder_name |
589 | 591 | module_folder.mkdir(parents=True, exist_ok=True) |
590 | 592 | module_file = module_folder / "index.rst" |
591 | | - with open(module_file, "w") as fid: |
| 593 | + with open(module_file, "w", encoding="utf-8") as fid: |
592 | 594 | fid.write(module_content) |
593 | 595 |
|
594 | 596 | for class_file_name, (class_name, method_list) in class_map.items(): |
@@ -616,7 +618,7 @@ def write_docs( |
616 | 618 |
|
617 | 619 | # Write the class file |
618 | 620 | class_file = module_folder / f"{class_file_name}.rst" |
619 | | - with open(class_file, "w") as fid: |
| 621 | + with open(class_file, "w", encoding="utf-8") as fid: |
620 | 622 | fid.write(class_content) |
621 | 623 |
|
622 | 624 | return doc_src |
0 commit comments