r659 - in branches/jrst-docutils-jython: docutils/src/main/resources/docutils docutils/src/main/resources/docutils/docutils doxia-module-jrst/src/main/java/org/nuiton/jrst jrst/src/main/java/org/nuiton/jrst
Author: jpages Date: 2012-05-14 18:01:52 +0200 (Mon, 14 May 2012) New Revision: 659 Url: http://nuiton.org/repositories/revision/jrst/659 Log: Correction partielle des bugs avec python et les chemins sous windows. Modified: branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py branches/jrst-docutils-jython/docutils/src/main/resources/docutils/docutils/core.py branches/jrst-docutils-jython/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstParser.java branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.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-10 15:42:39 UTC (rev 658) +++ branches/jrst-docutils-jython/docutils/src/main/resources/docutils/__run__.py 2012-05-14 16:01:52 UTC (rev 659) @@ -1,7 +1,7 @@ #!/usr/bin/python # coding=utf-8 -import __builtin__, os, sys, string +import __builtin__, os, sys, string, time from java.io import File, InputStream from java.net import URL, JarURLConnection @@ -18,9 +18,8 @@ # python doesn't be able to find the resource, so it must have the # absolute path to the jar to read it with jaropen after. # - # TODO (?) : Modifier pour que cela fonctionne avec pour la génération de site + # TODO (?) : Modifier pour que cela fonctionne pour la génération de site def jaropen(resourcePath, mode, bufsize=-1): - print "resourcePath " + resourcePath if "__pyclasspath__" in resourcePath: # Replace the relative resource filepath by the absolute path docutilsAbsolutePath = "jar:file:" + os.path.dirname(docutilsPath) + "!" @@ -29,7 +28,6 @@ # /!\ Warning : we use a deprecated function, it will could be a problem later url = URL(resourcePath) inStream = url.openStream() - print "new resourcePath : " + resourcePath f = __builtin__.openlegacy(inStream, bufsize) else: f = __builtin__.openlegacy(resourcePath, mode, bufsize) @@ -39,8 +37,8 @@ __builtin__.__dict__["openlegacy"] = __builtin__.__dict__["open"] __builtin__.__dict__["open"] = jaropen -def exec_docutils (docutilsPath, typeOutput, filein, fileout): - +def exec_docutils (docutilsPath, typeOutput, filein): + # Initalization before using Docutils init_docutils(docutilsPath) @@ -53,27 +51,11 @@ sys.path.append(scriptpath) typeOutput = typeOutput.lower() - + # Check if the output type is supported by Docutils listType = ["xml", "html", "odt", "latex", "man", "s5", "xetex"] if typeOutput in listType: - # With odt, man and s5 formats, the 'publish_file' function causes - # the error "IOError: StreamIO.seek() not supported" - # and the 'publish_cmdline_to_binary' function causes - # "__run__: error: Maximum 2 arguments allowed.". - # We must call the docutils script but execfile cannot be used with - # arguments, we have to use 'subprocess.call()' - import subprocess - if typeOutput == "odt": - subprocess.call(['rst2odt.py', filein, fileout]) - elif typeOutput == "man": - subprocess.call(['rst2man.py', filein, fileout]) - elif typeOutput == "s5": - subprocess.call(['rst2s5.py', filein, fileout]) - else: - # We can use 'publish_file' with the other format - from docutils.core import publish_file - print publish_file( source_path=filein, destination_path=fileout, - writer_name=typeOutput ) + from docutils.core import publish_file + publish_file( source_path=filein, writer_name=typeOutput ) else: print "Wrong output format" Modified: branches/jrst-docutils-jython/docutils/src/main/resources/docutils/docutils/core.py =================================================================== --- branches/jrst-docutils-jython/docutils/src/main/resources/docutils/docutils/core.py 2012-05-10 15:42:39 UTC (rev 658) +++ branches/jrst-docutils-jython/docutils/src/main/resources/docutils/docutils/core.py 2012-05-14 16:01:52 UTC (rev 659) @@ -212,16 +212,24 @@ """ exit = None try: + print "if" if self.settings is None: + print "self.settings is None" self.process_command_line( argv, usage, description, settings_spec, config_section, **(settings_overrides or {})) - self.set_io() + print "end if" + self.set_io() + print "set_io" self.document = self.reader.read(self.source, self.parser, self.settings) - self.apply_transforms() + print "document" + self.apply_transforms() + print "apply_transforms" output = self.writer.write(self.document, self.destination) + print "output = self.writer.write(self.document, self.destination)" self.writer.assemble_parts() + print "writer.assemble_parts()" except SystemExit, error: exit = 1 exit_status = error.code Modified: branches/jrst-docutils-jython/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstParser.java =================================================================== --- branches/jrst-docutils-jython/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstParser.java 2012-05-10 15:42:39 UTC (rev 658) +++ branches/jrst-docutils-jython/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstParser.java 2012-05-14 16:01:52 UTC (rev 659) @@ -28,11 +28,14 @@ import java.io.*; import java.net.URL; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.maven.doxia.module.xdoc.XdocParser; import org.apache.maven.doxia.parser.ParseException; import org.apache.maven.doxia.sink.Sink; import org.codehaus.plexus.util.IOUtil; import org.dom4j.Document; +import org.nuiton.util.FileUtil; import org.nuiton.util.Resource; import sun.nio.ch.FileKey; 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-10 15:42:39 UTC (rev 658) +++ branches/jrst-docutils-jython/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-05-14 16:01:52 UTC (rev 659) @@ -8,16 +8,16 @@ * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% @@ -57,6 +57,7 @@ import org.nuiton.util.FileUtil; import org.nuiton.util.Resource; import org.nuiton.util.StringUtil; +import org.python.core.PyObject; import org.python.util.PythonInterpreter; /** @@ -423,7 +424,6 @@ // do transformation doc = gen.transform(doc, stylesheet); - log.info(doc.asXML()); } return doc; } @@ -431,8 +431,18 @@ public static Document generateXML(File in) throws Exception { // RST to XML transformation via Docutils and a temporary file File out = File.createTempFile("result", ".xml"); + + FileWriter fileWriter = new FileWriter(out); + BufferedWriter bufferedWriter = null; + + try { + bufferedWriter = new BufferedWriter(fileWriter); + fileWriter.write(""); + } finally { + bufferedWriter.close(); + } + out = generateDocument(TYPE_XML, in, out); - SAXReader saxReader = new SAXReader(); return saxReader.read(out); } @@ -442,32 +452,49 @@ // Name of the main Python script final String runner = "__run__"; + log.info(out); + /* Transformation of the __run__ URL into a path that python will use For example the URL is : jar:file:/home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar!/__run__.py and it will become : /home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar/ */ - URL resource = in.getClass().getResource("/__run__.py"); - log.info("URL of the resource : " + resource); - String docutilsPath = resource.getPath().replaceAll("__run__.py", ""); + URL resource = JRST.class.getResource("/__run__.py"); + String docutilsPath = resource.getPath() + .replaceAll("__run__.py", ""); docutilsPath = docutilsPath.replaceAll("!", ""); docutilsPath = docutilsPath.replaceAll("file:", ""); - log.info("docutilsPath = "+docutilsPath); // Import of the main script to use docutils ( __run__ ) PythonInterpreter interp = new PythonInterpreter(); String commandImport = "import __run__"; interp.exec(commandImport); + String filePath = in.getAbsolutePath(); + String property = System.getProperty("os.name"); + if (property.contains("win")) { + filePath = filePath.replaceAll("\\\\", "\\\\\\\\"); + } + // Execution of the docutils script to transform rst to xml - String commandExec = String.format("__run__.exec_docutils('%s', '%s', '%s', '%s')", - docutilsPath, outputType, in.getPath(), out.getPath()); - log.info(commandExec); - interp.exec(commandExec); + String commandExec = String.format("__run__.exec_docutils('%s', '%s', '%s')", + docutilsPath, outputType, filePath); + log.info("commandExec : " + commandExec); + FileOutputStream fileOutputStream = null; + try { + // Sets an output stream to build the output file + fileOutputStream = new FileOutputStream(out); + interp.setOut(fileOutputStream); + interp.exec(commandExec); - // Cleaning the python interpreter to avoid problems if they are multiple execution of this method - interp.cleanup(); + // Cleans the python interpreter to avoid problems if they are multiple execution of this method + interp.cleanup(); + } finally { + if (fileOutputStream != null) { + fileOutputStream.close(); + } + } return out; } @@ -488,4 +515,4 @@ return result; } -} +} \ No newline at end of file
participants (1)
-
jpages@users.nuiton.org