From 6b913d5774d82172c64a2864b61adc8f8a39116f Mon Sep 17 00:00:00 2001 From: Lukáš Růžička Date: Jun 23 2022 10:57:50 +0000 Subject: Add new methods of printing the current info. --- diff --git a/needle_mapper.py b/needle_mapper.py index e292e02..d521612 100755 --- a/needle_mapper.py +++ b/needle_mapper.py @@ -146,13 +146,13 @@ class Solver: similar = {} taglist = [] for path in self.reader.paths: - if filetodisplay in path: + if filename in path: content = self.reader.read_json(path) try: tags = content['tags'] except TypeError: tags == [] - if filetodisplay == os.path.split(path)[1]: + if filename == os.path.split(path)[1]: exact[path] = tags else: similar[path] = tags @@ -169,7 +169,6 @@ class Solver: found_tags = {} needle_commands = ['assert_and_click', 'assert_screen', 'check_screen', 'match_has_tag', 'assert_and_dclick'] - count = 0 script = self.reader.read_script(filename) for line in script: if 'assert_and_click' in line or 'assert_screen' in line or 'check_screen' in line or 'start_with_launcher' in line: @@ -181,11 +180,73 @@ class Solver: if tag and tag not in found_tags.keys(): paths = self.solve_tag(tag)[0] found_tags[tag] = paths - count += 1 else: pass return found_tags + +class Printer: + """ This is a printer object that forms the output into something + quite readable. """ + def __init__(self): + pass + + def print_tag(self, data, total): + print("Available tags matching the search criteria: ".ljust(80, '*')) + print(f"(out of {total} files) ".ljust(80, ' ')) + exact_matches = data[0] + similar_matches = data[1] + stored_tags = data[2] + print("Exact matches ".ljust(80, '-')) + for path in exact_matches: + first, last = os.path.split(path) + fname = colored(last, 'green') + print(f"{first}/{fname}".ljust(80, '.')) + print("Similar matches ".ljust(80, '-')) + for path in similar_matches: + first, last = os.path.split(path) + fname = colored(last, 'yellow') + print(f"{first}/{fname}".ljust(80, '.')) + print(f" {colored(stored_tags[path], 'blue')}".rjust(80, '.')) + print("End of list ".ljust(80, '-')) + + def print_list(self, data, total): + stored_tags = data[2] + print(stored_tags) + print("All available tags in the search location: ".ljust(80, '*')) + taglist = sorted(stored_tags.items(), key=lambda x: x[1], reverse=True) + for line in taglist: + print(f"{line[0]} ({line[1]})") + print("End of list ".ljust(80, '-')) + + def print_filetags(self, data, filename): + print("All available tags in {filename}): ".ljust(80, '*')) + exact_tags = data[0] + similar_tags = data[1] + for key in exact_tags.keys(): + first, last = os.path.split(key) + print(f"{first}/{colored(last, 'green')}".ljust(80, '*')) + tags = exact_tags[key] + for tag in tags: + print(f" {colored(tag, 'blue')}".rjust(80, '.')) + for key in similar_tags.keys(): + first, last = os.path.split(key) + print(f"{first}/{colored(last, 'yellow')}".ljust(80, '.')) + tags = similar_tags[key] + for tag in tags: + print(f" {colored(tag, 'blue')}".rjust(80, '.')) + print("End of list ".ljust(80, '-')) + + def print_scriptags(self, data, filename, where): + print(f"Showing all tags from {filename} and their sources: ".ljust(80, '*')) + print(f"Looking in existing needles located in {where}".ljust(80, '-')) + for tag in data.keys(): + print(f"{colored(tag, 'red')} ".ljust(80, '.')) + for path in data[tag]: + first, last = os.path.split(path) + print(f"{first}/{colored(last, 'blue')}".rjust(80, '.')) + + def main(): """ Main program """ print('This is the needle mapper for OpenQA') @@ -196,66 +257,27 @@ def main(): script_command = args.command solver = Solver(args.root) + printer = Printer() if script_command == 'tag': tag = args.name - print(f"I am searching for '{tag}' in {solver.count} files.") - print('==================================================') locations = solver.solve_tag(tag) - exact = locations[0] - similar = locations[1] - stored = locations[2] if tag != 'NA': - print("The following files have an exact tag:") - for path in exact: - first, last = os.path.split(path) - fname = colored(last, 'green') - print(f"{first}/{fname}") - print('--------------------------------------------------') - print("The following files have a similar tag:") - for path in similar: - first, last = os.path.split(path) - fname = colored(last, 'yellow') - print(f"{first}/{fname} => ({colored(stored[path], 'blue')})") + printer.print_tag(locations, solver.count) else: - print("Here is the list of all used tags and their occurence.") - for key in stored.keys(): - print(f"{key}: {stored[key]}") + printer.print_list(locations, solver.count) elif script_command == 'file': filename = args.name print(f"I am displaying the tags from required file or files.") print('==================================================') locations = solver.solve_file(filename) - exact = locations[0] - similar = locations[1] - taglist = locations[2] - for key in exact.keys(): - first, last = os.path.split(key) - print(f"{first}/{colored(last, 'green')}") - tags = exact[key] - for tag in tags: - print(colored(tag, 'blue')) - print('--------------------------------------------------') - for key in similar.keys(): - first, last = os.path.split(key) - print(f"{first}/{colored(last, 'yellow')}") - tags = similar[key] - for tag in tags: - print(colored(tag, 'blue')) - print('--------------------------------------------------') + printer.print_filetags(locations, filename) elif script_command == 'inspect': filename = args.file - print(f"Inspecting file: {filename}") - print(f"Looking for already created needles in: {solver.where}") usedtags = solver.solve_file_inspection(filename) - print("-------------------------------------------------------") - for tag in usedtags.keys(): - print(f"{colored(tag, 'red')} ") - for path in usedtags[tag]: - first, last = os.path.split(path) - print(f" {first}/{colored(last, 'blue')}") + printer.print_scriptags(usedtags, filename, solver.where) elif script_command == 'orphaned': orphaned_needles = []