-
Notifications
You must be signed in to change notification settings - Fork 6
Sourcery refactored master branch #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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')) | ||
|
|
||
| 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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) | ||
|
Comment on lines
-64
to
+65
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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: | ||
|
|
@@ -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('<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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| fop_available = _check_fop() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,7 @@ | |
| from exceptions import ConversionError | ||
|
|
||
| def _check_pdfreactor(): | ||
| if not which('pdfreactor'): | ||
| return False | ||
| return True | ||
| return bool(which('pdfreactor')) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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) | ||
|
Comment on lines
-35
to
+36
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return dict(output_filename=output_filename, | ||
| status=status, | ||
| output=output) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| from registry import registerConverter | ||
| registerConverter(HTML2PDF) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| from registry import registerConverter | ||
| registerConverter(HTML2PDF) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,7 @@ | |
| from exceptions import ConversionError | ||
|
|
||
| def _check_prince(): | ||
| if not which('prince'): | ||
| return False | ||
| return True | ||
| return bool(which('prince')) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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} ') | ||
|
Comment on lines
-33
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
|
||
| def registerConverter(converter_cls): | ||
| converter_registry[converter_cls.name] = converter_cls | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| entity_reg = re.compile('(&.*?;)') | ||
| html = entity_reg.sub(handler, html) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return False | ||
|
|
||
| if not os.path.exists(dirname): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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}') | ||
|
Comment on lines
-36
to
+40
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
_check_calibrerefactored with the following changes:boolean-if-exp-identity)reintroduce-else)assign-if-exp)