Skip to content

Commit bb9ab4f

Browse files
Add type hints for post search , Modified search_posts in cli handler
1 parent 5b6c552 commit bb9ab4f

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/cli/cli_handler.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,25 @@ def suggest_users(input_file, user_id):
354354
print(f"{Fore.RED}Error suggesting users: {e}")
355355

356356
def search_posts(input_file, word=None, topic=None):
357-
print(f"Searching posts in {input_file}")
357+
if os.path.splitext(input_file)[1] != ".xml" and os.path.splitext(input_file)[1]:
358+
print(f"{Fore.RED}Error: Invalid input file. Please provide a valid XML file.")
359+
return
360+
print(f"{Style.BRIGHT}{Fore.CYAN}Searching posts in {input_file}{Style.RESET_ALL}")
361+
if not os.path.splitext(input_file)[1]:
362+
input_file = f"{input_file}.xml" # Append .xml if no extension is present
363+
print(f"{Fore.LIGHTYELLOW_EX}You forgot to add the extension to the input file :) \nAppending '.xml' to the input file name.")
358364
try:
359365
searcher = PostSearch(input_file)
360366
if word:
361-
results = searcher.search_by_word(word)
362-
print(f"Posts containing the word '{word}': {results}")
367+
results = searcher.search_word(word)
368+
print(f"{Fore.GREEN}Posts containing the word '{word}':")
369+
print(*results, sep="\n")
363370
elif topic:
364-
results = searcher.search_by_topic(topic)
365-
print(f"Posts related to the topic '{topic}': {results}")
371+
results = searcher.search_topic(topic)
372+
print(f"{Fore.GREEN}Posts related to the topic '{topic}':")
373+
print(*results, sep="\n")
366374
except Exception as e:
367-
print(f"Error searching posts: {e}")
375+
print(f"{Fore.RED}Error searching posts: {e}")
368376

369377

370378
def main():

src/postsearch/post_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ def __init__(self, xml_file):
55
Initialize the PostSearch object by loading the XML file content as a string.
66
"""
77
with open(xml_file, 'r', encoding='utf-8') as file:
8-
self.xml_content = file.read()
8+
self.xml_content:str = file.read()
99

10-
def search_word(self, word):
10+
def search_word(self, word:str) -> list[str]:
1111

1212
posts = []
1313
start = 0
@@ -28,7 +28,7 @@ def search_word(self, word):
2828
start = end + 7 # 7 is the length of "</post>"
2929
return posts
3030

31-
def search_topic(self, topic):
31+
def search_topic(self, topic:str) -> list[str]:
3232
posts = []
3333
start = 0
3434
while True:

0 commit comments

Comments
 (0)