Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Client side code cleaning and Python extension integration #85

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion server/core/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ def reload_database(ls):
Odoo.get().reset(ls)
FileMgr.files = {}
ls.show_message_log("Building new database", MessageType.Log)
ls.show_message("Reloading Odoo database", MessageType.Info)
ls.launch_thread(target=Odoo.initialize, args=(ls,))


Expand Down
2 changes: 2 additions & 0 deletions vscode/build_package.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions vscode/client/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as path from 'path';

const folderName = path.basename(__dirname);
export const EXTENSION_ROOT_DIR =
folderName === 'common' ? path.dirname(path.dirname(__dirname)) : path.dirname(__dirname);
export const BUNDLED_PYTHON_SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR, 'server');
export const SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, '__main__.py');
export const DEBUG_SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, `_debug_server.py`);
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import {EventEmitter} from "vscode";

export const selectedConfigurationChange = new EventEmitter();
export const ConfigurationsChange = new EventEmitter();
export const clientStopped = new EventEmitter();
87 changes: 87 additions & 0 deletions vscode/client/common/python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/* eslint-disable @typescript-eslint/naming-convention */
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode';
import { PythonExtension, ResolvedEnvironment, VersionInfo } from '@vscode/python-extension';

export interface IInterpreterDetails {
path?: string[];
resource?: Uri;
}

export const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;

let _api: PythonExtension | undefined;
async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> {
if (_api) {
return _api;
}
_api = await PythonExtension.api();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure it will never become invalid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use this Promise right after getting it and getting it each time we need it to be as close to the current configuration as we can be so there's no reason for it to be invalid in the current setup (but maybe I didn't understand the comment)

return _api;
}

export async function initializePython(disposables: Disposable[]): Promise<void> {
try {
const api = await getPythonExtensionAPI();

if (api) {
disposables.push(
api.environments.onDidChangeActiveEnvironmentPath((e) => {
onDidChangePythonInterpreterEvent.fire({ path: [e.path], resource: e.resource?.uri });
}),
);

console.log('Waiting for interpreter from python extension.');
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
}
} catch (error) {
console.error('Error initializing python: ', error);
}
}

export async function resolveInterpreter(interpreter: string[]): Promise<ResolvedEnvironment | undefined> {
const api = await getPythonExtensionAPI();
return api?.environments.resolveEnvironment(interpreter[0]);
}

export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
const api = await getPythonExtensionAPI();
const environment = await api?.environments.resolveEnvironment(
api?.environments.getActiveEnvironmentPath(resource),
);
if (environment?.executable.uri && checkVersion(environment)) {
return { path: [environment?.executable.uri.fsPath], resource };
}
return { path: undefined, resource };
}

export async function getDebuggerPath(): Promise<string | undefined> {
const api = await getPythonExtensionAPI();
return api?.debug.getDebuggerPackagePath();
}

export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
await getPythonExtensionAPI();
return await commands.executeCommand(command, ...rest);
}

export function checkVersion(resolved: ResolvedEnvironment | undefined): boolean {
const version = resolved?.version;
if (version?.major === 3 && version?.minor >= 8) {
return true;
}
console.error(`Python version ${version?.major}.${version?.minor} is not supported.`);
console.error(`Selected python path: ${resolved?.executable.uri?.fsPath}`);
console.error('Supported versions are 3.8 and above.');
return false;
}

export async function getPythonVersion(): Promise<VersionInfo> {
let interpreterDetails = await getInterpreterDetails();
let resolved = await resolveInterpreter(interpreterDetails.path);
let version = resolved.version;

return version;
}
11 changes: 1 addition & 10 deletions vscode/client/utils/utils.ts → vscode/client/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from "child_process";
import { ExtensionContext, Uri, Webview } from "vscode";

/**
Expand All @@ -25,17 +24,9 @@ export function getNonce() {
return text;
}

export function getPythonVersion(context: ExtensionContext, pythonPath: String = "python3") {
try {
return execSync(`${pythonPath} --version`, {encoding: 'utf8'})
} catch {
return null;
}
}

// Config related utils

export function getCurrentConfig(context: ExtensionContext) {
export async function getCurrentConfig(context: ExtensionContext) {
const configs: any = context.globalState.get("Odoo.configurations");
const activeConfig: number = Number(context.workspaceState.get('Odoo.selectedConfiguration'));
return (configs && activeConfig > -1 ? configs[activeConfig] : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export function getConfigurationStructure(id: number = 0) {
"name": `New Configuration ${id}`,
"odooPath": "",
"addons": [],
"pythonPath": "python3",
}
}
Loading