A polyfill for pathlib.Path.relative_to (string-based implementation). Does NOT involve filesystem operations.
(path, parent)
| 22 | |
| 23 | |
| 24 | def relative_to(path, parent): |
| 25 | """ |
| 26 | A polyfill for pathlib.Path.relative_to (string-based implementation). |
| 27 | Does NOT involve filesystem operations. |
| 28 | """ |
| 29 | path = os.path.normpath(path) |
| 30 | parent = os.path.normpath(parent) |
| 31 | |
| 32 | if path == parent: |
| 33 | return '' |
| 34 | |
| 35 | if not parent.endswith(os.sep): |
| 36 | parent += os.sep |
| 37 | |
| 38 | # Check if path starts with parent |
| 39 | if not path.startswith(parent): |
| 40 | raise ValueError('Path {} is not relative to {}', path, parent) |
| 41 | |
| 42 | return path[len(parent) :] |
| 43 | |
| 44 | |
| 45 | def get_module_file_path(module_name): |
no test coverage detected