Given a filename this will try to calculate the python path, add it to the search path and return the actual module name that is expected.
(path: str)
| 201 | |
| 202 | |
| 203 | def prepare_import(path: str) -> str: |
| 204 | """Given a filename this will try to calculate the python path, add it |
| 205 | to the search path and return the actual module name that is expected. |
| 206 | """ |
| 207 | path = os.path.realpath(path) |
| 208 | |
| 209 | fname, ext = os.path.splitext(path) |
| 210 | if ext == ".py": |
| 211 | path = fname |
| 212 | |
| 213 | if os.path.basename(path) == "__init__": |
| 214 | path = os.path.dirname(path) |
| 215 | |
| 216 | module_name = [] |
| 217 | |
| 218 | # move up until outside package structure (no __init__.py) |
| 219 | while True: |
| 220 | path, name = os.path.split(path) |
| 221 | module_name.append(name) |
| 222 | |
| 223 | if not os.path.exists(os.path.join(path, "__init__.py")): |
| 224 | break |
| 225 | |
| 226 | if sys.path[0] != path: |
| 227 | sys.path.insert(0, path) |
| 228 | |
| 229 | return ".".join(module_name[::-1]) |
| 230 | |
| 231 | |
| 232 | class ScriptInfo: |