gamelog/build.py

199 lines
5.3 KiB
Python
Executable File

#!/bin/env python3
from yaml import safe_load
from glob import glob
from os.path import dirname
BASE = dirname(__file__) + '/src'
FILES = sorted(glob(f'{BASE}/*.yaml'))
# GLOBAL VARIABLES
global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT
# CONSTANTS
MAX_WIDTH_PLATFORM = 4
MAX_WIDTH_NAME = 44
MAX_WIDTH_TITLE = MAX_WIDTH_NAME - 20
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=' '):
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
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_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
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(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 != 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 + len_wall(WALL_LEFT, WALL_RIGHT)))
return header
def index_identifier(prefix, index):
return f'{prefix}{index:>03d}'
def build_index(titles, prefix):
global CORNER_LEFT, CORNER_RIGHT, WALL_LEFT, WALL_RIGHT
index = [
' - ' + item.ljust((MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT)) - 10) + '[' + index_identifier(prefix, i+1) + '] '
for i, item in enumerate(titles)
]
# add one line of blank space for aesthetic reasons
index += [' ' * (MAX_WIDTH + len_wall(WALL_LEFT, WALL_RIGHT))]
return index
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.get('name')
platform = game.get('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')
lists = []
result = []
# 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')
return
if __name__ == "__main__":
main()