This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·226 lines (188 loc) · 7.09 KB
/
server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
import sys, os, airspeed, glob
from sanic import Sanic, response
from common import mergeTemplate, withResource, call
import card
import colorscheme
import svg
import config
app = Sanic()
@app.get("/")
async def root(request):
return response.redirect('/cards/')
@app.get("/cards/")
@app.post("/cards/")
@mergeTemplate("cards/cardlist.vm")
async def show_cardlist(request):
if "action" in request.form and "filename" in request.form:
with card.open_file(request.form.get("filename")) as c:
if request.form.get("action") == "increment_stock":
c.copies_owned = int(c.copies_owned) + 1
elif request.form.get("action") == "decrement_stock":
c.copies_owned = int(c.copies_owned) - 1
cards = card.from_dir(config.carddir)
sorting_key = ""
if "sort" in request.args:
sorting_key = request.args["sort"][0]
if "sort" in request.form:
sorting_key = request.form["sort"][0]
if sorting_key:
if sorting_key == "filename":
cards = sorted(cards, key=lambda x: x.filename.lower())
elif sorting_key == "cp":
cards = sorted(cards, key=lambda x: -int(x.cp or 0))
elif sorting_key == "description":
cards = sorted(cards, key=lambda x: x.description or "")
elif sorting_key == "copies":
cards = sorted(cards, key=lambda x: -int(x.copies_owned))
elif sorting_key == "title":
cards = sorted(cards, key=lambda x: x.title.lower())
elif sorting_key == "tag":
cards = sorted(cards, key=lambda x: x.tag or "\0")
filter_key = ""
if "filter" in request.args:
filter_key = request.args["filter"][0]
if "filter" in request.form:
filter_key = request.form["filter"][0]
if filter_key:
cards = [i for i in cards if i.tag.lower() == filter_key.lower()]
sum_cp = sum(int(i.copies_owned) * int(i.cp or 0) for i in cards)
sum_copies = sum(int(i.copies_owned) for i in cards)
return locals()
@app.get('/cards/creator')
@mergeTemplate("cards/creator.vm")
async def show_creator(request):
if "filename" in request.args:
initialcard = card.from_file(str(request.args["filename"][0])+".yaml")
else:
initialcard = card.Card()
initialcard.filename = card.get_vacant_filename()
#initialcard.power = ""
#initialcard.cp = ""
#initialcard.steps = ""
#initialcard.effects = ""
#initialcard.flags = ""
return {"card":initialcard}
@app.post('/cards/creator')
@mergeTemplate("cards/creator.vm")
async def show_creator(request):#not used atm
initialcard = card.from_form(request.form)
#find vacant fileame:
if not initialcard.filename:
initialcard.filename = card.get_vacant_filename()
return {"card":initialcard}
@app.get('/cards/show')
@mergeTemplate("cards/card.vm")
async def show_cards(request):
cards = []
if "card" in request.args:
for i in request.args["card"]:
if "/" in i or "\\" in i: return response.redirect('/cards/')
cards.append(card.from_file(i+".yaml"))
elif "stock" in request.args:
for i in card.from_dir(config.carddir):
cards.extend([i]*int(i.copies_owned))
else:
return response.redirect('/cards/')
return locals()
@app.post('/cards/preview')
@mergeTemplate("cards/card.vm")
async def preview_card(request):
cards = [card.from_form(request.form)]
if "save" in request.form:
if not cards[0].filename:
cards[0].filename = card.get_vacant_filename()
card.to_file(cards[0])
was_saved = True
if "delete" in request.form:
card.del_file(cards[0])
was_deleted = True
return locals()
@app.get("/cards/colors")
@mergeTemplate("cards/colors.vm")
async def get_color_picker(request):
colorschemes = colorscheme.list_all()
print(colorschemes)
return locals()
@app.get("/card/colors/preview")
@mergeTemplate("cards/card.vm")
async def get_color_preview(request):
cards = [card.Card()]
cards[0].colorscheme = request.args.get(schemename) or ""
cards[0].title = cards[0].colorscheme or "Default Colors"
cards[0].figure = ""
cards[0].figure_source = ""
cards[0].description = "This is a description"
cards[0].steps = ["Do A", "Then B"]
cards[0].cost = "Something"
cards[0].power = 5
cards[0].cp = 50
return locals()
@app.post("/card/colors/preview")
@mergeTemplate("cards/card.vm")
async def get_color_preview(request):
cards = [card.Card()]
cards[0].colorscheme = request.args.get(schemename) or ""
cards[0].title = cards[0].colorscheme or "Default Colors"
cards[0].figure = ""
cards[0].figure_source = ""
cards[0].description = "This is a description"
cards[0].steps = ["Do A", "Then B"]
cards[0].cost = "Something"
cards[0].power = 5
cards[0].cp = 50
return locals()
@app.get("/cards/svg")
@mergeTemplate("cards/svg.vm")
async def svg_list(request):
filter = request.args.get("filter")
page = int(request.args.get("page") or 1)
current_collection = request.args.get("collection")
collections = svg.list_collections()
svgs = svg.list_all(current_collection)
if filter:
svgs = [i for i in svgs if filter in i]
if len(svgs) > page*config.svg_page_size:
has_next = True
svgs.sort()
svgs = svgs[(page-1)*config.svg_page_size:page*config.svg_page_size]
return locals()
@app.post("/cards/svg")
@mergeTemplate("cards/svg.vm")
async def svg_add(request):
file = request.files.get("file")
name = request.form.get("name") or file.name
if name[-4:] == ".svg": name = name[:-4]
page = 1
current_collection = None
collections = svg.list_collections()
filter = None
svg.store(name, file.body)
uploaded = True
svgs = svg.list_all()
return locals()
#add static resources (with caching):
for file in glob.iglob(os.path.join(config.resourcedir, "**","*"), recursive=True):
@call
def temp():#namespace hack to store filetype
filetype = file.split('.')[-1]
if filetype in ("html", "css", "js"):
route = os.path.relpath(file, config.resourcedir)
if filetype == "js": filetype = "javascript"
print("Adding static resource", repr(route))
@app.get(f"/{route}")
@withResource(route)
async def card_style(request, file):
return response.text(file, headers={"Content-Type": f"text/{filetype}"})
#add svgs:
@app.get(f"/svg/<name>.svg")
async def get_svg(request, name):
color = request.args.get("color")
return response.text(svg.get(name, color), headers={"Content-Type": "image/svg+xml"})
@app.get(f"/svg/<collection>/<name>.svg")
async def get_svg(request, collection, name):
color = request.args.get("color")
return response.text(svg.get(os.path.join(collection, name), color), headers={"Content-Type": "image/svg+xml"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
print("Exiting...\n")