#!/usr/bin/python3 import argparse import http.server import json import os import re import shutil import socketserver class Builder: def __init__(self): self.config_file_path = 'homemaker.conf' self.template_path = 'templates/template.html' self.build_directory = 'build' self.key_map = {} self.bg_map = {} self.bg_options = '' self.links = '' self.style_map = { 'title': 'Homepage', 'bg_img': 'null', 'font': 'monospace', 'font_color': '#e6e6fa', 'font_size': '25', 'link_color': '#e6e6fa', 'link_hover_color': '#ff3f33', } self.section_count = 0 self.section_links = '' self.section_array = [] self.section_map = {} def main(self): self.handle_backgrounds() self.set_up_build_dir() self.parse_config_file() self.cleanup() self.generate_files() self.copy_bgs() def set_up_build_dir(self): if os.path.exists(self.build_directory): print('Deleting build directory...') shutil.rmtree(self.build_directory) print('Creating build directory...') os.makedirs(self.build_directory) def handle_backgrounds(self): print('Parsing background images...') for root, dirs, files in os.walk("bg/", topdown=False): for idx, file_path in enumerate(files): file_name = file_path.split('/')[-1].split('.')[0] if idx == 0: self.style_map['bg_img'] = file_path self.bg_map[file_name] = file_path option = f'\t\n' self.bg_options += option def parse_config_file(self): print('Parsing config file...') with open(self.config_file_path) as f: mode = None for line in f: line = line.strip() if line == "[style]": print('Handling styles...') mode = 'style' continue if line == "[links]": print('Handling links...') mode = 'links' continue if mode == 'links': self.handle_links(line) elif mode == 'style': self.handle_styles(line) def handle_styles(self, line): style = re.split(r'[ \t]+', line) if style[0] in self.style_map: self.style_map[style[0]] = style[1] def handle_links(self, line): links = re.split(r'[ \t]+', line) if links[0] == 'section': self.section_count += 1 self.section_links += f'[{self.section_count}] {links[1]} ' self.section_array.append(links[1]) if self.links == '': self.links += f'