Skip to content

Commit ac6626b

Browse files
authored
Improve the Basilisp bootstrapping experience (#620)
* Improve the Basilisp bootstrapping experience * Changelog
1 parent ca78e9f commit ac6626b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
* Added a bootstrapping function for easily bootstrapping Basilisp projects from Python (#620)
810

911
## [v0.1.dev15]
1012
### Added

src/basilisp/main.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
11
import importlib
22
import logging
3+
from typing import Optional
34

45
from basilisp import importer as importer
56
from basilisp.lang import runtime as runtime
7+
from basilisp.lang.compiler import compiler_opts
68
from basilisp.lang.typing import CompilerOpts
9+
from basilisp.lang.util import munge
710
from basilisp.logconfig import DEFAULT_HANDLER, DEFAULT_LEVEL
811

912
logger = logging.getLogger("basilisp")
1013
logger.setLevel(DEFAULT_LEVEL)
1114
logger.addHandler(DEFAULT_HANDLER)
1215

1316

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+
"""
1624
runtime.init_ns_var()
17-
runtime.bootstrap_core(compiler_opts)
25+
runtime.bootstrap_core(opts if opts is not None else compiler_opts())
1826
importer.hook_imports()
1927
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

Comments
 (0)