| 29 | } |
| 30 | |
| 31 | export class PythonRenderer implements Renderer { |
| 32 | /** |
| 33 | * Function to generate a file header. |
| 34 | * |
| 35 | * @private |
| 36 | */ |
| 37 | private _header?: HeaderArg; |
| 38 | /** |
| 39 | * Options for module specifier resolution. |
| 40 | * |
| 41 | * @private |
| 42 | */ |
| 43 | private _module?: Partial<BaseOutput>['module']; |
| 44 | /** |
| 45 | * Whether `export * from 'module'` should be used when possible instead of named exports. |
| 46 | * |
| 47 | * @private |
| 48 | */ |
| 49 | private _preferExportAll: boolean; |
| 50 | |
| 51 | constructor( |
| 52 | args: Pick<Partial<BaseOutput>, 'module'> & { |
| 53 | header?: HeaderArg; |
| 54 | preferExportAll?: boolean; |
| 55 | } = {}, |
| 56 | ) { |
| 57 | this._header = args.header; |
| 58 | this._module = args.module; |
| 59 | this._preferExportAll = args.preferExportAll ?? false; |
| 60 | } |
| 61 | |
| 62 | render(ctx: RenderContext<PyDsl>): string { |
| 63 | const header = typeof this._header === 'function' ? this._header(ctx) : this._header; |
| 64 | return PythonRenderer.astToString({ |
| 65 | exports: this.getExports(ctx), |
| 66 | exportsOptions: { |
| 67 | preferExportAll: this._preferExportAll, |
| 68 | }, |
| 69 | header, |
| 70 | imports: this.getImports(ctx), |
| 71 | nodes: ctx.file.nodes, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | supports(ctx: RenderContext): boolean { |
| 76 | return ctx.file.language === 'python'; |
| 77 | } |
| 78 | |
| 79 | static astToString(args: { |
| 80 | exports?: Exports; |
| 81 | exportsOptions?: ExportsOptions; |
| 82 | header?: Header; |
| 83 | imports?: Imports; |
| 84 | nodes?: ReadonlyArray<PyDsl>; |
| 85 | /** |
| 86 | * Whether to include a trailing newline at the end of the file. |
| 87 | * |
| 88 | * @default true |
nothing calls this directly
no outgoing calls
no test coverage detected