gamelog/build.py

220 lines
5.7 KiB
Python
Raw Normal View History

2022-11-04 13:32:45 +01:00
#!/bin/env python
2022-07-08 11:25:57 +02:00
import yaml
from os.path import dirname
BASE = dirname(__file__) + '/src'
2023-01-13 00:20:02 +01:00
TITLE_LEFT = "List of Shame"
TITLE_RIGHT = "List of Pride"
2022-07-08 11:25:57 +02:00
MAX_WIDTH = 64
MAX_WIDTH_TITLE = 40
MAX_WIDTH_PLATFORM = 4
MAX_WIDTH_NAME = 54
TITLE_PADDING = 7
DELIMITER = "| :|"
# 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
# build_header using section title
2023-01-13 00:48:36 +01:00
def build_header(l_title, add_index=False, index_string=None):
2023-01-13 00:27:41 +01:00
header = []
len_title = len(l_title[0])
pad, pad_left = divmod(MAX_WIDTH - (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 ' '
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
# lower part
2023-01-13 00:48:36 +01:00
# optional navigation tag
if add_index == True and index_string != None:
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * (pad - 7) + f'[{index_string}] ')
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)
2023-01-13 00:27:41 +01:00
return header
2022-07-08 11:25:57 +02:00
2023-01-13 00:48:36 +01:00
def index_string(prefix, index):
return f'{prefix}{index:>03d}'
2022-07-08 11:25:57 +02:00
def build_index(yaml, prefix):
2023-01-13 00:27:41 +01:00
l_title = []
for item in yaml:
l_title += [item['title']]
2022-07-08 11:25:57 +02:00
2023-01-13 00:27:41 +01:00
l_index = []
for i, item in enumerate(l_title):
2023-01-13 00:48:36 +01:00
l_index += [' - ' + item.ljust(MAX_WIDTH - 10) + f'[{index_string(prefix, i+1)}] ']
2022-07-08 11:25:57 +02:00
2023-01-13 00:27:41 +01:00
l_index += [' ' * MAX_WIDTH]
2022-07-08 11:25:57 +02:00
2023-01-13 00:27:41 +01:00
return l_index
2022-07-08 11:25:57 +02:00
def build_game(name, platform):
2023-01-13 00:27:41 +01:00
s_platform = platform.ljust(MAX_WIDTH_PLATFORM)
s_name = name.ljust(MAX_WIDTH_NAME)
return f' {s_platform} | {s_name} '
2022-07-08 11:25:57 +02:00
#
# main
#
def main():
2023-01-13 00:27:41 +01:00
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')
2023-01-13 00:48:36 +01:00
item_count = 0
2023-01-13 00:27:41 +01:00
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
2023-01-13 00:48:36 +01:00
item_count += 1
todo_result += build_header(l_title, add_index=True, index_string=index_string('S', item_count))
2023-01-13 00:27:41 +01:00
# 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')
2023-01-13 00:48:36 +01:00
item_count = 0
2023-01-13 00:27:41 +01:00
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
2023-01-13 00:48:36 +01:00
item_count += 1
finished_result += build_header(l_title, add_index=True, index_string=index_string('P', item_count))
2023-01-13 00:27:41 +01:00
# 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...")
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")
print('done')
return
2022-07-08 11:25:57 +02:00
if __name__ == "__main__":
2023-01-13 00:27:41 +01:00
main()