[Checkins] SVN: zopyx.convert2/trunk/ merged calibre-integration

Andreas Jung andreas at andreas-jung.com
Sat Sep 5 08:56:59 EDT 2009


Log message for revision 103593:
  merged calibre-integration
  

Changed:
  U   zopyx.convert2/trunk/CHANGES.txt
  U   zopyx.convert2/trunk/setup.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/__init__.py
  A   zopyx.convert2/trunk/src/zopyx/convert2/calibre.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/cli.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/fop.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/prince.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/util.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/xfc.py
  U   zopyx.convert2/trunk/src/zopyx/convert2/xinc.py

-=-
Modified: zopyx.convert2/trunk/CHANGES.txt
===================================================================
--- zopyx.convert2/trunk/CHANGES.txt	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/CHANGES.txt	2009-09-05 12:56:58 UTC (rev 103593)
@@ -1,3 +1,7 @@
+2.1.0 (unreleased)
+==================
+- Calibre integration
+
 2.0.4 (2009-07-07)
 ====================
 - pinned BeautifulSoup 3.0.x

Modified: zopyx.convert2/trunk/setup.py
===================================================================
--- zopyx.convert2/trunk/setup.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/setup.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -12,7 +12,7 @@
     'Programming Language :: Python',
 ]
 
-version = '2.0.4'
+version = '2.1.0-dev'
 
 desc = unicode(file('README.txt').read().strip(), 'utf-8')
 changes = file('CHANGES.txt').read().strip()

Modified: zopyx.convert2/trunk/src/zopyx/convert2/__init__.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/__init__.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/__init__.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -10,6 +10,7 @@
 import fop
 import prince
 import xfc
+import calibre
 from convert import Converter
 
 

Copied: zopyx.convert2/trunk/src/zopyx/convert2/calibre.py (from rev 103592, zopyx.convert2/branches/calibre-integration/src/zopyx/convert2/calibre.py)
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/calibre.py	                        (rev 0)
+++ zopyx.convert2/trunk/src/zopyx/convert2/calibre.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -0,0 +1,79 @@
+##########################################################################
+# zopyx.convert2 - SmartPrintNG low-level functionality
+#
+# (C) 2007, 2008, ZOPYX Ltd & Co. KG, Tuebingen, Germany
+##########################################################################
+
+import os
+import sys
+import shutil
+
+from convert import BaseConverter
+from util import runcmd, which, win32, checkEnvironment, newTempfile
+from logger import LOG
+
+from tidy import tidyhtml
+
+def _check_calibre():
+    if not which('ebook-convert'):
+        return False
+    return True
+
+calibre_available = _check_calibre()
+
+def html2calibre(html_filename, output_filename=None, **calibre_options):
+    """ Convert a HTML file using calibre """
+
+    if not html_filename.endswith('.html'):
+        shutil.copy(html_filename, html_filename + '.html')
+        html_filename += '.html'
+
+    if not output_filename:
+        output_filename = newTempfile(suffix='.epub')
+
+    if not calibre_available:
+        raise RuntimeError("The external calibre converter isn't available")
+
+    options = list()
+    for k,v in calibre_options.items():
+        if v is None:
+            options.append('--%s ' % k)
+        else:
+            options.append('--%s="%s" ' % (k, v)) 
+
+    if sys.platform == 'win32':
+        raise NotImplementedError('No support for using Calibre on Windows available')
+    else:
+        cmd = '"ebook-convert" "%s" "%s" %s' % (html_filename, output_filename, ' '.join(options))
+    
+    status, output = runcmd(cmd)
+    if status != 0:
+        raise RuntimeError('Error executing: %s' % cmd)
+    return dict(output_filename=output_filename,
+                status=status,
+                output=output)
+
+
+class HTML2Calibre(BaseConverter):
+
+    name = 'ebook-calibre'
+    output_format = 'epub'
+    visible_name = 'EPUB (Calibre)'
+    visible = True
+
+    @staticmethod
+    def available():
+        return calibre_available
+
+    def convert(self, output_filename=None, **calibre_options):
+        tidy_filename = tidyhtml(self.filename, self.encoding)
+        result = html2calibre(tidy_filename, output_filename, **calibre_options)
+        os.unlink(tidy_filename)
+        return result
+
+
+from registry import registerConverter
+registerConverter(HTML2Calibre)
+
+if __name__ == '__main__':
+    print html2calibre(sys.argv[1], 'output.epub')

Modified: zopyx.convert2/trunk/src/zopyx/convert2/cli.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/cli.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/cli.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -36,7 +36,8 @@
                 print '%s: %s.html -> %s.%s' % (name, tmpf, tmpf, cls.output_format)
                 C = Converter(tmpf + '.html', verbose=True)
                 try:
-                    output_filename = C(name, output_filename=tmpf + '.' + cls.output_format)
+                    result = C(name, output_filename=tmpf + '.' + cls.output_format)
+                    print result
                 except Exception, e:
                     print 'FAILED (%s)' % e
 
@@ -48,9 +49,9 @@
 
         for fn in args:
             C = Converter(fn, verbose=options.verbose)
-            output_filename = C(options.format, 
+            result = C(options.format, 
                                 output_filename=options.output_filename)
-            print 'Generated file: %s' % output_filename
+            print 'Generated file: %s' % result['output_filename']
    
 
 def main():

Modified: zopyx.convert2/trunk/src/zopyx/convert2/fop.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/fop.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/fop.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -42,9 +42,12 @@
     status, output = runcmd(cmd)
     if status != 0:
         raise RuntimeError('Error executing: %s' % cmd)
-    return output_filename
 
+    return dict(output_filename=output_filename,
+                status=status,
+                output=output)
 
+
 class HTML2PDF(BaseConverter):
 
     name = 'pdf-fop'
@@ -58,9 +61,9 @@
 
     def convert(self, output_filename=None, **options):
         self.convert2FO(**options)
-        return fo2pdf(self.fo_filename, output_filename)
+        result = fo2pdf(self.fo_filename, output_filename)
+        return result
 
 fop_available = _check_fop()
 
 from registry import registerConverter
-registerConverter(HTML2PDF)

Modified: zopyx.convert2/trunk/src/zopyx/convert2/prince.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/prince.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/prince.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -44,7 +44,9 @@
     status, output = runcmd(cmd)
     if status != 0:
         raise RuntimeError('Error executing: %s' % cmd)
-    return output_filename
+    return dict(output_filename=output_filename,
+                status=status,
+                output=output)
 
 
 class HTML2PDF(BaseConverter):
@@ -60,9 +62,9 @@
 
     def convert(self, output_filename=None, **prince_options):
         tidy_filename = tidyhtml(self.filename, self.encoding)
-        output_filename = html2pdf(tidy_filename, output_filename, **prince_options)
+        result = html2pdf(tidy_filename, output_filename, **prince_options)
         os.unlink(tidy_filename)
-        return output_filename
+        return result
 
 
 from registry import registerConverter
@@ -74,4 +76,4 @@
                                               'disallow-copy' : None,
                                               'disallow-modify' : None,
                                               'owner-password' : 'foo1',
-                                              'user-password' : 'foo'})
+                                              'user-password' : 'foo'})['output_filename']

Modified: zopyx.convert2/trunk/src/zopyx/convert2/util.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/util.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/util.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -8,6 +8,7 @@
 import sys
 import tempfile
 
+import commands
 from subprocess import Popen, PIPE
 from logger import LOG
 
@@ -34,6 +35,9 @@
             status = os.system(cmd)
             return status, ''
 
+        elif execute_mode == 'commands':
+            return commands.getstatusoutput(cmd)
+
         elif execute_mode == 'process':
 
             stdin = open('/dev/null')

Modified: zopyx.convert2/trunk/src/zopyx/convert2/xfc.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/xfc.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/xfc.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -46,8 +46,11 @@
     status, output = runcmd(cmd)
     if status != 0:
         raise RuntimeError('Error executing: %s' % cmd)
-    return output_filename
 
+    return dict(output_filename=output_filename,
+                status=status,
+                output=output)
+
 class RTFConverter(BaseConverter):
 
     name = 'rtf-xfc'

Modified: zopyx.convert2/trunk/src/zopyx/convert2/xinc.py
===================================================================
--- zopyx.convert2/trunk/src/zopyx/convert2/xinc.py	2009-09-05 12:52:28 UTC (rev 103592)
+++ zopyx.convert2/trunk/src/zopyx/convert2/xinc.py	2009-09-05 12:56:58 UTC (rev 103593)
@@ -42,9 +42,13 @@
     status, output = runcmd(cmd)
     if status != 0:
         raise RuntimeError('Error executing: %s' % cmd)
-    return output_filename
+    
+    return dict(output_filename=output_filename,
+                status=status,
+                output=output)
 
 
+
 class HTML2PDF(BaseConverter):
 
     name = 'pdf-xinc'
@@ -57,9 +61,9 @@
         return xinc_available
 
     def convert(self, output_filename=None, **options):
-
         self.convert2FO(**options)
-        return fo2pdf(self.fo_filename, output_filename)
+        result = fo2pdf(self.fo_filename, output_filename)
+        return result
 
 
 xinc_available = _check_xinc()



More information about the checkins mailing list