From 2447ce2af3a446d72d37ac59f8c51006024e44ed Mon Sep 17 00:00:00 2001 From: fanyx Date: Tue, 17 Jan 2023 19:29:16 +0100 Subject: [PATCH] Rework build.py to support more than 2 lists --- build.py | 284 +++++++++++++++++++++++++------------------------------ 1 file changed, 131 insertions(+), 153 deletions(-) diff --git a/build.py b/build.py index b3656f7..b36b2cd 100755 --- a/build.py +++ b/build.py @@ -1,20 +1,27 @@ #!/bin/env python -import yaml +from yaml import safe_load +from glob import glob from os.path import dirname BASE = dirname(__file__) + '/src' +FILES = glob(f'{BASE}/*.yaml') -TITLE_LEFT = "List of Shame" -TITLE_RIGHT = "List of Pride" +# GLOBAL VARIABLES + +global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT + +# CONSTANTS -MAX_WIDTH = 64 MAX_WIDTH_TITLE = 40 MAX_WIDTH_PLATFORM = 4 MAX_WIDTH_NAME = 54 +MAX_WIDTH = 1 + MAX_WIDTH_PLATFORM + 3 + MAX_WIDTH_NAME + 1 TITLE_PADDING = 7 DELIMITER = "| :|" +# FUNCTIONS + # split strings longer than length into a list # strings are split at last index of delim def split_to_list(str, length, delim=' '): @@ -29,187 +36,158 @@ def split_to_list(str, length, delim=' '): return l -# build_header using section title -def build_header(l_title, add_index=False, index_string=None): +def len_wall(wl, wr): + return len(wl + wr) + +def build_header(s_title, add_index=False, index=None): + global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT + + # split title to list if it's too long + if len(s_title) > MAX_WIDTH_TITLE: + l_title = split_to_list(s_title, MAX_WIDTH_TITLE) + else: + l_title = [s_title] + header = [] + # prepare left and right padding to center title len_title = len(l_title[0]) - pad, pad_left = divmod(MAX_WIDTH - (len_title + (TITLE_PADDING * 2)) - 2, 2) + pad, pad_left = divmod((MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)) - (len_title + (TITLE_PADDING * 2)) - 2, 2) len_border = len_title + ((TITLE_PADDING * 2) - 2) # upper part header.append(' ' * (pad + pad_left + 2) + '_' * len_border + ' ' * (pad + 2)) header.append('_' * (pad + pad_left) + '//' + ' ' * len_border + '\\\\' + '_' * pad) + # ' ' padded Title - count = 1 - for title in l_title: - c_pad = '_' if count == len(l_title) else ' ' + for title_index, title in enumerate(l_title): + char_pad = '_' if title_index+1 == len(l_title) else ' ' pad_right = len_title - len(title) - header.append(c_pad * (pad + pad_left) + '|' + ' ' * TITLE_PADDING + title + ' ' * (TITLE_PADDING + pad_right) + '|' + c_pad * pad) - count += 1 + header.append(char_pad * (pad + pad_left) + '|' + ' ' * TITLE_PADDING + title + ' ' * (TITLE_PADDING + pad_right) + '|' + char_pad * pad) + # lower part # optional navigation tag - if add_index == True and index_string != None: - header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * (pad - 7) + f'[{index_string}] ') + if add_index == True and index != None: + header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * (pad - 7) + f'[{index}] ') else: header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * pad) - - header.append(' ' * MAX_WIDTH) + + header.append(' ' * (MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT))) return header -def index_string(prefix, index): +def index_identifier(prefix, index): return f'{prefix}{index:>03d}' -def build_index(yaml, prefix): - l_title = [] - for item in yaml: - l_title += [item['title']] +def build_index(titles, prefix): + global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT - l_index = [] - for i, item in enumerate(l_title): - l_index += [' - ' + item.ljust(MAX_WIDTH - 10) + f'[{index_string(prefix, i+1)}] '] + index = [ + ' - ' + item.ljust((MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)) - 10) + '[' + index_identifier(prefix, i+1) + '] ' + for i, item in enumerate(titles) + ] - l_index += [' ' * MAX_WIDTH] + # add one line of blank space for aesthetic reasons + index += [' ' * (MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT))] - return l_index + return index def build_game(name, platform): - s_platform = platform.ljust(MAX_WIDTH_PLATFORM) - s_name = name.ljust(MAX_WIDTH_NAME) + return f' {platform.ljust(MAX_WIDTH_PLATFORM)} | {name.ljust(MAX_WIDTH_NAME)} ' - return f' {s_platform} | {s_name} ' +def build_table(index, games): + global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT -# -# main -# + # top of table + table_result = [ + CORNER_LEFT + '_' * (MAX_WIDTH_PLATFORM + 2) + ' ' + '_' * (MAX_WIDTH_NAME + 2) + CORNER_RIGHT + , WALL_LEFT + '=' * (MAX_WIDTH_PLATFORM + 2) + '|' + '=' * (MAX_WIDTH_NAME + 2) + WALL_RIGHT + ] + + # actual game text + for game in games: + name = game['name'] + platform = game['platform'] + if len(platform) > MAX_WIDTH_PLATFORM: + raise Exception(f"Platform shortcode for {name} is longer than {MAX_WIDTH_PLATFORM} characters. Invalid.") + + if len(name) > MAX_WIDTH_NAME: + for i,i_name in enumerate(split_to_list(name, MAX_WIDTH_NAME)): + table_result += [ + WALL_LEFT + build_game(i_name, platform if i == 0 else '') + WALL_RIGHT + ] + else: + table_result += [ + WALL_LEFT + build_game(name, platform) + WALL_RIGHT + ] + + # bottom of table + table_result += [ + WALL_LEFT + '_' * (MAX_WIDTH_PLATFORM + 2) + '|' + '_' * (MAX_WIDTH_NAME + 2) + WALL_RIGHT + ] + + return table_result + +def get_border_for_index(index): + last_index = len(FILES) - 1 + if index == 0: + return ' ', '', '|', '' + elif index == last_index: + return '', ' ', '', '|' + else: + return '', '', '', '' + +######## +# MAIN # +######## def main(): print('go') - with open(f'{BASE}/todo.yaml', 'r') as file: - todo_yaml = yaml.safe_load(file) - - with open(f'{BASE}/finished.yaml', 'r') as file: - finished_yaml = yaml.safe_load(file) - - # - # Section: TO-DO - # - - todo_result = [] - - todo_result += build_header([TITLE_LEFT]) - - todo_result += build_index(todo_yaml, 'S') - - item_count = 0 - for section in todo_yaml: - s_title = section['title'] - l_title = [] - - if len(s_title) > MAX_WIDTH_TITLE: - l_title = split_to_list(s_title, MAX_WIDTH_TITLE) - else: - l_title = [s_title] - - # Create section header - item_count += 1 - todo_result += build_header(l_title, add_index=True, index_string=index_string('S', item_count)) - - # Create table header - todo_result += [' ______ ________________________________________________________', - '|======|========================================================'] - - # Add games one by one - # Prefix : | - for game in section['games']: - s_name = game['name'] - s_platform = game['platform'] - - if len(s_platform) > MAX_WIDTH_PLATFORM: - raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...") - - if len(s_name) > MAX_WIDTH_NAME: - l_name = split_to_list(s_name, MAX_WIDTH_NAME) - for i,i_name in enumerate(l_name): - todo_result += ['|' + build_game(i_name, s_platform if i == 0 else '')] - else: - todo_result += ['|' + build_game(s_name, s_platform)] - - # Close table header - todo_result += ['|______|________________________________________________________', - ' ' * MAX_WIDTH] - - # - # Section: Finished - # - - finished_result = [] - - finished_result += build_header([TITLE_RIGHT]) - - finished_result += build_index(finished_yaml, 'P') - - item_count = 0 - for section in finished_yaml: - s_title = section['title'] - l_title = [] - - if len(s_title) > MAX_WIDTH_TITLE: - l_title = split_to_list(s_title, MAX_WIDTH_TITLE) - else: - l_title = [s_title] - - # Create section header - item_count += 1 - finished_result += build_header(l_title, add_index=True, index_string=index_string('P', item_count)) - - # Create table header - finished_result += ['______ ________________________________________________________ ', - '======|========================================================|'] - - # Add games one by one - # Suffix : | - for game in section['games']: - s_name = game['name'] - s_platform = game['platform'] - - if len(s_platform) > MAX_WIDTH_PLATFORM: - raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...") - - if len(s_name) > MAX_WIDTH_NAME: - l_name = split_to_list(s_name, MAX_WIDTH_NAME) - for i,i_name in enumerate(l_name): - finished_result += [build_game(i_name, platform if i == 0 else '') + '|'] - else: - finished_result += [build_game(s_name, s_platform) + '|'] - - # Close table header - finished_result += ['______|________________________________________________________|', - ' ' * MAX_WIDTH] - - # adjust length of both sides - len_difference = len(todo_result) - len(finished_result) - # todo > finished - if len_difference > 0: - finished_result += [' ' * MAX_WIDTH] * len_difference - # todo < finished - elif len_difference < 0: - len_difference *= -1 - todo_result += [' ' * MAX_WIDTH] * len_difference - - if len(todo_result) != len(finished_result): - raise Exception("Results are not of equal size. Something went wrong...") + lists = [] result = [] - i = 0 - while len(todo_result) > i: - result += [todo_result[i] + DELIMITER + finished_result[i]] - i += 1 - with open('list-of-game.txt', 'w') as file: - for i in result: - file.write(f"{i}\n") + # parse every file into list + for file_index, file_item in enumerate(FILES): + with open(file_item, 'r') as file: + yaml = safe_load(file) + + # prepare corners and walls + global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT + CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT = get_border_for_index(file_index) + + # create list by opening with header + list_result = build_header(yaml['title']) + + # add index right after + list_result += build_index([ i['title'] for i in yaml['sections'] ], yaml['prefix']) + + # build sections + for section_index, section in enumerate(yaml['sections']): + list_result += build_header(section['title'], add_index=True, index=index_identifier(yaml['prefix'], section_index+1)) + list_result += build_table(file_index, section['games']) + + lists.insert(file_index, list_result) + + max_length = len(max(lists, key=len)) + + for list_index, list_item in enumerate(lists): + if len(list_item) < max_length: + _, _, wl, wr = get_border_for_index(list_index) + + list_item += [' ' * (MAX_WIDTH + len_wall(wl, wr))] * (max_length - len(list_item)) + + if list_index == 0: + result = list_item + continue + + # add list to result line by line + for game_index, game_item in enumerate(list_item): + result[game_index] += ( DELIMITER + game_item ) + + with open('gamelog.txt', 'w') as file: + for line in result: + file.write(f"{line}\n") print('done')