Migrate to a semi-supported fork of result.py

This commit is contained in:
Olivia Brooks
2025-09-09 11:25:14 -04:00
parent 0ea69a5c89
commit 9f26939126
4 changed files with 54 additions and 17 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
**/target **/target
.venv/
**/__pycache__

13
pyproject.toml Normal file
View File

@@ -0,0 +1,13 @@
[project]
name = "balloon"
version = "0.0.1"
description = "Reuse chunks of HTML in other HTML files via preprocessing."
authors = ["Cutieguwu <olivia.a.brooks77@gmail.com>"]
dependencies = [
"result",
]
requires-python = ">=3.13"
[tool.uv.sources]
result = { git = "https://github.com/montasaurus/result" }

View File

@@ -3,9 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from types import NoneType from types import NoneType
from typing import Optional from typing import Optional
from icecream.icecream import print_function
from result import Result, Ok, Err from result import Result, Ok, Err
from icecream import ic
import os import os
import sys import sys
@@ -49,14 +47,14 @@ class Tag:
return param_value return param_value
def write(self) -> str: 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 @dataclass
class HTML: class HTML:
value: str value: str
# Returns all tags in order in the html file. # Returns all tags in order in the html file.
def tags(self) -> list[Tag]: def tags(self) -> Result[list[Tag], str]:
tag = str() tag = str()
trail: Optional[str] = str() trail: Optional[str] = str()
tags = list() tags = list()
@@ -72,16 +70,25 @@ class HTML:
record = not record # why can't I have ! operator... record = not record # why can't I have ! operator...
elif record == True: elif record == True:
tag += c tag += c
else: else:
try:
assert type(trail) == str
trail += c trail += c
except AssertionError:
return Err('Invalid HTML Structure')
tags.append(Tag(tag, trail)) tags.append(Tag(tag, trail))
return tags return Ok(tags)
def inflate(self) -> Result[str, str]: def inflate(self) -> Result[str, str]:
file = str() file = str()
for tag in self.tags(): tags = self.tags()
if tags.is_err():
return Err(tags.err()) # type: ignore[arg-type]
for tag in tags.ok(): # type: ignore[union-attr]
if tag.type() == 'include': if tag.type() == 'include':
chunk = tag.get_param('src') chunk = tag.get_param('src')
if isinstance(chunk, NoneType): if isinstance(chunk, NoneType):
@@ -98,36 +105,32 @@ class HTML:
def write(self) -> str: def write(self) -> str:
return self.inflate().unwrap() return self.inflate().unwrap()
def main() -> None: def main() -> None:
# If: # If:
# Incorrect number of arguments # Incorrect number of arguments
# Long help flag # Long help flag
# Short help flag # Short help flag
if len(sys.argv) != 2 or ( if len(sys.argv) != 3 or (
sys.argv[0] == '--help' sys.argv[0] == '--help'
or sys.argv[0] == '-h' or sys.argv[0] == '-h'
): ):
help() help()
return return
file_name = sys.argv[1].removeprefix('src/') with open(str(WORK_DIR) + sys.argv[1], 'rt') as f:
with open(str(WORK_DIR) + 'src/' + file_name, 'rt') as f:
html_src = HTML(f.read()) html_src = HTML(f.read())
# Patch to make sure that target paths are available.
try: try:
os.makedirs(str(WORK_DIR) + 'target/' + os.path.dirname(file_name)) os.makedirs(str(WORK_DIR) + os.path.dirname(sys.argv[2]))
except FileExistsError: except FileExistsError:
pass pass
with open(str(WORK_DIR) + 'target/' + file_name, 'w') as f: with open(str(WORK_DIR) + sys.argv[2], 'w') as f:
f.write(html_src.write()) f.write(html_src.write())
def help() -> None: def help() -> None:
print('Usage: python balloon.py [OPTIONS] <SOURCE>') print('Usage: python balloon.py [OPTIONS] <SOURCE> <DESTINATION>')
print()
print('Note: balloon implicitly assumes that <SOURCE> is in src/, and should inflate into target/')
print() print()
print() print()
print('Options:') print('Options:')

19
uv.lock generated Normal file
View File

@@ -0,0 +1,19 @@
version = 1
revision = 3
requires-python = ">=3.13"
[[package]]
name = "balloon"
version = "0.0.1"
source = { virtual = "." }
dependencies = [
{ name = "result" },
]
[package.metadata]
requires-dist = [{ name = "result", git = "https://github.com/montasaurus/result" }]
[[package]]
name = "result"
version = "0.19.0"
source = { git = "https://github.com/montasaurus/result#59cd2fb7b57e4851a7054917fbd03c4c8f68a62f" }