|
1 | 1 | import importlib
|
2 | 2 | import logging
|
| 3 | +from typing import Optional |
3 | 4 |
|
4 | 5 | from basilisp import importer as importer
|
5 | 6 | from basilisp.lang import runtime as runtime
|
| 7 | +from basilisp.lang.compiler import compiler_opts |
6 | 8 | from basilisp.lang.typing import CompilerOpts
|
| 9 | +from basilisp.lang.util import munge |
7 | 10 | from basilisp.logconfig import DEFAULT_HANDLER, DEFAULT_LEVEL
|
8 | 11 |
|
9 | 12 | logger = logging.getLogger("basilisp")
|
10 | 13 | logger.setLevel(DEFAULT_LEVEL)
|
11 | 14 | logger.addHandler(DEFAULT_HANDLER)
|
12 | 15 |
|
13 | 16 |
|
14 |
| -def init(compiler_opts: CompilerOpts) -> None: |
15 |
| - """Initialize the runtime environment for evaluation.""" |
| 17 | +def init(opts: Optional[CompilerOpts] = None) -> None: |
| 18 | + """ |
| 19 | + Initialize the runtime environment for Basilisp code evaluation. |
| 20 | +
|
| 21 | + If you want to execute a Basilisp file which is stored in a well-formed package |
| 22 | + or module structure, you probably want to use `bootstrap()` below. |
| 23 | + """ |
16 | 24 | runtime.init_ns_var()
|
17 |
| - runtime.bootstrap_core(compiler_opts) |
| 25 | + runtime.bootstrap_core(opts if opts is not None else compiler_opts()) |
18 | 26 | importer.hook_imports()
|
19 | 27 | importlib.import_module("basilisp.core")
|
| 28 | + |
| 29 | + |
| 30 | +def bootstrap( |
| 31 | + target: str, opts: Optional[CompilerOpts] = None |
| 32 | +) -> None: # pragma: no cover |
| 33 | + """ |
| 34 | + Import a Basilisp namespace or function identified by `target`. |
| 35 | +
|
| 36 | + Basilisp only needs to be bootstrapped once per Python VM invocation. Subsequent |
| 37 | + imports of Basilisp namespaces will work using Python's standard `import` statement |
| 38 | + and `importlib.import_module` function. |
| 39 | +
|
| 40 | + `target` must be a string naming a Basilisp namespace. Namespace references may |
| 41 | + be given exactly as they are found in Basilisp code. `target` may optionally |
| 42 | + include a trailing function reference, delimited by ":", which will be executed |
| 43 | + after the target namespace is imported. If a function reference is given, the |
| 44 | + function will be called with no arguments. |
| 45 | +
|
| 46 | + `opts` is a mapping of compiler options that may be supplied for bootstrapping. |
| 47 | + This setting should be left alone unless you know what you are doing. |
| 48 | + """ |
| 49 | + init(opts=opts) |
| 50 | + pkg_name, *rest = target.split(":", maxsplit=1) |
| 51 | + mod = importlib.import_module(munge(pkg_name)) |
| 52 | + if rest: |
| 53 | + fn_name = munge(rest[0]) |
| 54 | + getattr(mod, fn_name)() |
0 commit comments