Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ensure_pandoc_installed(url=None, targetfolder=None, version="latest", quiet=False, delete_installer=False):
"""Try to install pandoc if it isn't installed.
Parameters are passed to download_pandoc()
:raises OSError: if pandoc cannot be installed
"""
try:
# Perform the test quietly if asked
_ensure_pandoc_path(quiet=quiet)
except OSError:
download_pandoc(url=url, targetfolder=targetfolder, version=version, quiet=quiet, delete_installer=delete_installer)
# Show errors in case of secondary failure
_ensure_pandoc_path(quiet=False)
# First, try to use convert_file, assuming Pandoc is already installed.
# If that fails, try to download & install it, and then try to convert
# again.
# noinspection PyBroadException
try:
# pandoc, you rock...
rst_content = convert_file(md_path, 'rst')
with open(rst_path, 'w') as rst_file:
for line in rst_content.splitlines(keepends=False):
rst_file.write(line + '\n')
except Exception:
try:
# noinspection PyUnresolvedReferences,PyPackageRequirements
from pypandoc.pandoc_download import download_pandoc
download_pandoc()
except FileNotFoundError:
warnings.warn("Unable to download & install pandoc. Unable to generate README.rst.")
else:
# pandoc, you rock...
rst_content = convert_file(md_path, 'rst')
with open(rst_path, 'w') as rst_file:
for line in rst_content.splitlines(keepends=False):
rst_file.write(line + '\n')
if os.path.isfile(rst_path):
with open(rst_path) as rst_file:
return rst_file.read()
else:
# It will be messy, but it's better than nothing...
with open(md_path) as md_file:
return md_file.read()
def run(self):
from pypandoc.pandoc_download import download_pandoc
targetfolder = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pypandoc", "files")
download_pandoc(targetfolder=targetfolder)