gamelog/build.py

203 lines
6.0 KiB
Python
Raw Normal View History

2022-07-08 11:25:57 +02:00
import yaml
from os.path import dirname
# BASE = dirname('/home/hendrik/git/list-of-game/src/build.py')
BASE = dirname(__file__) + '/src'
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=' '):
l = []
while len(str) > length:
sep = str.rfind(delim, 0, length)
l.append(str[0:sep])
str = str[sep+1:]
l.append(str)
return l
# build_header using section title
def build_header(l_title):
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
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * pad)
header.append(' ' * MAX_WIDTH)
return header
def build_index(yaml, prefix):
l_title = []
for item in yaml:
l_title += [item['title']]
l_index = []
for i, item in enumerate(l_title):
l_index += [' - ' + item.ljust(MAX_WIDTH - 10) + f'[{prefix}{i+1}00] ']
l_index += [' ' * MAX_WIDTH]
return l_index
def build_game(name, platform):
s_platform = platform.ljust(MAX_WIDTH_PLATFORM)
s_name = name.ljust(MAX_WIDTH_NAME)
return f' {s_platform} | {s_name} '
#
# 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(['List of Shame'])
todo_result += build_index(todo_yaml, 'S')
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
todo_result += build_header(l_title)
# 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(['List of Pride'])
finished_result += build_index(finished_yaml, 'P')
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
finished_result += build_header(l_title)
# 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")
2022-07-30 17:59:12 +02:00
print('done')
2022-07-08 11:25:57 +02:00
return
if __name__ == "__main__":
main()