def get_code(name: str) -> Code: """Returns a Code instace by module name. Args: name: Module name. """ module = get_module(name) return Code(module)
def test_module_empty_filters(): module = get_module("mkapi.core.base") m = renderer.render_module(module).split("\n") assert m[0] == "# ![mkapi](mkapi.core.base|plain|link|sourcelink)" assert m[2] == "## ![mkapi](mkapi.core.base.Base||link|sourcelink)" assert m[3] == "## ![mkapi](mkapi.core.base.Inline||link|sourcelink)" assert m[4] == "## ![mkapi](mkapi.core.base.Type||link|sourcelink)" assert m[5] == "## ![mkapi](mkapi.core.base.Item||link|sourcelink)"
def test_get_node_from_module(): _ = get_module("mkapi.core") x = get_node("mkapi.core.base.Base.__iter__") y = get_node("mkapi.core.base.Base.__iter__") assert x is not y x = get_node_from_module("mkapi.core.base.Base.__iter__") y = get_node_from_module("mkapi.core.base.Base.__iter__") assert x is y
def collect(path: str, docs_dir: str, config_dir, global_filters) -> Tuple[list, list]: _, api_path, *paths, package_path = path.split("/") abs_api_path = os.path.join(docs_dir, api_path) if os.path.exists(abs_api_path): logger.error( f"[MkApi] {abs_api_path} exists: Delete manually for safety.") sys.exit(1) os.mkdir(abs_api_path) os.mkdir(os.path.join(abs_api_path, "source")) atexit.register(lambda path=abs_api_path: rmtree(path)) root = os.path.join(config_dir, *paths) if root not in sys.path: sys.path.insert(0, root) package_path, filters = utils.split_filters(package_path) filters = utils.update_filters(global_filters, filters) nav = [] abs_api_paths = [] modules: Dict[str, str] = {} package = None def add_page(module: Module, package: Optional[str]): page_file = module.object.id + ".md" abs_path = os.path.join(abs_api_path, page_file) abs_api_paths.append(abs_path) create_page(abs_path, module, filters) page_name = module.object.id if package and "short_nav" in filters and page_name != package: page_name = page_name[len(package) + 1:] modules[page_name] = os.path.join(api_path, page_file) abs_path = os.path.join(abs_api_path, "source", page_file) create_source_page(abs_path, module, filters) module = get_module(package_path) for m in module: print(m) if m.object.kind == "package": if package and modules: nav.append({package: modules}) package = m.object.id modules = {} if m.docstring or any(s.docstring for s in m.members): add_page(m, package) else: add_page(m, package) if package and modules: nav.append({package: modules}) return nav, abs_api_paths
def test_get_module(): module = get_module("mkapi") assert module.parent is None assert module.object.markdown == "[mkapi](!mkapi)" assert "core" in module core = module["core"] assert core.parent is module assert core.object.markdown == "[mkapi](!mkapi).[core](!mkapi.core)" assert core.object.kind == "package" assert "base" in core base = core["base"] assert base.parent is core assert base.object.markdown == "[mkapi.core](!mkapi.core).[base](!mkapi.core.base)" assert base.object.kind == "module" assert len(base.node.members) == 6
def collect(path: str, docs_dir: str, config_dir) -> Tuple[list, list]: _, api_path, *paths, package_path = path.split("/") abs_api_path = os.path.join(docs_dir, api_path) if os.path.exists(abs_api_path): logger.error( f"[MkApi] {abs_api_path} exists: Delete manually for safety.") sys.exit(1) os.mkdir(abs_api_path) atexit.register(lambda path=abs_api_path: rmtree(path)) root = os.path.join(config_dir, *paths) if root not in sys.path: sys.path.insert(0, root) package_path, filters = utils.filter(package_path) module = get_module(package_path) nav = [] abs_api_paths = [] modules: Dict[str, str] = {} package = None def add_page(module: Module) -> str: page_file = module.object.id + ".md" abs_path = os.path.join(abs_api_path, page_file) abs_api_paths.append(abs_path) create_page(abs_path, module, filters) return os.path.join(api_path, page_file) for m in module: if m.object.kind == "package": if package and modules: nav.append({package: modules}) package = m.object.id modules = {} if m.docstring: modules[m.object.id] = add_page(m) else: modules[m.object.id] = add_page(m) if package and modules: nav.append({package: modules}) return nav, abs_api_paths
def test_get_module_from_object(): from mkapi.core import base assert get_module(base).obj is base
def test_repr(): module = get_module("mkapi.core.base") s = "Module('mkapi.core.base', num_sections=2, num_members=0)" assert repr(module) == s
def get_code(name: str) -> Code: module = get_module(name) return Code(module)