Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/zopyx/convert2/calibre.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
from tidy import tidyhtml

def _check_calibre():
if not which('ebook-convert'):
return False
return True
return bool(which('ebook-convert'))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_calibre refactored with the following changes:


calibre_available = _check_calibre()

def html2calibre(html_filename, output_filename=None, cmdopts='', **calibre_options):
""" 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')
Comment on lines -29 to +27

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function html2calibre refactored with the following changes:

html_filename += '.html'

if not output_filename:
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/zopyx/convert2/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Converter.convert refactored with the following changes:


if format == 'fo':
c = converter()
Expand Down Expand Up @@ -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)
Comment on lines -64 to +65

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseConverter.__del__ refactored with the following changes:


16 changes: 6 additions & 10 deletions src/zopyx/convert2/fo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,19 @@ 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')

Comment on lines -43 to +47

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTML2FO.convert refactored with the following changes:

cmd = '"%s"' % java + \
' -Duser.language=en -Xms256m -Xmx256m -jar "%(csstoxslfo)s" "%(filename)s" -fo "%(fo_filename)s"' % vars()
for k in kw:
cmd += ' %s="%s"' % (k, kw[k])

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:
Expand All @@ -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

Expand Down Expand Up @@ -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('<ns0:block ' , '<ns0:block margin-top="0" margin-bottom="0" ') # avoid a linebreak through <li><p> (XFC)
# fo_text = fo_text.replace('<ns0:block/>', '') # causes a crash with XINC
Expand Down
9 changes: 4 additions & 5 deletions src/zopyx/convert2/fop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Comment on lines -21 to +24

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_fop refactored with the following changes:

return False

return True
Expand All @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fo2pdf refactored with the following changes:


return dict(output_filename=output_filename,
status=status,
Expand All @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTML2PDF.convert refactored with the following changes:


fop_available = _check_fop()

Expand Down
8 changes: 3 additions & 5 deletions src/zopyx/convert2/pdfreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
from exceptions import ConversionError

def _check_pdfreactor():
if not which('pdfreactor'):
return False
return True
return bool(which('pdfreactor'))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_pdfreactor refactored with the following changes:


pdfreactor_available = _check_pdfreactor()

Expand All @@ -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)
Comment on lines -35 to +36

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function html2pdf refactored with the following changes:

return dict(output_filename=output_filename,
status=status,
output=output)
Expand Down
3 changes: 1 addition & 2 deletions src/zopyx/convert2/pisa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTML2PDF.convert refactored with the following changes:


from registry import registerConverter
registerConverter(HTML2PDF)
Expand Down
3 changes: 1 addition & 2 deletions src/zopyx/convert2/pisa_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTML2PDF.convert refactored with the following changes:


from registry import registerConverter
registerConverter(HTML2PDF)
Expand Down
12 changes: 5 additions & 7 deletions src/zopyx/convert2/prince.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
from exceptions import ConversionError

def _check_prince():
if not which('prince'):
return False
return True
return bool(which('prince'))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_prince refactored with the following changes:


prince_available = _check_prince()

Expand All @@ -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} ')
Comment on lines -33 to +34

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function html2pdf refactored with the following changes:

else:
cmd_options.append('--%s="%s" ' % (k, v))

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/zopyx/convert2/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
A simple converter registry
"""


# map converter name to converter class
converter_registry = dict()
converter_registry = {}
Comment on lines +11 to +13

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12-12 refactored with the following changes:


def registerConverter(converter_cls):
converter_registry[converter_cls.name] = converter_cls
Expand Down
7 changes: 3 additions & 4 deletions src/zopyx/convert2/tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''
Comment on lines -51 to +54

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tidyhtml.handler refactored with the following changes:


entity_reg = re.compile('(&.*?;)')
html = entity_reg.sub(handler, html)
Expand Down
2 changes: 1 addition & 1 deletion src/zopyx/convert2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function checkEnvironment refactored with the following changes:

return False

if not os.path.exists(dirname):
Expand Down
10 changes: 5 additions & 5 deletions src/zopyx/convert2/xfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_xfc refactored with the following changes:

return False

return True
Expand All @@ -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}')
Comment on lines -36 to +40

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fo2xfc refactored with the following changes:


if sys.platform == 'win32':
cmd = '"%s\\fo2%s.bat" "%s" "%s"' % (xfc_dir, format, fo_filename, output_filename)
Expand All @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions src/zopyx/convert2/xinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Comment on lines -22 to +25

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_xinc refactored with the following changes:

return False

return True
Expand All @@ -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)
Expand All @@ -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()
Expand Down