"""Call sphinx-apidoc while skipping the autogenerated and code-heavy modules

The modules list is generated automatically based on file names.
"""

import os
import glob
import subprocess

src=r"..\pylablib"

exclude_folders=["gui","thread","misc","legacy"]
exclude_files=["aux_libs.py",os.path.join("core","utils","ctypes_tools.py"),os.path.join("core","devio","hid_lib.py")]

def is_autogen(path):
    name=os.path.split(path)[1]
    return name.endswith("_defs.py") or (name.endswith("_lib.py") and name!="load_lib.py")
def get_autogen_files():
    dev_files=glob.glob(os.path.join(src,"devices","**","*.py"))
    return [f for f in dev_files if is_autogen(f)]
def get_c_files():
    return glob.glob(os.path.join(src,"**","*.c"),recursive=True)

if __name__=="__main__":
    autogen_files=get_autogen_files()
    c_files=get_c_files()
    print("\n".join(["Skipping {}".format(f) for f in autogen_files+c_files]))
    skip_files=[f+"/**" for f in exclude_folders]+exclude_files
    skip_files=[os.path.join(src,f) for f in skip_files]+autogen_files+c_files
    with open(".skipped_apidoc","w") as f:
        for ln in skip_files:
            f.write(ln+"\n")

    skip_files_pattern=[(f[:-1]+"*" if f.endswith(".c") else f) for f in skip_files]
    os.environ["SPHINX_APIDOC_OPTIONS"]="members,inherited-members,undoc-members,show-inheritance"  # include inherited members into the docstring
    subprocess.call(["sphinx-apidoc","-o",".apidoc",src]+skip_files_pattern)