-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (132 loc) · 4.51 KB
/
main.py
File metadata and controls
155 lines (132 loc) · 4.51 KB
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
150
151
152
153
154
155
import json
import os
from distutils.dir_util import copy_tree
import yaml
from flask import Flask, render_template, send_from_directory
import requests
skarf_version = "v0.2.6"
print(f"Skarf {skarf_version}")
print("Loading Config...")
def load_config():
try:
with open("config/config.yml", "r") as f:
config = yaml.load(f.read(), Loader=yaml.Loader)
print("Loaded YAML")
except FileNotFoundError:
try:
with open("config/config.json", "r") as f:
config = json.loads(f.read())
print("Loaded JSON")
except FileNotFoundError:
print("Could not find config")
config = None
if config:
for i in config['settings']['links']:
try:
i['copy']
except KeyError:
i['copy'] = False
try:
for i in config['settings']['mini-links']:
try:
i['copy']
except KeyError:
i['copy'] = False
except KeyError:
pass
else:
try:
print("Attempting to download example config(s).")
example_yml_t = ""
example_json_t = ""
print("Starting YAML Download")
example_yml = requests.get(
"https://raw.githubusercontent.com/woooferz/skarf/master/config/config.yml", timeout=3)
example_yml_t = ""
example_json_t = ""
if example_yml.status_code >= 200 and example_yml.status_code < 300:
example_yml_t = example_yml.text
else:
return None
with open("config/config.yml", "w") as f:
f.write(example_yml_t)
print("Example YAML Downloaded!")
print("Starting YAML Download")
example_json = requests.get(
"https://raw.githubusercontent.com/woooferz/skarf/master/config/config.json", timeout=3)
if example_json.status_code >= 200 and example_json.status_code < 300:
example_json_t = example_json.text
else:
return None
with open("config/config.json", "w") as f:
f.write(example_json_t)
print("Example JSON Downloaded!")
config = load_config()
except requests.ConnectionError:
print("Failed.")
return config
config = load_config()
if config:
print("Loaded!")
try:
if config["debug"]:
print("SKARF IS IN DEBUG MODE! THIS IS NOT PRODUCTION READY!")
except KeyError:
pass
else:
print("Error while loading config")
# print(config)
try:
if config:
if config['static'] and config['version']:
print("Static Mode: ON")
from jinja2 import Environment, FileSystemLoader
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
template = env.get_template(
f"versions/{str(config['version'])}.html")
output = template.render(config=config["settings"],
more=config,
version=skarf_version,)
try:
with open("build/index.html", "w") as f:
f.write(output)
except FileNotFoundError:
os.mkdir("build")
with open("build/index.html", "w") as f:
f.write(output)
copy_tree("res", "build/res")
copy_tree("static", "build/static")
print("Success.\n\nExiting!")
exit()
except KeyError:
pass
app = Flask(__name__)
@ app.route("/")
def home():
global config
if config:
try:
if config["debug"]:
print("Reloading Config! (THIS IS BECAUSE DEBUG MODE IS ON)")
config = load_config()
print("FINISHED RELOADING CONFIG")
except KeyError:
pass
if config:
return render_template(
f"versions/{str(config['version'])}.html",
config=config["settings"],
more=config,
version=skarf_version,
)
else:
return render_template("no_config.html", the_version=skarf_version)
@ app.route("/static/<path:path>")
def send_static(path):
return send_from_directory("static/", path)
@ app.route("/res/<path:path>")
def send_res(path):
return send_from_directory("res", path)
if __name__ == "__main__":
app.run(port=80, host="0.0.0.0")