diff --git a/src/zopyx/convert2/calibre.py b/src/zopyx/convert2/calibre.py index 4714c1b..c994e2c 100755 --- a/src/zopyx/convert2/calibre.py +++ b/src/zopyx/convert2/calibre.py @@ -16,9 +16,7 @@ from tidy import tidyhtml def _check_calibre(): - if not which('ebook-convert'): - return False - return True + return bool(which('ebook-convert')) calibre_available = _check_calibre() @@ -26,7 +24,7 @@ def html2calibre(html_filename, output_filename=None, cmdopts='', **calibre_opti """ Convert a HTML file using calibre """ if not html_filename.endswith('.html'): - shutil.copy(html_filename, html_filename + '.html') + shutil.copy(html_filename, f'{html_filename}.html') html_filename += '.html' if not output_filename: @@ -35,23 +33,22 @@ def html2calibre(html_filename, output_filename=None, cmdopts='', **calibre_opti if not calibre_available: raise RuntimeError("The external calibre converter isn't available") - options = list() + options = [] for k,v in calibre_options.items(): if v is None: - options.append('--%s ' % k) + options.append(f'--{k} ') else: options.append('--%s="%s" ' % (k, v)) if sys.platform == 'win32': raise NotImplementedError('No support for using Calibre on Windows available') - else: - options = ' '.join(options) - options = options + ' ' + cmdopts - cmd = '"ebook-convert" "%s" "%s" %s' % (html_filename, output_filename, options) - + options = ' '.join(options) + options = f'{options} {cmdopts}' + cmd = '"ebook-convert" "%s" "%s" %s' % (html_filename, output_filename, options) + status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, diff --git a/src/zopyx/convert2/convert.py b/src/zopyx/convert2/convert.py index b90649f..5867311 100755 --- a/src/zopyx/convert2/convert.py +++ b/src/zopyx/convert2/convert.py @@ -21,7 +21,7 @@ def convert(self, format, output_filename=None, options={}): converter = registry.converter_registry.get(format) if converter is None: - raise ValueError('Unsupported format: %s' % format) + raise ValueError(f'Unsupported format: {format}') if format == 'fo': c = converter() @@ -61,7 +61,6 @@ def convert(self, format, output_filename=None, **options): def __del__(self): """ House-keeping """ - if self.cleanup: - if self.fo_filename: - os.unlink(self.fo_filename) + if self.cleanup and self.fo_filename: + os.unlink(self.fo_filename) diff --git a/src/zopyx/convert2/fo.py b/src/zopyx/convert2/fo.py index b054caf..4a50682 100755 --- a/src/zopyx/convert2/fo.py +++ b/src/zopyx/convert2/fo.py @@ -40,15 +40,11 @@ def convert(self, filename, encoding='utf-8', tidy=True, output_filename=None, * if tidy: filename = tidyhtml(filename, encoding, strip_base=kw.get('strip_base', False)) - if output_filename: - fo_filename = output_filename - else: - fo_filename = newTempfile(suffix='.fo') - + fo_filename = output_filename or newTempfile(suffix='.fo') csstoxslfo = os.path.abspath(os.path.join(dirname, 'lib', 'csstoxslfo', 'css2xslfo.jar')) if not os.path.exists(csstoxslfo): - raise IOError('%s does not exist' % csstoxslfo) - + raise IOError(f'{csstoxslfo} does not exist') + cmd = '"%s"' % java + \ ' -Duser.language=en -Xms256m -Xmx256m -jar "%(csstoxslfo)s" "%(filename)s" -fo "%(fo_filename)s"' % vars() for k in kw: @@ -56,7 +52,7 @@ def convert(self, filename, encoding='utf-8', tidy=True, output_filename=None, * status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) # remove tidy-ed file if tidy: @@ -67,7 +63,7 @@ def convert(self, filename, encoding='utf-8', tidy=True, output_filename=None, * E = parse(fo_filename) - ids_seen = list() + ids_seen = [] for node in E.getiterator(): get = node.attrib.get @@ -114,7 +110,7 @@ def convert(self, filename, encoding='utf-8', tidy=True, output_filename=None, * 'wrap-option' : 'no-wrap', 'linefeed-treatment' : 'preserve' }.items(): node.attrib[k] = v - + fo_text = tostring(E.getroot()) fo_text = fo_text.replace('

(XFC) # fo_text = fo_text.replace('', '') # causes a crash with XINC diff --git a/src/zopyx/convert2/fop.py b/src/zopyx/convert2/fop.py index 3adace9..e5555fe 100755 --- a/src/zopyx/convert2/fop.py +++ b/src/zopyx/convert2/fop.py @@ -18,10 +18,10 @@ def _check_fop(): if not checkEnvironment('FOP_HOME'): return False - exe_name = win32 and 'fop.bat' or 'fop' + exe_name = 'fop.bat' if win32 else 'fop' full_exe_name = os.path.join(fop_home, exe_name) if not os.path.exists(full_exe_name): - LOG.debug('%s does not exist' % full_exe_name) + LOG.debug(f'{full_exe_name} does not exist') return False return True @@ -43,7 +43,7 @@ def fo2pdf(fo_filename, output_filename=None): status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, @@ -64,8 +64,7 @@ def available(): def convert(self, output_filename=None, **options): options['strip_base'] = True self.convert2FO(**options) - result = fo2pdf(self.fo_filename, output_filename) - return result + return fo2pdf(self.fo_filename, output_filename) fop_available = _check_fop() diff --git a/src/zopyx/convert2/pdfreactor.py b/src/zopyx/convert2/pdfreactor.py index 9870cd7..6c57cbe 100644 --- a/src/zopyx/convert2/pdfreactor.py +++ b/src/zopyx/convert2/pdfreactor.py @@ -15,9 +15,7 @@ from exceptions import ConversionError def _check_pdfreactor(): - if not which('pdfreactor'): - return False - return True + return bool(which('pdfreactor')) pdfreactor_available = _check_pdfreactor() @@ -32,10 +30,10 @@ def html2pdf(html_filename, output_filename=None, **options): cmd = '%s "pdfreactor" "%s" "%s"' % \ (execution_shell, html_filename, output_filename) - + status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, output=output) diff --git a/src/zopyx/convert2/pisa.py b/src/zopyx/convert2/pisa.py index d350157..3cbd9e2 100755 --- a/src/zopyx/convert2/pisa.py +++ b/src/zopyx/convert2/pisa.py @@ -47,8 +47,7 @@ def available(): return True def convert(self, output_filename=None, **options): - result = html2pdf(self.filename, output_filename, **options) - return result + return html2pdf(self.filename, output_filename, **options) from registry import registerConverter registerConverter(HTML2PDF) diff --git a/src/zopyx/convert2/pisa_bin.py b/src/zopyx/convert2/pisa_bin.py index 31df42a..e414bf8 100755 --- a/src/zopyx/convert2/pisa_bin.py +++ b/src/zopyx/convert2/pisa_bin.py @@ -41,8 +41,7 @@ def available(): return True def convert(self, output_filename=None, **options): - result = html2pdf(self.filename, output_filename, **options) - return result + return html2pdf(self.filename, output_filename, **options) from registry import registerConverter registerConverter(HTML2PDF) diff --git a/src/zopyx/convert2/prince.py b/src/zopyx/convert2/prince.py index 690e078..60102f7 100755 --- a/src/zopyx/convert2/prince.py +++ b/src/zopyx/convert2/prince.py @@ -15,9 +15,7 @@ from exceptions import ConversionError def _check_prince(): - if not which('prince'): - return False - return True + return bool(which('prince')) prince_available = _check_prince() @@ -30,10 +28,10 @@ def html2pdf(html_filename, output_filename=None, **options): if not prince_available: raise RuntimeError("The external PrinceXML converter isn't available") - cmd_options = list() + cmd_options = [] for k,v in options.items(): if v is None: - cmd_options.append('--%s ' % k) + cmd_options.append(f'--{k} ') else: cmd_options.append('--%s="%s" ' % (k, v)) @@ -42,10 +40,10 @@ def html2pdf(html_filename, output_filename=None, **options): else: cmd = '%s "prince" "%s" %s -o "%s"' % \ (execution_shell, html_filename, ' '.join(cmd_options), output_filename) - + status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, output=output) diff --git a/src/zopyx/convert2/registry.py b/src/zopyx/convert2/registry.py index 1135870..3187305 100644 --- a/src/zopyx/convert2/registry.py +++ b/src/zopyx/convert2/registry.py @@ -8,8 +8,9 @@ A simple converter registry """ + # map converter name to converter class -converter_registry = dict() +converter_registry = {} def registerConverter(converter_cls): converter_registry[converter_cls.name] = converter_cls diff --git a/src/zopyx/convert2/tidy.py b/src/zopyx/convert2/tidy.py index 64a8ad4..e819bc1 100755 --- a/src/zopyx/convert2/tidy.py +++ b/src/zopyx/convert2/tidy.py @@ -48,11 +48,10 @@ def handler(mo): """ Callback to convert entities """ e = mo.group(1) v = e[1:-1] - if not v.startswith('#'): - codepoint = name2codepoint.get(v) - return codepoint and '&#%d;' % codepoint or '' - else: + if v.startswith('#'): return e + codepoint = name2codepoint.get(v) + return codepoint and '&#%d;' % codepoint or '' entity_reg = re.compile('(&.*?;)') html = entity_reg.sub(handler, html) diff --git a/src/zopyx/convert2/util.py b/src/zopyx/convert2/util.py index 94a9b81..6ead417 100755 --- a/src/zopyx/convert2/util.py +++ b/src/zopyx/convert2/util.py @@ -71,7 +71,7 @@ def checkEnvironment(envname): dirname = os.environ.get(envname, None) if dirname is None: - LOG.debug('Environment variable $%s is unset' % envname) + LOG.debug(f'Environment variable ${envname} is unset') return False if not os.path.exists(dirname): diff --git a/src/zopyx/convert2/xfc.py b/src/zopyx/convert2/xfc.py index a27d6c8..4448cd2 100755 --- a/src/zopyx/convert2/xfc.py +++ b/src/zopyx/convert2/xfc.py @@ -22,7 +22,7 @@ def _check_xfc(): # converters are also installed properly) full_exe_name = os.path.join(xfc_dir, 'fo2rtf') if not os.path.exists(full_exe_name): - LOG.debug('%s does not exist' % full_exe_name) + LOG.debug(f'{full_exe_name} does not exist') return False return True @@ -33,11 +33,11 @@ def fo2xfc(fo_filename, format='rtf', output_filename=None): through XFC-4.0. """ - if not format in ('rtf', 'docx', 'wml', 'odt'): - raise ValueError('Unsupported format: %s' % format) + if format not in ('rtf', 'docx', 'wml', 'odt'): + raise ValueError(f'Unsupported format: {format}') if not output_filename: - output_filename = newTempfile(suffix='.%s' % format) + output_filename = newTempfile(suffix=f'.{format}') if sys.platform == 'win32': cmd = '"%s\\fo2%s.bat" "%s" "%s"' % (xfc_dir, format, fo_filename, output_filename) @@ -47,7 +47,7 @@ def fo2xfc(fo_filename, format='rtf', output_filename=None): status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, diff --git a/src/zopyx/convert2/xinc.py b/src/zopyx/convert2/xinc.py index d19ae11..e932de8 100755 --- a/src/zopyx/convert2/xinc.py +++ b/src/zopyx/convert2/xinc.py @@ -19,10 +19,10 @@ def _check_xinc(): if not checkEnvironment('XINC_HOME'): return False - exe_name = win32 and '\\bin\\windows\\xinc.exe' or 'bin/unix/xinc' + exe_name = '\\bin\\windows\\xinc.exe' if win32 else 'bin/unix/xinc' full_exe_name = os.path.join(xinc_home, exe_name) if not os.path.exists(full_exe_name): - LOG.debug('%s does not exist' % full_exe_name) + LOG.debug(f'{full_exe_name} does not exist') return False return True @@ -43,7 +43,7 @@ def fo2pdf(fo_filename, output_filename=None): status, output = runcmd(cmd) if status != 0: - raise ConversionError('Error executing: %s' % cmd, output) + raise ConversionError(f'Error executing: {cmd}', output) return dict(output_filename=output_filename, status=status, output=output) @@ -63,8 +63,7 @@ def available(): def convert(self, output_filename=None, **options): self.convert2FO(**options) - result = fo2pdf(self.fo_filename, output_filename) - return result + return fo2pdf(self.fo_filename, output_filename) xinc_available = _check_xinc()