journal/update.py

56 lines
1.8 KiB
Python
Raw Permalink Normal View History

2021-09-04 21:13:56 +00:00
import os
import re
def articles_in_readme(readme_path):
2021-09-05 04:11:17 +00:00
article_finder = re.compile(r'- \[\S+\]\((\S+)].md\)')
2021-09-04 21:13:56 +00:00
with open(readme_path, 'r', encoding="UTF-8") as file:
2021-12-10 17:40:46 +00:00
content = file.read()
2021-09-04 21:13:56 +00:00
file.close()
2021-12-10 17:40:46 +00:00
return article_finder.findall(content)
2021-09-05 04:11:17 +00:00
2021-09-04 21:13:56 +00:00
def is_markdown_file(filename):
2021-09-05 04:11:17 +00:00
matcher = re.compile(r'\S+\.md')
2021-09-04 21:13:56 +00:00
return True if matcher.match(filename) else False
def make_readme_article_name(filename):
word_finder = re.compile(r"_?([A-Z][a-z]+")
2021-09-05 04:11:17 +00:00
return ' '.join(word_finder.findall(filename))
2021-09-04 21:13:56 +00:00
def articles_in_directory(directory):
files = list()
for file in os.listdir(directory):
2021-09-05 04:25:50 +00:00
if is_markdown_file(file) and file != "README.md":
2021-09-04 21:13:56 +00:00
files.append(file)
2021-12-10 17:40:46 +00:00
return files
2021-09-04 21:13:56 +00:00
2021-09-05 04:25:50 +00:00
def get_section_dirs():
dirs = list()
for dir in os.listdir():
if dir not in [".git"] and os.path.isdir(dir):
dirs.append(dir)
return dirs
2021-09-04 21:13:56 +00:00
2021-09-05 04:11:17 +00:00
def readme_matches_directory_contents(directory):
2021-12-10 17:40:46 +00:00
return True if (articles_in_readme(os.path.join(directory, 'README.md')) == articles_in_directory(directory)) else False
2021-09-05 04:11:17 +00:00
2021-12-10 17:40:46 +00:00
#This updates the Article section of the each README.md
2021-09-05 04:11:17 +00:00
def update_readme_contents(directory):
readme_path = os.path.join(directory,"README.md")
2021-09-05 04:25:50 +00:00
if readme_matches_directory_contents(directory):
2021-09-05 04:11:17 +00:00
logging.debug("update_readme_contents : {} : there were no differences between directory and file".format(readme_path))
else:
with open(readme_path, 'a') as file:
2021-09-05 04:25:50 +00:00
files_difference = list(set(articles_in_directory(directory))-set(articles_in_readme(readme_path)))
2021-09-05 04:11:17 +00:00
for item in files_difference:
file.write(f"- [{make_readme_article_name(item)}]({item})")
2021-09-05 04:25:50 +00:00
file.close()
2021-09-04 21:13:56 +00:00
2021-09-05 04:25:50 +00:00
if __name__ == "__main__":
for directory in get_section_dirs():
update_readme_contents(directory)
2021-09-04 21:13:56 +00:00