Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse(self, inputstring, parser, preargs, postargs):
"""Use the parser to parse the inputstring with appropriate setup and teardown."""
self.reset()
pre_procd = None
with logger.gather_parsing_stats():
try:
pre_procd = self.pre(inputstring, **preargs)
parsed = parse(parser, pre_procd)
out = self.post(parsed, **postargs)
except ParseBaseException as err:
raise self.make_parse_err(err)
except CoconutDeferredSyntaxError as err:
internal_assert(pre_procd is not None, "invalid deferred syntax error in pre-processing", err)
raise self.make_syntax_err(err, pre_procd)
except RuntimeError as err:
raise CoconutException(
str(err), extra="try again with --recursion-limit greater than the current "
+ str(sys.getrecursionlimit()),
)
if self.strict:
for name in self.unused_imports:
if name != "*":
logger.warn("found unused import", name, extra="disable --strict to dismiss")
return out
def handling_exceptions(self):
"""Perform proper exception handling."""
try:
if self.using_jobs:
with handling_broken_process_pool():
yield
else:
yield
except SystemExit as err:
self.register_error(err.code)
except BaseException as err:
if isinstance(err, CoconutException):
logger.display_exc()
elif not isinstance(err, KeyboardInterrupt):
traceback.print_exc()
printerr(report_this_text)
self.register_error(errmsg=err.__class__.__name__)
if args.mypy is not None:
self.set_mypy_args(args.mypy)
if args.argv is not None:
sys.argv = [args.source if args.source is not None else ""]
sys.argv.extend(args.argv)
if args.source is not None:
if args.interact and args.run:
logger.warn("extraneous --run argument passed; --interact implies --run")
if args.package and self.mypy:
logger.warn("extraneous --package argument passed; --mypy implies --package")
if args.standalone and args.package:
raise CoconutException("cannot compile as both --package and --standalone")
if args.standalone and self.mypy:
raise CoconutException("cannot compile as both --package (implied by --mypy) and --standalone")
if args.no_write and self.mypy:
raise CoconutException("cannot compile with --no-write when using --mypy")
if (args.run or args.interact) and os.path.isdir(args.source):
if args.run:
raise CoconutException("source path must point to file not directory when --run is enabled")
if args.interact:
raise CoconutException("source path must point to file not directory when --run (implied by --interact) is enabled")
if args.watch and os.path.isfile(args.source):
raise CoconutException("source path must point to directory not file when --watch is enabled")
if args.dest is None:
if args.no_write:
dest = False # no dest
else:
def parse(code="", mode="sys"):
"""Compile Coconut code."""
if CLI.comp is None:
setup()
if mode in PARSERS:
return PARSERS[mode](CLI.comp)(code)
else:
raise CoconutException(
"invalid parse mode " + ascii(mode),
extra="valid modes are " + ", ".join(PARSERS),
)
class CoconutSyntaxWarning(CoconutSyntaxError, CoconutWarning):
"""CoconutWarning with CoconutSyntaxError semantics."""
class CoconutInternalException(CoconutException):
"""Internal Coconut exception."""
def message(self, message, item, extra):
"""Creates the Coconut internal exception message."""
return (
super(CoconutInternalException, self).message(message, item, extra)
+ " " + report_this_text
)
class CoconutDeferredSyntaxError(CoconutException):
"""Deferred Coconut SyntaxError."""
def __init__(self, message, loc):
"""Creates the Coconut exception."""
self.args = (message, loc)
def message(self, message, loc):
"""Uses arguments to create the message."""
return message
def setup(self, target=None, strict=False, minify=False, line_numbers=False, keep_lines=False, no_tco=False):
"""Initializes parsing parameters."""
if target is None:
target = ""
else:
target = str(target).replace(".", "")
if target in pseudo_targets:
target = pseudo_targets[target]
if target not in targets:
raise CoconutException(
"unsupported target Python version " + ascii(target),
extra="supported targets are " + ', '.join(ascii(t) for t in specific_targets) + ", or leave blank for universal",
)
logger.log_vars("Compiler args:", locals())
self.target = target
self.strict = strict
self.minify = minify
self.line_numbers = line_numbers
self.keep_lines = keep_lines
self.no_tco = no_tco
if args.interact and args.run:
logger.warn("extraneous --run argument passed; --interact implies --run")
if args.package and self.mypy:
logger.warn("extraneous --package argument passed; --mypy implies --package")
if args.standalone and args.package:
raise CoconutException("cannot compile as both --package and --standalone")
if args.standalone and self.mypy:
raise CoconutException("cannot compile as both --package (implied by --mypy) and --standalone")
if args.no_write and self.mypy:
raise CoconutException("cannot compile with --no-write when using --mypy")
if (args.run or args.interact) and os.path.isdir(args.source):
if args.run:
raise CoconutException("source path must point to file not directory when --run is enabled")
if args.interact:
raise CoconutException("source path must point to file not directory when --run (implied by --interact) is enabled")
if args.watch and os.path.isfile(args.source):
raise CoconutException("source path must point to directory not file when --watch is enabled")
if args.dest is None:
if args.no_write:
dest = False # no dest
else:
dest = True # auto-generate dest
elif args.no_write:
raise CoconutException("destination path cannot be given when --no-write is enabled")
else:
dest = args.dest
source = fixpath(args.source)
if args.package or self.mypy:
def compile_path(self, path, write=True, package=True, *args, **kwargs):
"""Compile a path and returns paths to compiled files."""
if not isinstance(write, bool):
write = fixpath(write)
if os.path.isfile(path):
destpath = self.compile_file(path, write, package, *args, **kwargs)
return [destpath] if destpath is not None else []
elif os.path.isdir(path):
return self.compile_folder(path, write, package, *args, **kwargs)
else:
raise CoconutException("could not find source path", path)