[Checkins] SVN: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py better error handling and logging

Andreas Jung andreas at andreas-jung.com
Sun Jul 5 11:15:32 EDT 2009


Log message for revision 101584:
  better error handling and logging
  

Changed:
  U   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py

-=-
Modified: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py	2009-07-05 14:04:51 UTC (rev 101583)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/cli.py	2009-07-05 15:15:32 UTC (rev 101584)
@@ -25,50 +25,54 @@
     def xmlrpc_convertZIP(self, zip_archive, converter_name='pdf-prince'):
         """ Process html-file + images within a ZIP archive """
 
-        LOG.debug('Incoming request (%s, %d)' % (converter_name, len(zip_archive)))
-        ts = time.time()
-        # 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()
+        def _convertZIP(zip_archive, converter_name):
 
-        # 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]
+            LOG.debug('Incoming request (%s, %d)' % (converter_name, len(zip_archive)))
+            ts = time.time()
+            # 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()
 
-        result = self.convert(html_filename, 
-                              converter_name=converter_name)
+            # find HTML file
+            html_files = glob.glob(os.path.join(tempdir, '*.htm*'))
+            if not html_files:
+                raise IOError('Archive does not contain any html files')
+            if len(html_files) > 1:
+                raise RuntimeError('Archive contains more than one html file')
+            html_filename = html_files[0]
+            result = self.convert(html_filename, 
+                                  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)
-        LOG.debug('request end (%3.2lf seconds)' % (time.time() -ts))
-        return encoded_result
+            # 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)
+            LOG.debug('request end (%3.2lf seconds)' % (time.time() -ts))
+            return encoded_result
 
+        try:
+            return _convertZIP(zip_archive, converter_name)
+        except Exception, e:
+            msg = 'Conversion failed (%s)' % e
+            LOG.error(msg, exc_info=True)
+            return xmlrpc.Fault(123, msg)
+
     def convert(self, html_filename, converter_name='pdf-prince'):
         """ Process a single HTML file """
+        return Converter(html_filename)(converter_name)
 
-        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
-        return output_filename
 
-
 def main():
     from twisted.internet import reactor
     from optparse import OptionParser
@@ -78,7 +82,7 @@
                       help="port", default=7080)
 
     (options, args) = parser.parse_args()
-    print 'Started SmartPrintNG XMLRPC server(port %d)' % options.port
+    LOG.debug('Started SmartPrintNG XMLRPC server(port %d)' % options.port)
     r = Server()
     reactor.listenTCP(options.port, server.Site(r))
     reactor.run()



More information about the Checkins mailing list