From 67878cb87e5387ee984b526840be1287777be63f Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Tue, 19 Jul 2022 17:28:37 -0700 Subject: [PATCH] Add flask dynamic configurations --- .../dynamicdebugConfigurationService.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts b/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts index 30783227cd45..95ab0e7c3a9f 100644 --- a/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts +++ b/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts @@ -44,6 +44,40 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf justMyCode: true, }); } + + const flaskPath = await this.getFlaskPath(folder); + + if (flaskPath) { + providers.push({ + name: 'Dynamic Python: Flask', + type: DebuggerTypeName, + request: 'launch', + module: 'flask', + env: { + FLASK_APP: path.relative(folder.uri.fsPath, flaskPath), + FLASK_ENV: 'development', + }, + args: ['run', '--no-debugger'], + jinja: true, + justMyCode: true, + }); + } + return providers; } + + private async getFlaskPath(folder: WorkspaceFolder) { + const initPaths = await this.fs.search(path.join(folder.uri.fsPath, '**/__init__.py')); + const appPaths = await this.fs.search(path.join(folder.uri.fsPath, '**/app.py')); + const wsgiPaths = await this.fs.search(path.join(folder.uri.fsPath, '**/wsgi.py')); + const possiblePaths = [...initPaths, ...appPaths, ...wsgiPaths]; + + const regExpression = /app(?:lication)?\s*=\s*(?:flask\.)?Flask\(|def\s+(?:create|make)_app\(/; + + const flaskPaths = possiblePaths.filter((applicationPath) => + regExpression.exec(this.fs.readFileSync(applicationPath).toString()), + ); + + return flaskPaths.length ? flaskPaths[0] : null; + } }