Return a dotted module name based on the given path, anchored on root. For example: path="projects/src/tests/test_foo.py" and root="/projects", the resulting module name will be "src.tests.test_foo".
(path: Path, root: Path)
| 570 | |
| 571 | |
| 572 | def module_name_from_path(path: Path, root: Path) -> str: |
| 573 | """ |
| 574 | Return a dotted module name based on the given path, anchored on root. |
| 575 | |
| 576 | For example: path="projects/src/tests/test_foo.py" and root="/projects", the |
| 577 | resulting module name will be "src.tests.test_foo". |
| 578 | """ |
| 579 | path = path.with_suffix("") |
| 580 | try: |
| 581 | relative_path = path.relative_to(root) |
| 582 | except ValueError: |
| 583 | # If we can't get a relative path to root, use the full path, except |
| 584 | # for the first part ("d:\\" or "/" depending on the platform, for example). |
| 585 | path_parts = path.parts[1:] |
| 586 | else: |
| 587 | # Use the parts for the relative path to the root path. |
| 588 | path_parts = relative_path.parts |
| 589 | |
| 590 | return ".".join(path_parts) |
| 591 | |
| 592 | |
| 593 | def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) -> None: |
no outgoing calls