[Checkins] SVN: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli added

Andreas Jung andreas at andreas-jung.com
Sun Jul 5 08:07:30 EDT 2009


Log message for revision 101572:
  added
  

Changed:
  U   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py
  A   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/
  A   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.html
  A   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.jpg
  A   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.zip
  A   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/zip_client.py

-=-
Modified: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py	2009-07-05 11:58:47 UTC (rev 101571)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py	2009-07-05 12:07:30 UTC (rev 101572)
@@ -1,3 +1,5 @@
+import re
+from zopyx.convert2.convert import Converter
 from twisted.web import xmlrpc, server
 
 class Server(xmlrpc.XMLRPC):
@@ -3,11 +5,72 @@
     """ SmartPrintNG Server """
 
-    def xmlrpc_echo(self, x):
-        """
-        Return all passed args.
-        """
-        return x
+    rxcountpages = re.compile(r"$\s*/Type\s*/Page[/\s]", re.MULTILINE|re.DOTALL)
 
+    def _countPages(self, filename):
+        data = file(filename,"rb").read()
+        return len(self.rxcountpages.findall(data))
 
+    def xmlrpc_convertZIP(self, zip_archive, spool=0, converter_name='pdf-prince'):
+        """ Process html-file + images within a ZIP archive """
+
+        # store zip archive first
+        tempdir = tempfile.mkdtemp()
+        zip_temp = os.path.join(tempdir, 'input.zip')
+        file(zip_temp, 'wb').write(base64.decodestring(zip_archive))
+        ZF = zipfile.ZipFile(zip_temp, 'r')
+        for name in ZF.namelist():
+            destfile = os.path.join(tempdir, name)
+            if not os.path.exists(os.path.dirname(destfile)):
+                os.makedirs(os.path.dirname(destfile))
+            file(destfile, 'wb').write(ZF.read(name))
+        ZF.close()
+
+        # find HTML file
+        html_files = glob.glob(os.path.join(tempdir, '*.htm*'))
+        if not html_files:
+            raise IOError('No HTML files found in %s' % tempdir)
+        html_filename = html_files[0]
+
+        result = self.convert(html_filename, 
+                              spool=spool, 
+                              converter_name=converter_name)
+
+        # Generate result ZIP archive with base64-encoded result
+        zip_out = os.path.join(tempdir, 'output.zip')
+        ZF = zipfile.ZipFile(zip_out, 'w')
+        ZF.writestr('output.pdf', file(result, 'rb').read())
+        ZF.close()
+        encoded_result = base64.encodestring(file(zip_out, 'rb').read())
+        shutil.rmtree(tempdir)
+        return encoded_result
+
+    def xmlrpc_convert(self, html_filename, spool=0, converter_name='pdf-prince'):
+        """ Process a single HTML file """
+
+        start_time = time.time()
+        c = Converter(html_filename)
+        output_filename = c(converter_name)
+        file_size = os.stat(output_filename)[6]
+        duration = time.time() - start_time
+        self.context.addJob(input_filename=html_filename, 
+                            output_filename=output_filename,
+                            output_size=file_size,
+                            duration=duration,
+                            pages=self._countPages(output_filename),
+                            converter_name=converter_name)
+        if spool:
+            id = uuid.uuid1()
+            if not os.path.exists(self.context.spool_directory):
+                os.makedirs(self.context.spool_directory)
+            new_filename = os.path.join(self.context.spool_directory, 
+                                        '%s.pdf' % id)
+            os.rename(output_filename, new_filename)
+            return grok.url(self.request, self.context) + \
+                            '/download?id=%s' % id
+        else:
+            return output_filename
+
+
+
 if __name__ == '__main__':
     from twisted.internet import reactor

Added: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.html
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.html	                        (rev 0)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.html	2009-07-05 12:07:30 UTC (rev 101572)
@@ -0,0 +1,54 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+  <meta http-equiv="Content-Style-Type" content="text/css">
+  <title></title>
+  <meta name="Generator" content="Cocoa HTML Writer">
+  <meta name="CocoaVersion" content="949.35">
+  <style type="text/css">
+    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
+    p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
+  </style>
+</head>
+<body>
+<img src="test.jpg" />
+<p class="p1"><span class="Apple-converted-space">  </span>Further technologies</p>
+<p class="p2"><br></p>
+<p class="p1">Programming languages:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Python, C, C++, Shell, Javascript,</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* SQL, PHP</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* SGML, XML, DOM, SAX, XSL, XSLT, XSL-FO, XPATH etc.</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Java, JDBC, Servlets</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* C, C++</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Middleware (ILU, CORBA)</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* HTML, CSS</p>
+<p class="p2"><br></p>
+<p class="p1">Databases:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Oracle, MySQL, Postgres, ZODB</p>
+<p class="p2"><br></p>
+<p class="p1">Tools:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Apache, Squid, Pound</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Zope, CMF, Plone</p>
+<p class="p2"><br></p>
+<p class="p1">Operating system:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Unix, Linux, Solaris</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Windows</p>
+<p class="p2"><br></p>
+<p class="p1">Applications server:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Zope 2, PHP</p>
+<p class="p2"><br></p>
+<p class="p1">Content-Management-Systems:</p>
+<p class="p2"><br></p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Typo 3</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Zope</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* Plone</p>
+<p class="p1"><span class="Apple-converted-space">    </span>* CPS</p>
+<p class="p2"><br></p>
+</body>
+</html>

Added: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.jpg
===================================================================
(Binary files differ)


Property changes on: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.jpg
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.zip
===================================================================
(Binary files differ)


Property changes on: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/test.zip
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/zip_client.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/zip_client.py	                        (rev 0)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/client_demo/zip_client.py	2009-07-05 12:07:30 UTC (rev 101572)
@@ -0,0 +1,19 @@
+import base64
+import xmlrpclib
+import zipfile
+import tempfile
+
+# XMLRPC server instance
+server = xmlrpclib.Server('http://localhost:11080/demo')
+
+# send the ZIP archive base64 encoded
+zip_data = server.convertZIP(base64.encodestring(file('test.zip', 'rb').read()))
+
+# and receive the result PDF as base64 encoded ZIP archive
+zip_temp = tempfile.mktemp()
+file(zip_temp, 'wb').write(base64.decodestring(zip_data))
+ZF = zipfile.ZipFile(zip_temp, 'r')
+file('output.pdf', 'wb').write(ZF.read('output.pdf'))
+ZF.close()
+
+print 'output.pdf'



More information about the Checkins mailing list