From 2dccd0ee5fe6d31e908e248ddf6644feb73df16a Mon Sep 17 00:00:00 2001 From: Ammar64 Date: Thu, 7 Nov 2024 01:43:40 +0200 Subject: [PATCH] Make python script to check wether all categories are sorted or not --- ensure_sorted.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 ensure_sorted.py diff --git a/ensure_sorted.py b/ensure_sorted.py new file mode 100644 index 00000000..f954d1a6 --- /dev/null +++ b/ensure_sorted.py @@ -0,0 +1,75 @@ +#!/bin/python3 +import re + +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +class Category: + def __init__(self, name): + self.name = name + # a list of apps + self.apps = [] + + def add_app(self, app_str: str): + matches = re.findall("(?<=\\[\\*\\*).*(?=\\*\\*\\])", app_str) + if len(matches) != 1: + raise "These should be only one match" + app_name = matches[0] + # make it lower case and append it + self.apps.append(app_name.lower()) + + def is_sorted(self): + # I know not effiencent + return sorted(self.apps) == self.apps + + def __str__(self): + return str(self.apps) + + def __repr__(self): + return self.__str__() + +def main(): + readme_file = open('README.md', 'r') + # start of the Apps section + APPS_LINE_START = '## – Apps –\n' + lines = readme_file.readlines() + + index: int + try: + index = lines.index(APPS_LINE_START) + except ValueError: + print(f'String "{APPS_LINE_START}" was not found in README.md it\'s needed to determine the start of the app categories') + exit(1) + + categories = [] + for i in range(index+1, len(lines)): + # This is a category + if lines[i].startswith("### •"): + category = Category(lines[i][6:-1]) + categories.append(category) + # This is an app + elif lines[i].startswith("*"): + # The last category in the categories list is the one we're working on + category = categories[-1] + category.add_app(lines[i]) + + all_sorted = True + for i in categories: + if not i.is_sorted(): + print(f'Category {bcolors.OKBLUE}{i.name}{bcolors.ENDC} is not sorted') + all_sorted = False + + if not all_sorted: + exit(2) + + +if __name__ == "__main__": + main() \ No newline at end of file