From 7bc71ee6094a9a8782237c90cca2dd05ab3f0fec Mon Sep 17 00:00:00 2001 From: Emil Merle Date: Tue, 23 Jan 2024 00:25:06 +0100 Subject: [PATCH] Added a directory to find tasks in markdown files --- alliterations/alliterationInClipboard.py | 6 ++-- alliterations/alliterationInFile.py | 13 ++++---- emailAdresses/email.py | 7 +++-- emailAdresses/emailInClipboard.py | 6 ++-- emailAdresses/emailInFile.py | 13 ++++---- markdownTasks/__init__.py | 0 markdownTasks/taskInFile.py | 39 ++++++++++++++++++++++++ markdownTasks/test.md | 7 +++++ phoneNumbers/countries.py | 10 +++--- phoneNumbers/numberInFile.py | 20 ++++++------ urls/url.py | 5 +-- urls/urlInClipboard.py | 6 ++-- urls/urlInFile.py | 13 ++++---- 13 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 markdownTasks/__init__.py create mode 100644 markdownTasks/taskInFile.py create mode 100644 markdownTasks/test.md diff --git a/alliterations/alliterationInClipboard.py b/alliterations/alliterationInClipboard.py index 88266fb..74f1499 100644 --- a/alliterations/alliterationInClipboard.py +++ b/alliterations/alliterationInClipboard.py @@ -16,11 +16,11 @@ def main(): matches.append(alliterations) if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Alliteration(s) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No alliteration found.') + print("No alliteration found.") if __name__ == "__main__": diff --git a/alliterations/alliterationInFile.py b/alliterations/alliterationInFile.py index 8db5b7f..046034f 100644 --- a/alliterations/alliterationInFile.py +++ b/alliterations/alliterationInFile.py @@ -15,17 +15,16 @@ def main(): try: argumentOne = sys.argv[1] except IndexError: - exit("No second argument given. " - "Try again with a file path as an argument") + exit("No second argument given. " "Try again with a file path as an argument") # Make absolute path if given path is a relative path - if(not os.path.isabs(argumentOne)): + if not os.path.isabs(argumentOne): argumentOne = os.path.abspath(argumentOne) openedContent = "" # Check if argumentOne is a valid path and file - if(os.path.exists(argumentOne) and os.path.isfile(argumentOne)): + if os.path.exists(argumentOne) and os.path.isfile(argumentOne): # Try to open and read file try: openedFile = open(argumentOne, "r") @@ -43,11 +42,11 @@ def main(): openedFile.close() if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Alliteration(s) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No alliteration found.') + print("No alliteration found.") if __name__ == "__main__": diff --git a/emailAdresses/email.py b/emailAdresses/email.py index 7910ef1..7ed3c0d 100644 --- a/emailAdresses/email.py +++ b/emailAdresses/email.py @@ -1,9 +1,12 @@ import re -regexp = re.compile(r'''( +regexp = re.compile( + r"""( [a-zA-Z0-9._%+-]+ # username @ # @ symbol (\w)+ # domain name (\.[a-zA-Z]{2,4}) # dot-something - )''', re.VERBOSE) + )""", + re.VERBOSE, +) diff --git a/emailAdresses/emailInClipboard.py b/emailAdresses/emailInClipboard.py index 88d9737..2695403 100644 --- a/emailAdresses/emailInClipboard.py +++ b/emailAdresses/emailInClipboard.py @@ -16,11 +16,11 @@ def main(): matches.append(emails) if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Email address(es) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No email address found.') + print("No email address found.") if __name__ == "__main__": diff --git a/emailAdresses/emailInFile.py b/emailAdresses/emailInFile.py index 6eebd25..b4f3f1e 100644 --- a/emailAdresses/emailInFile.py +++ b/emailAdresses/emailInFile.py @@ -15,17 +15,16 @@ def main(): try: argumentOne = sys.argv[1] except IndexError: - exit("No second argument given. " - "Try again with a file path as an argument") + exit("No second argument given. " "Try again with a file path as an argument") # Make absolute path if given path is a relative path - if(not os.path.isabs(argumentOne)): + if not os.path.isabs(argumentOne): argumentOne = os.path.abspath(argumentOne) openedContent = "" # Check if argumentOne is a valid path and file - if(os.path.exists(argumentOne) and os.path.isfile(argumentOne)): + if os.path.exists(argumentOne) and os.path.isfile(argumentOne): # Try to open and read file try: openedFile = open(argumentOne, "r") @@ -43,11 +42,11 @@ def main(): openedFile.close() if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Email address(es) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No email address found.') + print("No email address found.") if __name__ == "__main__": diff --git a/markdownTasks/__init__.py b/markdownTasks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/markdownTasks/taskInFile.py b/markdownTasks/taskInFile.py new file mode 100644 index 0000000..3792cc0 --- /dev/null +++ b/markdownTasks/taskInFile.py @@ -0,0 +1,39 @@ +#! python3 +# taskInFile.py - Finds tasks in a given markdown file, prints them to the terminal and copys them to the clipboards +# Usage: call the program with a file path as the first argument +# Example: >>> taskInFile.py C:\\Users\\ExampleUser\\Documents\\myTasks.md + + +# import pyperclip +import sys +import os + + +def main(): + # Check if sys.argv[1] is given + argumentOne = "" + try: + argumentOne = sys.argv[1] + except IndexError: + exit("No argument given. Try again with a file path as an argument") + + # Make absolute path if given path is a relative path + if not os.path.isabs(argumentOne): + argumentOne = os.path.abspath(argumentOne) + + files = os.listdir(argumentOne) + + for file in files: + if file.endswith(".md"): + handleFile(argumentOne + "\\" + file) + + +def handleFile(markdownFile: str): + with open(markdownFile, "r") as f: + for line in f: + if "!!!" in line: + print(f"Task in line found: {line[:-1]}") + + +if __name__ == "__main__": + main() diff --git a/markdownTasks/test.md b/markdownTasks/test.md new file mode 100644 index 0000000..9f0a1a8 --- /dev/null +++ b/markdownTasks/test.md @@ -0,0 +1,7 @@ +# Header 1 +## Header 2 +- test +# Header 3 +- task !!! +- Unteraufgabe + - Task Unter!!! \ No newline at end of file diff --git a/phoneNumbers/countries.py b/phoneNumbers/countries.py index 716fae8..2c1b95f 100644 --- a/phoneNumbers/countries.py +++ b/phoneNumbers/countries.py @@ -4,7 +4,7 @@ """ countryDict = { - 'canada': r''' + "canada": r""" ((\+?1)?) # country code (\s|-|\.)? # separator (\([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]\))? # area code @@ -13,12 +13,12 @@ ([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2}) # first three (\s|-|\.)? # separator (\d{4}) # last four (required) - ''', - 'germany': r'''( + """, + "germany": r"""( (\d{3}|\(\d{3}\))? # area code (optional) (\s|-|\.|/)? # separator (optional) (\d{7,9}) # 7 to 9 digits - )''' + )""", } -countryDict['usa'] = countryDict.get('canada') +countryDict["usa"] = countryDict.get("canada") diff --git a/phoneNumbers/numberInFile.py b/phoneNumbers/numberInFile.py index 0c9a903..316d563 100644 --- a/phoneNumbers/numberInFile.py +++ b/phoneNumbers/numberInFile.py @@ -23,10 +23,9 @@ def main(): try: argumentOne = sys.argv[1] except IndexError: - exit("No argument given." - " Try again with a country as an argument") + exit("No argument given." " Try again with a country as an argument") - if(argumentOne in countryDict): + if argumentOne in countryDict: # Create regex object regexCompile = re.compile(countryDict[argumentOne], re.VERBOSE) else: @@ -37,17 +36,16 @@ def main(): try: argumentTwo = sys.argv[2] except IndexError: - exit("No second argument given." - " Try again with a file path as an argument") + exit("No second argument given." " Try again with a file path as an argument") # Make absolute path if given path is a relative path - if(not os.path.isabs(argumentTwo)): + if not os.path.isabs(argumentTwo): argumentTwo = os.path.abspath(argumentTwo) openedContent = "" # Check if argumentTwo is a valid path and file - if(os.path.exists(argumentTwo) and os.path.isfile(argumentTwo)): + if os.path.exists(argumentTwo) and os.path.isfile(argumentTwo): # Try to open and read file try: openedFile = open(argumentTwo, "r") @@ -60,17 +58,17 @@ def main(): matches = [] # Find all phone numbers and store them in matches for groups in regexCompile.findall(openedContent): - phoneNumber = ''.join(groups) + phoneNumber = "".join(groups) matches.append(phoneNumber) openedFile.close() if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Phone number(s) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No phone numbers or email addresses found.') + print("No phone numbers or email addresses found.") if __name__ == "__main__": diff --git a/urls/url.py b/urls/url.py index e703d5a..82f5f6b 100644 --- a/urls/url.py +++ b/urls/url.py @@ -1,3 +1,4 @@ import re - # protocol (http,https,ftp), www, url with dot something -regexp = re.compile(r'''(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+''') \ No newline at end of file + +# protocol (http,https,ftp), www, url with dot something +regexp = re.compile(r"""(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+""") diff --git a/urls/urlInClipboard.py b/urls/urlInClipboard.py index 91a26d5..a967f8c 100644 --- a/urls/urlInClipboard.py +++ b/urls/urlInClipboard.py @@ -15,11 +15,11 @@ def main(): matches.append(url_match) if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Url(s) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No Url found.') + print("No Url found.") if __name__ == "__main__": diff --git a/urls/urlInFile.py b/urls/urlInFile.py index 2043b8e..3cfaf35 100644 --- a/urls/urlInFile.py +++ b/urls/urlInFile.py @@ -15,17 +15,16 @@ def main(): try: argumentOne = sys.argv[1] except IndexError: - exit("No second argument given. " - "Try again with a file path as an argument") + exit("No second argument given. " "Try again with a file path as an argument") # Make absolute path if given path is a relative path - if(not os.path.isabs(argumentOne)): + if not os.path.isabs(argumentOne): argumentOne = os.path.abspath(argumentOne) openedContent = "" # Check if argumentOne is a valid path and file - if(os.path.exists(argumentOne) and os.path.isfile(argumentOne)): + if os.path.exists(argumentOne) and os.path.isfile(argumentOne): # Try to open and read file try: openedFile = open(argumentOne, "r") @@ -42,11 +41,11 @@ def main(): openedFile.close() if len(matches) > 0: - pyperclip.copy(' -+- '.join(matches)) + pyperclip.copy(" -+- ".join(matches)) print("Url(s) found:") - print('\n'.join(matches)) + print("\n".join(matches)) else: - print('No Url found.') + print("No Url found.") if __name__ == "__main__":