Search in sources :

Example 1 with PySystemState

use of org.python.core.PySystemState in project yamcs-studio by yamcs.

the class ExecutePythonScriptAction method run.

@Override
public void run() {
    if (code == null) {
        try {
            ScriptStoreFactory.initPythonInterpreter();
        } catch (Exception e) {
            final String message = "Failed to initialize PythonInterpreter";
            OPIBuilderPlugin.getLogger().log(Level.WARNING, message, e);
            // $NON-NLS-1$
            ConsoleService.getInstance().writeError(message + "\n" + e);
        }
        // read file
        IPath absolutePath = getAbsolutePath();
        state = new PySystemState();
        // Add the path of script to python module search path
        if (!isEmbedded() && absolutePath != null && !absolutePath.isEmpty()) {
            // If it is a workspace file.
            if (ResourceUtil.isExistingWorkspaceFile(absolutePath)) {
                IPath folderPath = absolutePath.removeLastSegments(1);
                String sysLocation = ResourceUtil.workspacePathToSysPath(folderPath).toOSString();
                state.path.append(new PyString(sysLocation));
            } else if (ResourceUtil.isExistingLocalFile(absolutePath)) {
                IPath folderPath = absolutePath.removeLastSegments(1);
                state.path.append(new PyString(folderPath.toOSString()));
            }
        }
        interpreter = new PythonInterpreter(null, state);
        GraphicalViewer viewer = getWidgetModel().getRootDisplayModel().getViewer();
        if (viewer != null) {
            Object obj = viewer.getEditPartRegistry().get(getWidgetModel());
            if (obj != null && obj instanceof AbstractBaseEditPart) {
                displayEditpart = (DisplayEditpart) (viewer.getContents());
                widgetEditPart = (AbstractBaseEditPart) obj;
            }
        }
    }
    Job job = new Job("Execute Python Script") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            String taskName = isEmbedded() ? "Execute Python Script" : "Connecting to " + getAbsolutePath();
            monitor.beginTask(taskName, IProgressMonitor.UNKNOWN);
            runTask();
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
}
Also used : PySystemState(org.python.core.PySystemState) PyString(org.python.core.PyString) PythonInterpreter(org.python.util.PythonInterpreter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GraphicalViewer(org.eclipse.gef.GraphicalViewer) IPath(org.eclipse.core.runtime.IPath) AbstractBaseEditPart(org.csstudio.opibuilder.editparts.AbstractBaseEditPart) PyObject(org.python.core.PyObject) PyString(org.python.core.PyString) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

AbstractBaseEditPart (org.csstudio.opibuilder.editparts.AbstractBaseEditPart)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 GraphicalViewer (org.eclipse.gef.GraphicalViewer)1 PyObject (org.python.core.PyObject)1 PyString (org.python.core.PyString)1 PySystemState (org.python.core.PySystemState)1 PythonInterpreter (org.python.util.PythonInterpreter)1