gamelog/build.py

198 lines
5.3 KiB
Python
Raw Permalink Normal View History

2022-11-04 13:32:45 +01:00
#!/bin/env python
from yaml import safe_load
from glob import glob
2022-07-08 11:25:57 +02:00
from os.path import dirname
BASE = dirname(__file__) + '/src'
FILES = glob(f'{BASE}/*.yaml')
2023-01-13 00:20:02 +01:00
# GLOBAL VARIABLES
global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT
# CONSTANTS
2023-01-13 00:20:02 +01:00
2022-07-08 11:25:57 +02:00
MAX_WIDTH_TITLE = 40
MAX_WIDTH_PLATFORM = 4
MAX_WIDTH_NAME = 54
MAX_WIDTH = 1 + MAX_WIDTH_PLATFORM + 3 + MAX_WIDTH_NAME + 1
2022-07-08 11:25:57 +02:00
TITLE_PADDING = 7
DELIMITER = "| :|"
# FUNCTIONS
2022-07-08 11:25:57 +02:00
# split strings longer than length into a list
# strings are split at last index of delim
def split_to_list(str, length, delim=' '):
2023-01-13 00:27:41 +01:00
l = []
2022-07-08 11:25:57 +02:00
2023-01-13 00:27:41 +01:00
while len(str) > length:
sep = str.rfind(delim, 0, length)
l.append(str[0:sep])
str = str[sep+1:]
2022-07-08 11:25:57 +02:00
2023-01-13 00:27:41 +01:00
l.append(str)
return l
2022-07-08 11:25:57 +02:00
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]
2023-01-13 00:27:41 +01:00
header = []
# prepare left and right padding to center title
2023-01-13 00:27:41 +01:00
len_title = len(l_title[0])
pad, pad_left = divmod((MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)) - (len_title + (TITLE_PADDING * 2)) - 2, 2)
2023-01-13 00:27:41 +01:00
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)
2023-01-13 00:27:41 +01:00
# ' ' padded Title
for title_index, title in enumerate(l_title):
char_pad = '_' if title_index+1 == len(l_title) else ' '
2023-01-13 00:27:41 +01:00
pad_right = len_title - len(title)
header.append(char_pad * (pad + pad_left) + '|' + ' ' * TITLE_PADDING + title + ' ' * (TITLE_PADDING + pad_right) + '|' + char_pad * pad)
2023-01-13 00:27:41 +01:00
# lower part
2023-01-13 00:48:36 +01:00
# optional navigation tag
if add_index == True and index != None:
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * (pad - 7) + f'[{index}] ')
2023-01-13 00:27:41 +01:00
else:
2023-01-13 00:48:36 +01:00
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * pad)
header.append(' ' * (MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)))
2023-01-13 00:27:41 +01:00
return header
2022-07-08 11:25:57 +02:00
def index_identifier(prefix, index):
2023-01-13 00:48:36 +01:00
return f'{prefix}{index:>03d}'
def build_index(titles, prefix):
global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT
2022-07-08 11:25:57 +02:00
index = [
' - ' + item.ljust((MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)) - 10) + '[' + index_identifier(prefix, i+1) + '] '
for i, item in enumerate(titles)
]
2022-07-08 11:25:57 +02:00
# add one line of blank space for aesthetic reasons
index += [' ' * (MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT))]
2022-07-08 11:25:57 +02:00
return index
2022-07-08 11:25:57 +02:00
def build_game(name, platform):
return f' {platform.ljust(MAX_WIDTH_PLATFORM)} | {name.ljust(MAX_WIDTH_NAME)} '
def build_table(index, games):
global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT
# 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 '', '', '', ''
2022-07-08 11:25:57 +02:00
########
# MAIN #
########
2022-07-08 11:25:57 +02:00
def main():
2023-01-13 00:27:41 +01:00
print('go')
lists = []
result = []
2023-01-13 00:27:41 +01:00
# parse every file into list
for file_index, file_item in enumerate(FILES):
with open(file_item, 'r') as file:
yaml = safe_load(file)
2023-01-13 00:27:41 +01:00
# 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)
2023-01-13 00:27:41 +01:00
# create list by opening with header
list_result = build_header(yaml['title'])
2023-01-13 00:27:41 +01:00
# add index right after
list_result += build_index([ i['title'] for i in yaml['sections'] ], yaml['prefix'])
2023-01-13 00:27:41 +01:00
# 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'])
2023-01-13 00:27:41 +01:00
lists.insert(file_index, list_result)
2023-01-13 00:27:41 +01:00
max_length = len(max(lists, key=len))
2023-01-13 00:27:41 +01:00
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))
2023-01-13 00:27:41 +01:00
if list_index == 0:
result = list_item
continue
2023-01-13 00:27:41 +01:00
# add list to result line by line
for game_index, game_item in enumerate(list_item):
result[game_index] += ( DELIMITER + game_item )
2023-01-13 00:27:41 +01:00
with open('gamelog.txt', 'w') as file:
for line in result:
file.write(f"{line}\n")
2023-01-13 00:27:41 +01:00
print('done')
return
2022-07-08 11:25:57 +02:00
if __name__ == "__main__":
2023-01-13 00:27:41 +01:00
main()