Beispiel #1
0
def set_docstring_for_import_func(
    *path: str, package: T.Optional[str] = None, section: str = "Returns"
) -> str:
    """Set docstring for IPython import function, from import file's docstring.

    Takes a helper function for a module and adds the content of the modules'
    `section`. This currently only works on numpy-doc style docstrings.

    Parameters
    ----------
    *path: str
        path of import module
    package : str, optional, keyword only
        package for :func:`~utilipy.data_utils.get_path_to_file`
    section: str, optiona, keyword only
        numpy-doc style section name

    Notes
    -----
    This function might be moved

    """
    module = get_path_to_file(*path, package=package)

    # read docstring out of file
    with open(module, "r") as fd:
        module_doc = ast.get_docstring(ast.parse(fd.read()))

    # process docstring
    len_title = 2 * len(section) + 1  # section & underline
    ind_section = module_doc.find(section)
    if ind_section == -1:
        raise IndexError(f"Section {section} does not exist.")

    ind = ind_section + len_title
    end_ind = ind + module_doc[ind:].find("---")  # noqa

    sub_doc = module_doc[ind:end_ind]  # get section (+ next header)

    # drop next header
    sec = "\n".join(sub_doc.split("\n")[:-2])

    # modify function with a basic decorator
    def decorator(func):
        @functools.wraps(func, docstring=(func.__doc__ or "") + "\n" + sec)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        return wrapper

    # /def

    return decorator
Beispiel #2
0
def import_astronat(
    verbose_imports: T.Optional[bool] = None,
    logger: LogFile = _LOGFILE,
    verbose: T.Optional[int] = None,
    logger_kw: T.Optional[T.Dict[str, T.Any]] = _LOGGER_KW,
) -> None:
    """Import basic amuse packages."""
    import_from_file(
        get_path_to_file("astronat_imports.py", package="utilipy.imports"),
        is_relative=False,
        verbose_imports=verbose_imports,
        logger=logger,
        verbose=verbose,
        logger_kw=logger_kw,
    )
    return