2019-07-28 15:27:23 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-06-17 10:17:53 +00:00
|
|
|
import os
|
2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Iterator
|
2019-07-28 15:27:23 +00:00
|
|
|
|
2019-06-17 10:17:53 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def good_file_paths(top_dir: str = ".") -> Iterator[str]:
|
|
|
|
for dir_path, dir_names, filenames in os.walk(top_dir):
|
|
|
|
dir_names[:] = [d for d in dir_names if d != "scripts" and d[0] not in "._"]
|
2019-07-28 15:27:23 +00:00
|
|
|
for filename in filenames:
|
|
|
|
if filename == "__init__.py":
|
|
|
|
continue
|
|
|
|
if os.path.splitext(filename)[1] in (".py", ".ipynb"):
|
2020-03-04 12:40:28 +00:00
|
|
|
yield os.path.join(dir_path, filename).lstrip("./")
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-06-17 10:17:53 +00:00
|
|
|
|
2019-07-28 15:27:23 +00:00
|
|
|
def md_prefix(i):
|
2019-10-25 17:33:24 +00:00
|
|
|
return f"{i * ' '}*" if i else "\n##"
|
2019-06-17 10:17:53 +00:00
|
|
|
|
|
|
|
|
2019-07-28 15:27:23 +00:00
|
|
|
def print_path(old_path: str, new_path: str) -> str:
|
|
|
|
old_parts = old_path.split(os.sep)
|
|
|
|
for i, new_part in enumerate(new_path.split(os.sep)):
|
2023-03-01 16:23:33 +00:00
|
|
|
if (i + 1 > len(old_parts) or old_parts[i] != new_part) and new_part:
|
|
|
|
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}")
|
2019-07-28 15:27:23 +00:00
|
|
|
return new_path
|
2019-06-17 10:17:53 +00:00
|
|
|
|
|
|
|
|
2019-07-28 15:27:23 +00:00
|
|
|
def print_directory_md(top_dir: str = ".") -> None:
|
|
|
|
old_path = ""
|
2020-06-02 19:14:12 +00:00
|
|
|
for filepath in sorted(good_file_paths(top_dir)):
|
2019-07-28 15:27:23 +00:00
|
|
|
filepath, filename = os.path.split(filepath)
|
|
|
|
if filepath != old_path:
|
|
|
|
old_path = print_path(old_path, filepath)
|
|
|
|
indent = (filepath.count(os.sep) + 1) if filepath else 0
|
2022-06-22 04:01:05 +00:00
|
|
|
url = "/".join((filepath, filename)).replace(" ", "%20")
|
2019-10-25 17:33:24 +00:00
|
|
|
filename = os.path.splitext(filename.replace("_", " ").title())[0]
|
2019-07-28 15:27:23 +00:00
|
|
|
print(f"{md_prefix(indent)} [{filename}]({url})")
|
2019-06-17 10:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-07-28 15:27:23 +00:00
|
|
|
print_directory_md(".")
|