[Pywps-commits] r863 - in trunk/pywps: Parser Process Wps

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 5 17:15:34 CET 2009


Author: jachym
Date: 2009-11-05 17:15:33 +0100 (Thu, 05 Nov 2009)
New Revision: 863

Modified:
   trunk/pywps/Parser/Execute.py
   trunk/pywps/Process/InAndOutputs.py
   trunk/pywps/Wps/Execute.py
Log:
use base64 encoding for binary data in input/output XML

Modified: trunk/pywps/Parser/Execute.py
===================================================================
--- trunk/pywps/Parser/Execute.py	2009-11-05 10:13:57 UTC (rev 862)
+++ trunk/pywps/Parser/Execute.py	2009-11-05 16:15:33 UTC (rev 863)
@@ -49,7 +49,7 @@
         language  = None
         identifiers = []
         identifierNode = None
-        dataInputs = None
+        dataInputs = []
 
         #
         # Mandatory options
@@ -197,6 +197,7 @@
 
         parsedDataInputs = []
 
+        import sys
         for inputNode in inputsNode.getElementsByTagNameNS(self.nameSpace,
                                                                 "Input"):
             # input Identifier
@@ -206,7 +207,6 @@
             except (IndexError, AttributeError):
                 raise self.wps.exceptions.NoApplicableCode(
                                               "Identifier for input not set")
-
             parsedDataInputs.append({"identifier":identifier,"value":None,
                 "attributes":{}})
 
@@ -220,7 +220,8 @@
                 attributes = self.parseReferenceDataInput(dataTypeNode)
                 attributes["identifier"] = identifier
                 parsedDataInputs[-1] = attributes
-            except IndexError:
+            except IndexError,e:
+
                 dataTypeNode = inputNode.getElementsByTagNameNS(
                                                 self.nameSpace,"Data")[0]
                 attributes =self.parseDataInput(dataTypeNode)

Modified: trunk/pywps/Process/InAndOutputs.py
===================================================================
--- trunk/pywps/Process/InAndOutputs.py	2009-11-05 10:13:57 UTC (rev 862)
+++ trunk/pywps/Process/InAndOutputs.py	2009-11-05 16:15:33 UTC (rev 863)
@@ -18,7 +18,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-import types,re
+import types,re, magic, base64
 from pywps import Exceptions
 
 class Input:
@@ -333,6 +333,22 @@
         fout.write(data)
         fout.close()
 
+        # load the mimetypes
+        ms = magic.open(magic.MAGIC_MIME)
+        ms.load()
+        
+        # check, if the file is binary or not
+        if self.format["mimeType"].find("text") == -1:
+            # it *should* be binary, is the file really binary?
+            # if so, convert it to binary using base64
+            if ms.open(fout.name).find("text") > -1: 
+                sys.rename(fout.name,fout.name+".base64")
+                base64.decode(open(fout.name+".base64"),
+                              open(fout.name,"w"))
+
+        # check the mimeType again
+        self.checkMimeType(ms.open(fout.name))
+            
         resp = self._setValueWithOccurence(self.value, outputName)
         if resp:
             return resp
@@ -399,6 +415,11 @@
                         inputUrl)
         fout.close()
 
+        # check the mimetypes
+        ms = magic.open(magic.MAGIC_MIME)
+        ms.load()
+        self.checkMimeType(ms.open(fout.name))
+
         resp = self._setValueWithOccurence(self.value, outputName)
         if resp:
             return resp
@@ -413,6 +434,24 @@
         """
         pass
 
+    def checkMimeType(self,mimeType):
+        """Check, if the given mimetype is in self.formats
+
+        Parameters:
+        mimeType {String}
+        """
+        mimeTypes = "";
+        for format in self.formats:
+            mimeTypes += format["mimeType"] + " ";
+            if ms.file(fout.name()) in format["mimeType"]:
+                self.format = format
+                return
+        if self.format == None:
+            self.onProblem("InvalidParameterValue","Files mimeType ["+
+                ms.file(fout.name())" does not correspond with allowed
+                mimeType values, which can be on from ["+ mimeTypes+"]")
+
+
     def onMaxFileSizeExceeded(self, why):
         """Empty method, called, when there was any problem with the input.
         

Modified: trunk/pywps/Wps/Execute.py
===================================================================
--- trunk/pywps/Wps/Execute.py	2009-11-05 10:13:57 UTC (rev 862)
+++ trunk/pywps/Wps/Execute.py	2009-11-05 16:15:33 UTC (rev 863)
@@ -25,7 +25,7 @@
 
 from Response import Response
 from htmltmpl import TemplateError
-import time,os,sys,tempfile,re,types, ConfigParser
+import time,os,sys,tempfile,re,types, ConfigParser, base64
 from shutil import copyfile as COPY
 from shutil import rmtree as RMTREE
 
@@ -749,9 +749,13 @@
 
         # CDATA section in output
         if output.format["mimeType"].find("text") < 0:
-            complexOutput["cdata"] = 1
-        # set output value
-        complexOutput["complexdata"] = open(output.value,"r").read()
+            #complexOutput["cdata"] = 1
+            os.rename(output.value, output.value+".binary")
+            base64.encode(open(output.value+".binary"),open(output.value,"w"))
+            complexOutput["complexdata"] = open(output.value,"r").read()
+        else:
+            # set output value
+            complexOutput["complexdata"] = open(output.value,"r").read()
 
         # remove <?xml version= ... part from beginning of some xml
         # documents



More information about the Pywps-commits mailing list