Format Python source code and write to a file, creating parent directories as needed. Parameters ---------- py_source : str String containing valid Python source code. If string is empty, no file will be written. filepath : str Full path to the file
(py_source, filepath, leading_newlines=0)
| 18 | # Source code utilities |
| 19 | # ===================== |
| 20 | def write_source_py(py_source, filepath, leading_newlines=0): |
| 21 | """ |
| 22 | Format Python source code and write to a file, creating parent |
| 23 | directories as needed. |
| 24 | |
| 25 | Parameters |
| 26 | ---------- |
| 27 | py_source : str |
| 28 | String containing valid Python source code. If string is empty, |
| 29 | no file will be written. |
| 30 | filepath : str |
| 31 | Full path to the file to be written |
| 32 | Returns |
| 33 | ------- |
| 34 | None |
| 35 | """ |
| 36 | if py_source: |
| 37 | # Make dir if needed |
| 38 | filedir = opath.dirname(filepath) |
| 39 | # The exist_ok kwarg is only supported with Python 3, but that's ok since |
| 40 | # codegen is only supported with Python 3 anyway |
| 41 | os.makedirs(filedir, exist_ok=True) |
| 42 | |
| 43 | # Write file |
| 44 | py_source = "\n" * leading_newlines + py_source |
| 45 | with open(filepath, "at") as f: |
| 46 | f.write(py_source) |
| 47 | |
| 48 | |
| 49 | def build_from_imports_py(rel_modules=(), rel_classes=(), init_extra=""): |
no test coverage detected