summary refs log tree commit diff
path: root/dot_ipython/profile_default/startup/00_internal.py
blob: 57403d8c32b856ac0e3989266d4ccb6c5416b819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Internal startup utils."""

_IPYTHON_INTERNAL_NAMES = set(locals())
_INTERNAL_NAMES = set(locals())


def _summarise_startup() -> None:
    import inspect
    import logging
    import os

    frame = inspect.stack()[1].frame
    filename = os.path.basename(frame.f_locals["__file__"])
    nice_name, _ = os.path.splitext(filename)
    nice_name = nice_name.lstrip("0123456789_")

    names = sorted(
        name
        for name in set(frame.f_locals) - _INTERNAL_NAMES
        if not name.startswith("_")
    )

    if names:
        for name in names:
            _INTERNAL_NAMES.add(name)
        logging.getLogger("startup").setLevel(logging.INFO)
        logger = logging.getLogger(f"startup.{nice_name}")
        log_level = getattr(logging, os.getenv("STARTUP_LOG_LEVEL", "DEBUG"))
        logger.log(log_level, "%s", ", ".join(names))


_summarise_startup()