<commit_before>package org.openlca.app.devtools.python;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.openlca.app.App;
import org.openlca.app.Config;
import org.openlca.app.db.Database;
import org.openlca.app.rcp.RcpActivator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.zip.ZipUtil;

public class Python {

	private static boolean initialized = false;
	private static org.openlca.updates.script.Python python;

	public static void eval(String script) {
		try {
			if (!initialized)
				initialize();
			python.eval(script);
		} catch (Exception e) {
			Logger log = LoggerFactory.getLogger(Python.class);
			log.error("failed to evaluate script", e);
		}
	}

	public static File getDir() {
		if (!initialized)
			initialize();
		File workspace = App.getWorkspace();
		return new File(workspace, "python");
	}

	private static synchronized void initialize() {
		if (initialized)
			return;
		Logger log = LoggerFactory.getLogger(Python.class);
		File workspace = App.getWorkspace();
		File pyDir = new File(workspace, "python");
		log.info("initialize Python interpreter");
		try {
			if (!matchVersion(pyDir))
				initPythonDir(pyDir);
			python = new org.openlca.updates.script.Python(Database.get(),
					App.getCalculationContext(), pyDir);
			python.register("app", App.class);
			File dataDir = new File(workspace, "script_data");
			if (!dataDir.exists())
				dataDir.mkdir();
			python.setDataDir(dataDir);
			initialized = true;
		} catch (Exception e) {
			log.error("failed to initialize python interpreter @" + pyDir, e);
		}
	}

	private static boolean matchVersion(File pyDir) throws Exception {
		File versionFile = new File(pyDir, ".version");
		if (!versionFile.exists())
			return false;
		byte[] bytes = Files.readAllBytes(versionFile.toPath());<commit_msg>Fixed issue with python editor when switching database<commit_after>package org.openlca.app.devtools.python;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.openlca.app.App;
import org.openlca.app.Config;
import org.openlca.app.db.Database;
import org.openlca.app.rcp.RcpActivator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.zip.ZipUtil;

public class Python {

	private static boolean initialized = false;
	private static org.openlca.updates.script.Python python;

	public static void eval(String script) {
		try {
			if (!initialized)
				initialize();
			python.setDatabase(Database.get());
			python.eval(script);
		} catch (Exception e) {
			Logger log = LoggerFactory.getLogger(Python.class);
			log.error("failed to evaluate script", e);
		}
	}

	public static File getDir() {
		if (!initialized)
			initialize();
		File workspace = App.getWorkspace();
		return new File(workspace, "python");
	}

	private static synchronized void initialize() {
		if (initialized)
			return;
		Logger log = LoggerFactory.getLogger(Python.class);
		File workspace = App.getWorkspace();
		File pyDir = new File(workspace, "python");
		log.info("initialize Python interpreter");
		try {
			if (!matchVersion(pyDir))
				initPythonDir(pyDir);
			python = new org.openlca.updates.script.Python(App.getCalculationContext(), pyDir);
			python.register("app", App.class);
			File dataDir = new File(workspace, "script_data");
			if (!dataDir.exists())
				dataDir.mkdir();
			python.setDataDir(dataDir);
			initialized = true;
		} catch (Exception e) {
			log.error("failed to initialize python interpreter @" + pyDir, e);
		}
	}

	private static boolean matchVersion(File pyDir) throws Exception {
		File versionFile = new File(pyDir, ".version");
		if (!versionFile.exists())
			return false;
		byte[] bytes = Files.readAllBytes(versionFile.toPath());
{'fraction_non_alphanumeric': 0.12611921207469942, 'fraction_numerical': 0.0010232796111537478, 'mean_word_length': 4.468531468531468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7656773', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1224, 'n_words': 298}