package com.jetbrains.python;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NonNls;
import org.python.core.PyObject;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author yole
 */
public class JythonManager {
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.JythonManager");

  public static JythonManager getInstance() {
    return ServiceManager.getService(JythonManager.class);
  }

  private PythonInterpreter myInterpreter;

  public JythonManager() {
    @NonNls Properties postProperties = new Properties();
    postProperties.put("python.cachedir.skip", "true");
    PySystemState.initialize(PySystemState.getBaseProperties(), postProperties, new String[] { "" });
    myInterpreter = new PythonInterpreter();
  }

  public void execScriptFromResource(@NonNls String resourcePath) {
    final InputStream stream = getClass().getClassLoader().getResourceAsStream(resourcePath);
    try {
      myInterpreter.execfile(stream);
    }
    finally {
      try {
        stream.close();
      }
      catch (IOException e) {
        LOG.error(e);
      }
    }
  }

  public PyObject eval(@NonNls final String code) {
    return myInterpreter.eval(code);
  }
}

@@ -1125,16 +1125,106 @@
 ePath);%0A
+    if (stream != null) %7B // TODO: stream == null seems to indicate some build problem?%0A  
     try 
@@ -1221,24 +1221,26 @@
       try %7B%0A
+  
       myInte
@@ -1269,22 +1269,26 @@
 m);%0A    
+  
 %7D%0A    
+ 
+ 
 finally 
@@ -1291,24 +1291,26 @@
 lly %7B%0A      
+  
 try %7B%0A      
@@ -1311,16 +1311,18 @@
         
+ 
+ 
 stream.c
@@ -1325,24 +1325,26 @@
 am.close();%0A
+  
       %7D%0A    
@@ -1345,16 +1345,18 @@
 %7D%0A      
+  
 catch (I
@@ -1379,16 +1379,18 @@
         
+  
 LOG.erro
@@ -1395,16 +1395,26 @@
 ror(e);%0A
+        %7D%0A
       %7D%0A

work around for missed (?) py files