Update main.py
This commit is contained in:
123
src/main.py
123
src/main.py
@@ -3,9 +3,11 @@ 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
|
from icecream import ic
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -13,9 +15,6 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
WORK_DIR: StrPath = os.getcwd()
|
WORK_DIR: StrPath = os.getcwd()
|
||||||
|
|
||||||
consts = dict()
|
|
||||||
inserts = dict()
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Tag:
|
class Tag:
|
||||||
value: str
|
value: str
|
||||||
@@ -49,50 +48,8 @@ class Tag:
|
|||||||
|
|
||||||
return param_value
|
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:
|
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:
|
||||||
@@ -121,56 +78,52 @@ class HTML:
|
|||||||
tags.append(Tag(tag, trail))
|
tags.append(Tag(tag, trail))
|
||||||
return tags
|
return tags
|
||||||
|
|
||||||
# This modifies the internal structure of the HTML instance.
|
def inflate(self) -> Result[str, str]:
|
||||||
def inflate(self) -> str:
|
file = str()
|
||||||
tag_const = 0
|
|
||||||
tag_include = 0
|
|
||||||
tag_insert = 0
|
|
||||||
|
|
||||||
while (tag_const + tag_include + tag_insert) != 0:
|
for tag in self.tags():
|
||||||
file = str()
|
if tag.type() == 'include':
|
||||||
|
chunk = tag.get_param('src')
|
||||||
|
if isinstance(chunk, NoneType):
|
||||||
|
return Err('FileNotFoundError')
|
||||||
|
|
||||||
tag_const = 0
|
html = HTML(open(str(WORK_DIR) + '/src/' + chunk, 'rt').read())
|
||||||
tag_include = 0
|
file += html.inflate().expect('FileNotFoundError')
|
||||||
tag_insert = 0
|
else:
|
||||||
|
file += tag.write()
|
||||||
|
|
||||||
# Must expand <const> then <include> then <insert>
|
return Ok(file)
|
||||||
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
|
|
||||||
|
|
||||||
# Convert the HTML obj into a str to write to file.
|
# Convert the HTML obj into a str to write to file.
|
||||||
def write(self) -> str:
|
def write(self) -> str:
|
||||||
return self.inflate()
|
return self.inflate().unwrap()
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
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())
|
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())
|
f.write(html_src.write())
|
||||||
|
|
||||||
|
def help() -> None:
|
||||||
|
print('Usage: python balloon.py [OPTIONS] <SOURCE>')
|
||||||
|
print()
|
||||||
|
print('Note: balloon implicitly assumes that <SOURCE> is in src/, and should inflate into target/')
|
||||||
|
print()
|
||||||
|
print()
|
||||||
|
print('Options:')
|
||||||
|
print('-h, --help Print help')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user