# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_load.ipynb.

# %% auto 0
__all__ = ['VArgs', 'getall', 'getalls', 'tovargs', 'dotstr', 'pkgstr', 'modstr', 'dostar', 'fromstar', 'fromstars', 'frommods']

# %% ../nbs/00_load.ipynb 6
from itertools import chain
from importlib import import_module

# %% ../nbs/00_load.ipynb 8
from types import ModuleType
from typing import Any, Union, TypeAlias, ParamSpec
VArgs: TypeAlias = Union[Any, tuple]

# %% ../nbs/00_load.ipynb 10
#| export


# %% ../nbs/00_load.ipynb 12
from nchr import DOT, DASH, U1, U2
from nlit import __ALL__, __MODULE__
from chck import isstr, istuple

# %% ../nbs/00_load.ipynb 14
#| export

# %% ../nbs/00_load.ipynb 16
def getall(mod: ModuleType) -> list[str]:
    if hasattr(mod, __ALL__): return getattr(mod, __ALL__)
    if hasattr(mod, __MODULE__): return getall(import_module(getattr(mod, __MODULE__)))
    raise AttributeError(f'''module {mod} has no attribute '__all__' or '__module__'.''')

def getalls(*mods: ModuleType) -> list[str]:
    return sorted(set(chain(*[getall(mod) for mod in mods])))

# %% ../nbs/00_load.ipynb 17
def tovargs(*args: VArgs) -> tuple:
    if len(args) == 1 and istuple(args[0]): return tovargs(*args[0])
    return args if istuple(args) else (args, )

def dotstr(*strs) -> str: return DOT.join(strs)

def pkgstr(*strs) -> str: 
    return dotstr(*tovargs(*strs))

def modstr(lib:str, *pkgs: str): 
    return pkgstr(lib, *tovargs(*pkgs))

def dostar(v: str) -> bool:
    return (not v.startswith('_') or v.isupper())

# %% ../nbs/00_load.ipynb 20
def fromstar(lib: str, *pkgs: str):
    mod = import_module(modstr(lib, *pkgs))

    global globals
    globals().update({var: getattr(mod, var) for var in dir(mod) if dostar(var)})

def fromstars(lib: str, *subs: str | tuple[str]):
    for pkgs in subs: fromstar(lib, pkgstr(pkgs))

def frommods(lib, *subs: str):
    global globals
    res = ()
    for pkgs in subs:
        pkg = pkgstr(pkgs)
        mod = import_module(modstr(lib, pkg))
        globals().update({pkg: mod})
        res += (mod,)
    return tuple(res)
