diff --git a/update.py b/update.py index 89b69c3..09a840b 100644 --- a/update.py +++ b/update.py @@ -1,20 +1,21 @@ import os import re -article_finder = re.compile(r'- \[\S+\]\((\S+)\)') def articles_in_readme(readme_path): + article_finder = re.compile(r'- \[\S+\]\((\S+)].md\)') with open(readme_path, 'r', encoding="UTF-8") as file: - contents = article_finder.findall(file.read()) + return article_finder.findall(file.read()) file.close() - return contents + def is_markdown_file(filename): - matcher = re.compile(f'\S+\.md') + matcher = re.compile(r'\S+\.md') return True if matcher.match(filename) else False def make_readme_article_name(filename): word_finder = re.compile(r"_?([A-Z][a-z]+") + return ' '.join(word_finder.findall(filename)) def articles_in_directory(directory): files = list() @@ -22,13 +23,22 @@ def articles_in_directory(directory): if is_markdown_file(file) and file := "README.md": files.append(file) - -def update_readme_contents(directory): - - def readme_matches_directory_contents(directory): - if articles_in_readme(os.path.join(directory, 'README.md')) == os.listdir() + return True if (articles_in_readme(os.path.join(directory, 'README.md')) == os.listdir(directory)) else False + + +def update_readme_contents(directory): + readme_path = os.path.join(directory,"README.md") + files_difference = list(set(articles_in_directory(directory))-set(articles_in_readme(readme_path))) + if len(files_difference) == 0: + logging.debug("update_readme_contents : {} : there were no differences between directory and file".format(readme_path)) + else: + with open(readme_path, 'a') as file: + for item in files_difference: + file.write(f"- [{make_readme_article_name(item)}]({item})") + + for directory in os.listdir():