-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (122 loc) · 3.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from time import sleep
from typing import Type
from markupsafe import Markup
from config import load_config
from jinja2 import (
Environment,
PackageLoader,
StrictUndefined,
TemplateError,
select_autoescape,
)
from rich.console import Console
from markdown_parser import build_markdown
from processors import *
from watch import watch_files
from server import base_url, serve_forever, serve_until
from utils import *
import requests
import sass
import typer
import xml.etree.ElementTree as ET
import pdfkit
config = load_config()
console = Console()
app = typer.Typer()
env = Environment(
loader=PackageLoader(config.package_name),
autoescape=select_autoescape(),
undefined=StrictUndefined,
)
md = build_markdown()
env.filters["markdown"] = lambda text: Markup(md.convert(text))
processors: list[Type[Processor]] = [
HomePage,
PdfResume,
CustomPages,
Blog,
Assets,
Downloads,
CName,
SiteMap, # Has to be the last one
]
def reset_output():
shutil.rmtree(config.output_path, ignore_errors=True)
Path(config.output_path).mkdir(exist_ok=True)
def build_all(develop_mode: bool):
config.develop_mode = develop_mode
print("Compiling...", end="")
reset_output()
sitemap_data = SiteMapData()
try:
for processor_class in processors:
processor_class(env, config).run(sitemap_data)
print(" [Done]")
except (TemplateError, sass.CompileError):
console.print_exception(show_locals=True)
def on_change(event):
global config
config = load_config()
build_all(develop_mode=True)
export_resume_pdf()
def export_resume_pdf():
server_thread, httpd = serve_until("localhost", 8090, config, lambda: keep_running)
sleep(1)
print("Exporting the PDF...")
pdfkit.from_url(
"http://localhost:8090/pdf_resume.html",
f"{config.output_path}/{config.pdf_resume_path}",
)
print("Stopping the server...")
httpd.shutdown()
server_thread.join()
@app.command()
def build():
build_all(develop_mode=False)
export_resume_pdf()
@app.command()
def serve(
watch: bool = typer.Option(False, help="Watch for changes."),
addr: str = "localhost",
port: int = 8000,
):
config.base_url = base_url(addr, port)
on_change(None)
observer = None
if watch:
print("Monitoring for changes...")
observer = watch_files(
on_change,
ignore_regexes=[r"\./.+\.py", r"\./docs(/.+)?"],
ignore_directories=True,
)
try:
if observer:
observer.start()
serve_forever(addr, port, config)
except KeyboardInterrupt:
if observer:
observer.stop()
observer.join()
@app.command()
def test_sitemap():
build()
tree = ET.parse("docs/sitemap.xml")
root = tree.getroot()
for url_element in root:
loc = url_element.findtext("./{*}loc")
lastmod = url_element.findtext("./{*}lastmod")
resp = requests.get(loc)
print(loc, end=" ")
status_ok = resp.status_code == 200
content_ok = "<title>Amir Karimi" in str(resp.content) or loc.endswith(".pdf")
if status_ok and content_ok:
console.print("[OK]", style="green")
else:
console.print("[ERROR]", end=" ", style="red")
if not status_ok:
print(f"status: {resp.status_code}")
else:
print(f"content: {resp.content[:100]}...")
if __name__ == "__main__":
app()