Test Setup Failed
Push — main ( a5437a...a621ab )
by torrua
12:11
created

generate_documentation.ignore_files()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
Generate HTML docs
4
"""
5
6
import os
7
import shutil
8
9
import pdoc
10
11
from loglan_db import run_with_context
12
13
DEFAULT_OUTPUT_DIRECTORY = "docs"
14
DEFAULT_PACKAGE_DIRECTORY = "loglan_db"
15
16
17
def ignore_files(directory, files):
18
    return [file for file in files if os.path.isfile(os.path.join(directory, file))]
19
20
21
def create_structure(
22
        src: str = DEFAULT_PACKAGE_DIRECTORY,
23
        dst: str = DEFAULT_OUTPUT_DIRECTORY):
24
    shutil.rmtree(dst, ignore_errors=True)
25
    shutil.copytree(src, dst, ignore=ignore_files)
26
27
28
def recursive_html(mod):
29
    yield mod.name, mod.html(show_source_code=False)
30
    for sub_mod in mod.submodules():
31
        yield from recursive_html(sub_mod)
32
33
34
def generate_html(module_name, html, dst: str = DEFAULT_OUTPUT_DIRECTORY):
35
    line = "\\"
36
    path = os.path.join(dst, line.join(str(module_name).split('.')[1:]))
37
    if os.path.exists(path) and os.path.isdir(path):
38
        path = os.path.join(path, "index")
39
    with open(f"{path}.html", "w+", encoding='utf-8') as file:
40
        file.write(html)
41
42
43
def get_package_modules(src: str = DEFAULT_PACKAGE_DIRECTORY) -> list:
44
    # Public submodules are auto-imported
45
    context = pdoc.Context()
46
    modules = [pdoc.Module(mod, context=context)
47
               for mod in [src, ]]
48
    pdoc.link_inheritance(context)
49
    return modules
50
51
52
@run_with_context
53
def run(
54
        src: str = DEFAULT_PACKAGE_DIRECTORY,
55
        dst: str = DEFAULT_OUTPUT_DIRECTORY):
56
57
    modules = get_package_modules(src)
58
    create_structure(src, dst)
59
60
    for mod in modules:
61
        for module_name, html in recursive_html(mod):
62
            generate_html(module_name, html, dst)
63
64
65
if __name__ == "__main__":
66
    run()
67