Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sys.stdout = alt_stdout = StringIO()
for filter_, filter_path, module_, extra_dir in filter_paths:
if verbose:
debug("panflute: running filter <{}>".format(filter_))
with ContextImport(module_, extra_dir) as module:
try:
module.main(doc)
except Exception as e:
debug("Failed to run filter: " + filter_)
if not hasattr(module, 'main'):
debug(' - Possible cause: filter lacks a main() function')
debug('Filter code:')
debug('-' * 64)
with open(filter_path) as fp:
debug(fp.read())
debug('-' * 64)
raise Exception(e)
if verbose:
debug("panflute: filter <{}> completed".format(filter_))
alt_stdout_data = alt_stdout.getvalue()
if alt_stdout_data:
debug('[PANFLUTE WARNING] Filter "{}" wrote to stdout, but Pandoc does not allow that'.format(filter_))
debug(alt_stdout_data)
sys.stderr.flush()
sys.stdout = alt_stdout = StringIO()
# Restore stdout
sys.stdout = sys.__stdout__
return doc
debug(msg)
if filters is None:
# metadata 'panflute-filters' can be a list, a string, or missing
# `filters` should be a list of str
filters = doc.get_metadata('panflute-filters', [])
if type(filters) != list:
filters = [filters]
if filters:
if verbose:
msg = "panflute: will run the following filters:"
debug(msg, ' '.join(filters))
doc = autorun_filters(filters, doc, search_dirs, verbose)
elif verbose:
debug("panflute: no filters were provided")
dump(doc, output_stream)
elif verbose:
debug(" filter <{}> NOT found in {}".format(filter_, filter_path))
else:
raise Exception("filter not found: " + filter_)
# Intercept any print() statements made by filters (which would cause Pandoc to fail)
sys.stdout = alt_stdout = StringIO()
for filter_, filter_path, module_, extra_dir in filter_paths:
if verbose:
debug("panflute: running filter <{}>".format(filter_))
with ContextImport(module_, extra_dir) as module:
try:
module.main(doc)
except Exception as e:
debug("Failed to run filter: " + filter_)
if not hasattr(module, 'main'):
debug(' - Possible cause: filter lacks a main() function')
debug('Filter code:')
debug('-' * 64)
with open(filter_path) as fp:
debug(fp.read())
debug('-' * 64)
raise Exception(e)
if verbose:
debug("panflute: filter <{}> completed".format(filter_))
alt_stdout_data = alt_stdout.getvalue()
if alt_stdout_data:
debug('[PANFLUTE WARNING] Filter "{}" wrote to stdout, but Pandoc does not allow that'.format(filter_))
debug(alt_stdout_data)
sys.stderr.flush()
# Display message (tests that everything is working ok)
msg = doc.get_metadata('panflute-echo', False)
if msg:
debug(msg)
if filters is None:
# metadata 'panflute-filters' can be a list, a string, or missing
# `filters` should be a list of str
filters = doc.get_metadata('panflute-filters', [])
if type(filters) != list:
filters = [filters]
if filters:
if verbose:
msg = "panflute: will run the following filters:"
debug(msg, ' '.join(filters))
doc = autorun_filters(filters, doc, search_dirs, verbose)
elif verbose:
debug("panflute: no filters were provided")
dump(doc, output_stream)
if not panfl_:
# default panflute behaviour:
search_dirs.append('.')
if data_dir:
search_dirs.extend(get_filter_dirs())
if sys_path:
search_dirs += sys.path
else:
# panfl/pandoctools behaviour:
if data_dir:
search_dirs.extend(get_filter_dirs())
if sys_path:
search_dirs += reduced_sys_path
if verbose:
debug('panflute will search for filters in the following folders:')
debug(' '.join('"{}"'.format(f) for f in search_dirs))
# Display message (tests that everything is working ok)
msg = doc.get_metadata('panflute-echo', False)
if msg:
debug(msg)
if filters is None:
# metadata 'panflute-filters' can be a list, a string, or missing
# `filters` should be a list of str
filters = doc.get_metadata('panflute-filters', [])
if type(filters) != list:
filters = [filters]
if filters:
if verbose:
else:
module = False
# allow with and without .py ending
path_postfixes = (remove_py(filter_exp) + '.py',)
for path, path_postf in [(path, path_postf)
for path in search_dirs
for path_postf in path_postfixes]:
if p.isabs(path_postf):
filter_path = path_postf
else:
filter_path = p.abspath(p.normpath(p.join(path, path_postf)))
if p.isfile(filter_path):
if verbose:
debug("panflute: filter <{}> found in {}".format(filter_, filter_path))
if module and not (path in reduced_sys_path):
extra_dir = p.abspath(path)
# `path` already doesn't contain `.`, `..`, env vars or `~`
else:
extra_dir = None
module_ = filter_exp if module else filter_path
filter_paths.append((filter_, filter_path, module_, extra_dir))
break
elif p.isabs(path_postf):
if verbose:
debug(" filter <{}> NOT found in {}".format(filter_, filter_path))
raise Exception("filter not found: " + filter_)
elif verbose:
debug(" filter <{}> NOT found in {}".format(filter_, filter_path))
for filter_, filter_path, module_, extra_dir in filter_paths:
if verbose:
debug("panflute: running filter <{}>".format(filter_))
with ContextImport(module_, extra_dir) as module:
try:
module.main(doc)
except Exception as e:
debug("Failed to run filter: " + filter_)
if not hasattr(module, 'main'):
debug(' - Possible cause: filter lacks a main() function')
debug('Filter code:')
debug('-' * 64)
with open(filter_path) as fp:
debug(fp.read())
debug('-' * 64)
raise Exception(e)
if verbose:
debug("panflute: filter <{}> completed".format(filter_))
alt_stdout_data = alt_stdout.getvalue()
if alt_stdout_data:
debug('[PANFLUTE WARNING] Filter "{}" wrote to stdout, but Pandoc does not allow that'.format(filter_))
debug(alt_stdout_data)
sys.stderr.flush()
sys.stdout = alt_stdout = StringIO()
# Restore stdout
sys.stdout = sys.__stdout__
return doc
raise Exception("filter not found: " + filter_)
# Intercept any print() statements made by filters (which would cause Pandoc to fail)
sys.stdout = alt_stdout = StringIO()
for filter_, filter_path, module_, extra_dir in filter_paths:
if verbose:
debug("panflute: running filter <{}>".format(filter_))
with ContextImport(module_, extra_dir) as module:
try:
module.main(doc)
except Exception as e:
debug("Failed to run filter: " + filter_)
if not hasattr(module, 'main'):
debug(' - Possible cause: filter lacks a main() function')
debug('Filter code:')
debug('-' * 64)
with open(filter_path) as fp:
debug(fp.read())
debug('-' * 64)
raise Exception(e)
if verbose:
debug("panflute: filter <{}> completed".format(filter_))
alt_stdout_data = alt_stdout.getvalue()
if alt_stdout_data:
debug('[PANFLUTE WARNING] Filter "{}" wrote to stdout, but Pandoc does not allow that'.format(filter_))
debug(alt_stdout_data)
sys.stderr.flush()
sys.stdout = alt_stdout = StringIO()
# Restore stdout
debug("Failed to run filter: " + filter_)
if not hasattr(module, 'main'):
debug(' - Possible cause: filter lacks a main() function')
debug('Filter code:')
debug('-' * 64)
with open(filter_path) as fp:
debug(fp.read())
debug('-' * 64)
raise Exception(e)
if verbose:
debug("panflute: filter <{}> completed".format(filter_))
alt_stdout_data = alt_stdout.getvalue()
if alt_stdout_data:
debug('[PANFLUTE WARNING] Filter "{}" wrote to stdout, but Pandoc does not allow that'.format(filter_))
debug(alt_stdout_data)
sys.stderr.flush()
sys.stdout = alt_stdout = StringIO()
# Restore stdout
sys.stdout = sys.__stdout__
return doc