-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Rewrite build_directory_md.py #1076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 2 commits into
TheAlgorithms:master
from
cclauss:rewrite-build_directory_md.py
Jul 28, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,45 @@ | ||
""" | ||
This is a simple script that will scan through the current directory | ||
and generate the corresponding DIRECTORY.md file, can also specify | ||
files or folders to be ignored. | ||
""" | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
from typing import Iterator | ||
|
||
URL_BASE = "https://github.com/TheAlgorithms/Python/blob/master" | ||
|
||
|
||
def good_filepaths(top_dir: str = ".") -> Iterator[str]: | ||
for dirpath, dirnames, filenames in os.walk(top_dir): | ||
dirnames[:] = [d for d in dirnames if d != "scripts" and d[0] not in "._"] | ||
for filename in filenames: | ||
if filename == "__init__.py": | ||
continue | ||
if os.path.splitext(filename)[1] in (".py", ".ipynb"): | ||
yield os.path.join(dirpath, filename).lstrip("./") | ||
|
||
# Target URL (master) | ||
URL = "https://github.com/TheAlgorithms/Python/blob/master/" | ||
|
||
def md_prefix(i): | ||
return f"{i * ' '}*" if i else "##" | ||
|
||
def tree(d, ignores, ignores_ext): | ||
return _markdown(d, ignores, ignores_ext, 0) | ||
|
||
|
||
def _markdown(parent, ignores, ignores_ext, depth): | ||
out = "" | ||
dirs, files = [], [] | ||
for i in os.listdir(parent): | ||
full = os.path.join(parent, i) | ||
name, ext = os.path.splitext(i) | ||
if i not in ignores and ext not in ignores_ext: | ||
if os.path.isfile(full): | ||
# generate list | ||
pre = parent.replace("./", "").replace(" ", "%20") | ||
# replace all spaces to safe URL | ||
child = i.replace(" ", "%20") | ||
files.append((pre, child, name)) | ||
else: | ||
dirs.append(i) | ||
# Sort files | ||
files.sort(key=lambda e: e[2].lower()) | ||
for f in files: | ||
pre, child, name = f | ||
out += " " * depth + "* [" + name.replace("_", " ") + "](" + URL + pre + "/" + child + ")\n" | ||
# Sort directories | ||
dirs.sort() | ||
for i in dirs: | ||
full = os.path.join(parent, i) | ||
i = i.replace("_", " ").title() | ||
if depth == 0: | ||
out += "## " + i + "\n" | ||
else: | ||
out += " " * depth + "* " + i + "\n" | ||
out += _markdown(full, ignores, ignores_ext, depth+1) | ||
return out | ||
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)): | ||
if i + 1 > len(old_parts) or old_parts[i] != new_part: | ||
if new_part: | ||
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") | ||
return new_path | ||
|
||
|
||
# Specific files or folders with the given names will be ignored | ||
ignores = [".vs", | ||
".gitignore", | ||
".git", | ||
"scripts", | ||
"__init__.py", | ||
"requirements.txt", | ||
".github" | ||
] | ||
# Files with given entensions will be ignored | ||
ignores_ext = [ | ||
".md", | ||
".ipynb", | ||
".png", | ||
".jpg", | ||
".yml" | ||
] | ||
def print_directory_md(top_dir: str = ".") -> None: | ||
old_path = "" | ||
for filepath in sorted(good_filepaths()): | ||
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 | ||
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") | ||
filename = os.path.splitext(filename.replace("_", " "))[0] | ||
print(f"{md_prefix(indent)} [{filename}]({url})") | ||
|
||
|
||
if __name__ == "__main__": | ||
with open("DIRECTORY.md", "w+") as f: | ||
f.write(tree(".", ignores, ignores_ext)) | ||
print_directory_md(".") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this line mean travis will write to Directory on successful builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but... It is only writing in its local directory. This does not commit those changes back into the repo. I could investigate how to do the latter on branch==master if that would be of interest.