r654 - in branches/jrst-docutils-jython: docutils/src/main/resources/docutils jrst/src/main/java/org/nuiton/jrst jrst/src/test/java/org/nuiton/jrst
Author: jpages Date: 2012-05-02 18:30:29 +0200 (Wed, 02 May 2012) New Revision: 654 Url: http://nuiton.org/repositories/revision/jrst/654 Log: Modifications de JRST pour pouvoir r?\195?\169-initialiser PySystemState Modified: branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java Modified: branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py =================================================================== --- branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py 2012-05-02 07:12:20 UTC (rev 653) +++ branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py 2012-05-02 16:30:29 UTC (rev 654) @@ -46,6 +46,10 @@ filein = sys.argv[3] fileout = sys.argv[4] +print "filein : " + filein +print "fileout : " + fileout +print "writer_name" + typeOutput + # If Docutils can manage this output format, we call it from docutils.core import publish_file Modified: branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java =================================================================== --- branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-05-02 07:12:20 UTC (rev 653) +++ branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-05-02 16:30:29 UTC (rev 654) @@ -27,6 +27,8 @@ import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.io.*; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.net.URL; import java.util.*; @@ -56,7 +58,12 @@ import org.nuiton.util.FileUtil; import org.nuiton.util.Resource; import org.nuiton.util.StringUtil; +import org.python.core.Py; +import org.python.core.PySystemState; +import org.python.core.adapter.ExtensiblePyObjectAdapter; +import org.python.core.imp; import org.python.util.JarRunner; +import org.python.util.PythonInterpreter; /** * FIXME: 'JRST --help' doesn't work, but 'JRST --help toto' work :( FIXME: @@ -142,6 +149,9 @@ /** PDF output format type */ public static final String TYPE_PDF = "pdf"; + /** Boolean to know if PySystemState has already been initialized */ + private static boolean INITIALIZED = false; + /** * key, Out type; value: chain of XSL file to provide wanted file for output */ @@ -416,7 +426,10 @@ return doc; } + + /* public static Document generateXML(File in) throws Exception { + final String runner = "__run__"; URL resource = in.getClass().getResource("/__run__.py"); log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + resource); @@ -424,13 +437,118 @@ docutilsPath = docutilsPath.replaceAll("!", ""); docutilsPath = docutilsPath.replaceAll("file:", ""); + // Methode initialize qui retourne un booléen + Method method = PySystemState.class.getDeclaredMethod("initialize",Properties.class, + Properties.class, + String[].class, + ClassLoader.class, + ExtensiblePyObjectAdapter.class, + ClassLoader.class); + method.setAccessible(true); + + //arguments de la méthode + Properties props = PySystemState.getBaseProperties(); + props.setProperty("python.path", docutilsPath); + File out = File.createTempFile("result", ".xml"); + String[] argv = new String[]{runner, docutilsPath, Format.XML.name(), in.getPath(), out.getPath()}; log.info("docutilsPath = "+docutilsPath); - File out = File.createTempFile("result", ".xml"); + ClassLoader context = Thread.currentThread().getContextClassLoader(); + + Field initialized3 = PySystemState.class.getDeclaredField("defaultArgv"); + initialized3.setAccessible(true); + initialized3.set(null, argv); + JarRunner.run(new String[]{docutilsPath, Format.XML.name(), in.getPath(), out.getPath()}); log.info("out = "+FileUtils.readFileToString(out)); SAXReader saxReader = new SAXReader(); return saxReader.read(out); + } */ + + + public static Document generateXML(File in) throws Exception { + final String runner = "__run__"; + + URL resource = in.getClass().getResource("/__run__.py"); + log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + resource); + String docutilsPath = resource.getPath().replaceAll("__run__.py", ""); + docutilsPath = docutilsPath.replaceAll("!", ""); + docutilsPath = docutilsPath.replaceAll("file:", ""); + + log.info("docutilsPath = "+docutilsPath); + File out = File.createTempFile("result", ".xml"); + + Properties props = PySystemState.getBaseProperties(); + props.setProperty("python.path", docutilsPath); + String[] argv = new String[]{runner, docutilsPath, Format.XML.name(), in.getPath(), out.getPath()}; + log.info("Run Python with args: " + Arrays.toString(argv)); + + + if (INITIALIZED) { + Field initialized = PySystemState.class.getDeclaredField("initialized"); + initialized.setAccessible(true); + initialized.set(null, false); + + Field initialized2 = PySystemState.class.getDeclaredField("registry"); + initialized2.setAccessible(true); + initialized2.set(null, null); + + + /* + closer = new PySystemStateCloser(this); + modules = new PyStringMap(); + + argv = (PyList)defaultArgv.repeat(1); + path = (PyList)defaultPath.repeat(1); + path.append(Py.newString(JavaImporter.JAVA_IMPORT_PATH_ENTRY)); + path.append(Py.newString(ClasspathPyImporter.PYCLASSPATH_PREFIX)); + executable = defaultExecutable; + + meta_path = new PyList(); + path_hooks = new PyList(); + path_hooks.append(new JavaImporter()); + path_hooks.append(zipimporter.TYPE); + path_hooks.append(ClasspathPyImporter.TYPE); + path_importer_cache = new PyDictionary(); + + currentWorkingDir = new File("").getAbsolutePath(); + + // Set up the initial standard ins and outs + String mode = Options.unbuffered ? "b" : ""; + int buffering = Options.unbuffered ? 0 : 1; + stdin = __stdin__ = new PyFile(System.in, "<stdin>", "r" + mode, buffering, false); + stdout = __stdout__ = new PyFile(System.out, "<stdout>", "w" + mode, buffering, false); + stderr = __stderr__ = new PyFile(System.err, "<stderr>", "w" + mode, 0, false); + initEncoding(); + + __displayhook__ = new PySystemStateFunctions("displayhook", 10, 1, 1); + __excepthook__ = new PySystemStateFunctions("excepthook", 30, 3, 3); + + if (builtins == null) { + builtins = getDefaultBuiltins(); + } + modules.__setitem__("__builtin__", new PyModule("__builtin__", getDefaultBuiltins())); + __dict__ = new PyStringMap(); + __dict__.invoke("update", getType().fastGetDict()); + __dict__.__setitem__("displayhook", __displayhook__); + __dict__.__setitem__("excepthook", __excepthook__); + */ + + + } else { + INITIALIZED = true; + } + + PythonInterpreter.initialize(PySystemState.getBaseProperties(), props, argv); + + PythonInterpreter interp = new PythonInterpreter(); + interp.execfile(in.getClass().getResourceAsStream("/__run__.py")); + interp.getSystemState().cleanup(); + interp.cleanup(); + + log.info("out content =\n"+FileUtils.readFileToString(out)+"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + SAXReader saxReader = new SAXReader(); + return saxReader.read(out); } @@ -441,15 +559,24 @@ docutilsPath = docutilsPath.replaceAll("!", ""); docutilsPath = docutilsPath.replaceAll("file:", ""); + + String[] argv = new String[]{"__run__.py", docutilsPath, outputType, in.getPath(), out.getPath()}; + // Only for debugging if (log.isDebugEnabled()){ - String message = "Run Python with args: " + - Arrays.toString(new String[]{docutilsPath, outputType, in.getPath(), out.getPath()}); + String message = "Run Python with args: " + Arrays.toString(argv); log.debug(message); } + /* + JarRunner.run(); + */ + Properties props = PySystemState.getBaseProperties(); + System.out.println(props.getProperty("python.path")); + props.setProperty("python.path", docutilsPath); + PythonInterpreter.initialize(PySystemState.getBaseProperties(), props, argv); + PythonInterpreter interp = new PythonInterpreter(); + interp.execfile(docutilsPath + "__run__.py"); - JarRunner.run(new String[]{docutilsPath, outputType, in.getPath(), out.getPath()}); - return out; } Modified: branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java =================================================================== --- branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java 2012-05-02 07:12:20 UTC (rev 653) +++ branches/jrst-docutils-jython/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java 2012-05-02 16:30:29 UTC (rev 654) @@ -121,13 +121,14 @@ File in = getTestFile("text.rst"); - File out = getOutputTestFile("jrst-RstToXml2.xml"); + File out = getOutputTestFile("jrst-RstToXml1.xml"); + File out2 = getOutputTestFile("jrst-RstToXml2.xml"); JRST.generate(JRST.Format.XML.name(), in, out, JRST.Overwrite.ALLTIME); try { - JRST.generate(JRST.Format.XML.name(), in, out, JRST.Overwrite.ALLTIME); + JRST.generate(JRST.Format.XML.name(), in, out2, JRST.Overwrite.ALLTIME); } catch (Throwable e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + e.printStackTrace(); } }
participants (1)
-
jpages@users.nuiton.org