From 62ed4969c6f8ea5d3f0cdb7dab8f5e2991c358dc Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Mon, 21 Jul 2025 18:23:39 -0400 Subject: [PATCH] Update main.py --- src/main.py | 123 ++++++++++++++++------------------------------------ 1 file changed, 38 insertions(+), 85 deletions(-) diff --git a/src/main.py b/src/main.py index b8da9d4..2e53aa1 100644 --- a/src/main.py +++ b/src/main.py @@ -3,9 +3,11 @@ from __future__ import annotations from dataclasses import dataclass from types import NoneType from typing import Optional +from icecream.icecream import print_function from result import Result, Ok, Err from icecream import ic import os +import sys from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -13,9 +15,6 @@ if TYPE_CHECKING: WORK_DIR: StrPath = os.getcwd() -consts = dict() -inserts = dict() - @dataclass class Tag: value: str @@ -49,50 +48,8 @@ class Tag: return param_value - def inflate(self) -> Optional[str]: - match self.type(): - case 'include': - chunk = str(self.get_param('src')) - - html = HTML(open( - str(WORK_DIR) - + '/src/' - + chunk, - 'rt') - .read()) - - return html.inflate() - - case 'const': - global consts - id = str(self.get_param('id')) - - if self.self_closes(): - # Pull value - return consts[id] - else: - # Set value - consts[id] = self.trail - - case 'insert': - global inserts - id = str(self.get_param('id')) - - if self.self_closes(): - # Pull value - return inserts[id] - else: - # Set value - inserts[id] = self.trail - - case _: - return self.write() - - def self_closes(self): - return False if self.value.find('/>') == -1 else True - def write(self) -> str: - return f'<{self.value}>{self.trail if self.trail != None else ""}' + return f'<{self.value}>{self.trail if self.trail != None else ''}' @dataclass class HTML: @@ -121,56 +78,52 @@ class HTML: tags.append(Tag(tag, trail)) return tags - # This modifies the internal structure of the HTML instance. - def inflate(self) -> str: - tag_const = 0 - tag_include = 0 - tag_insert = 0 + def inflate(self) -> Result[str, str]: + file = str() - while (tag_const + tag_include + tag_insert) != 0: - file = str() + for tag in self.tags(): + if tag.type() == 'include': + chunk = tag.get_param('src') + if isinstance(chunk, NoneType): + return Err('FileNotFoundError') - tag_const = 0 - tag_include = 0 - tag_insert = 0 + html = HTML(open(str(WORK_DIR) + '/src/' + chunk, 'rt').read()) + file += html.inflate().expect('FileNotFoundError') + else: + file += tag.write() - # Must expand then then - for tag in self.tags(): - not_finished = True - - match tag.type(): - case 'const': - tag_const += 1 - case 'include': - tag_include += 1 - case 'insert': - tag_insert += 1 - case _: - pass - - inflated = tag.inflate() - if inflated is not None: - file += str(inflated) - - for tag in self.tags(): - if tag_const + tag_include + tag_insert - if (tag_const != 0) and (tag.type() == 'const'): - tag.inflate() - - self.value = file - - return self.value + return Ok(file) # Convert the HTML obj into a str to write to file. def write(self) -> str: - return self.inflate() + return self.inflate().unwrap() def main() -> None: - with open(str(WORK_DIR) + '/src/template.html', 'rt') as f: + # If: + # Incorrect number of arguments + # Long help flag + # Short help flag + if len(sys.argv) != 1 or ( + sys.argv[0] == '--help' + or sys.argv[0] == '-h' + ): + help() + return + + with open(str(WORK_DIR) + 'src' + sys.argv[0], 'rt') as f: html_src = HTML(f.read()) - with open(str(WORK_DIR) + '/target/index.html', 'w') as f: + with open(str(WORK_DIR) + 'target' + sys.argv[0], 'w') as f: f.write(html_src.write()) +def help() -> None: + print('Usage: python balloon.py [OPTIONS] ') + print() + print('Note: balloon implicitly assumes that is in src/, and should inflate into target/') + print() + print() + print('Options:') + print('-h, --help Print help') + if __name__ == '__main__': main()