summaryrefslogtreecommitdiff
path: root/src/pyssg/discovery.py
blob: 39c8bb19ccf72f30c231e235d583cd7c260b3a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os


def get_file_list(extensions: list[str], exclude: list[str]=None) -> list[str]:
    cwd = os.getcwd()

    out = []
    for root, dirs, files in os.walk(cwd):
        if exclude is not None:
            dirs[:] = [d for d in dirs if d not in exclude]

        for f in files:
            if f.endswith(tuple(extensions)):
                out.append(os.path.join(root, f))

    return out


def get_dir_structure(exclude: list[str]=None) -> list[str]:
    cwd = os.getcwd()

    out = []
    for root, dirs, files in os.walk(cwd):
        if exclude is not None:
            dirs[:] = [d for d in dirs if d not in exclude]

        for d in dirs:
            if root in out:
                out.remove(root)
            out.append(os.path.join(root, d).replace(cwd, ''))

    return out


def get_all_files():
    md_files = get_file_list(['.md', '.markdown'], ['templates'])
    html_files = get_file_list(['.html'], ['templates'])
    dirs = get_dir_structure(['templates'])

    return (dirs, md_files, html_files)