-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
543 lines (393 loc) · 18.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import subprocess
import json
import time
import argparse
import re
import os
import sys
import base64
from http.server import SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn, TCPServer
import threading as thr
import signal
import shutil
import numpy as np
#import tools
stop_gen_event = thr.Event()
generating = thr.Event()
failed_img = ["./failed_gen.png"]
all_parameters = {}
received_params = {}
def get_python_executable():
python_exec = sys.executable
if python_exec and shutil.which(python_exec):
return python_exec
for python_cmd in ["python3", "python"]:
if shutil.which(python_cmd):
return python_cmd
raise RuntimeError("Python not found!")
def divide_in_squares(list_c, xmin, xmax, ymin, ymax):
"""Recalculate boundaries based on grid divisions."""
list_c = np.array(list_c) - [1, 1, 0]
for col, line, n_squares in list_c:
size_x = (xmax - xmin) / n_squares
size_y = (ymax - ymin) / n_squares
xmin, xmax = xmin + col * size_x, xmin + (col + 1) * size_x
ymin, ymax = ymin + line * size_y, ymin + (line + 1) * size_y
return xmin, xmax, ymin, ymax
python_exe = get_python_executable()
current_client_id = 0
def set_current_gen(id):
global current_client_id
current_client_id = id
def process_form_data(params, timeout, noserverIsOn=False):
global all_parameters, stop_gen_event
def is_url_or_path(value):
url_regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # Domain
r'localhost|' # localhost
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # IPv4
r'?[A-F0-9]*:[A-F0-9:]+?)' # IPv6
r'(?::\d+)?' # port
r'(?:/?|[/?]\S+)$', # path
re.IGNORECASE
)
b64_regex = re.compile(
r'^data:image/([a-zA-Z]+);base64,[A-Za-z0-9+/]+={0,2}$', # Base64 images
re.IGNORECASE
)
if re.match(b64_regex, value):
return "b64"
elif re.match(url_regex, value):
return "url"
elif os.path.isfile(value):
return "image path"
else:
return "invalid"
def download_image(url):
import hashlib
# Check if the value is a URL
url_type = is_url_or_path(url)
if url_type != "url" and url_type != "b64":
return url # If not a URL, return the original value
elif url_type == "b64":
header, image_data = url.split(",")
header = header.split(";")[0].split(":")[1]
ext = header.split("/")[-1] # Extract the extension (e.g., png, jpeg)
if ext not in ['png', 'jpg', 'jpeg']:
ext = 'png' # Fallback to PNG if unsupported format
# Decode the Base64 string to get image data
image_data = base64.b64decode(image_data)
# Generate an MD5 hash for the image data
md5_hash = hashlib.md5(image_data).hexdigest()
file_name = f"./palettes/{md5_hash}.{ext}"
# Check if the file already exists
if os.path.isfile(file_name):
return file_name
# Save the image
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with open(file_name, 'wb') as f:
f.write(image_data)
return file_name
# Download the image
import requests
response = requests.get(url)
if response.status_code == 200:
# Calculate MD5 hash of the content
md5_hash = hashlib.md5(response.content).hexdigest()
# Determine the extension
ext = url.split('.')[-1].lower()
if ext not in ['png', 'jpg', 'jpeg']:
ext = 'png' # Fallback to PNG if unsupported format
file_name = f"./palettes/{md5_hash}.{ext}"
if os.path.isfile(file_name):
return file_name
with open(file_name, 'wb') as file:
file.write(response.content)
return file_name
else:
raise Exception(f"Failed to download the image. Status Code: {response.status_code}")
all_parameters['expression'] = params.get('expression', 'z*z+c')
all_parameters['fractals'] = params.get("fractals", {
'mandelbrot': False,
'juliaset': False,
'newton' : False,
'newton_juliaset': False,
'magnet': False,
'lyapunov': False,
'sandpile': False,
})
all_parameters['width'] = int(params.get('width', 1024))
all_parameters['height'] = int(params.get('height', 1024))
all_parameters['max_iter'] = int(params.get('max_iter', 400))
all_parameters["escape_radius"] = float(params.get('escape_radius',0.0))
all_parameters['top_colors'] = int(params.get('top_colors', 24))
all_parameters['max_grains'] = int(params.get('max_grains', 3))
all_parameters['use_palette'] = bool(params.get('use_palette', True))
palette = params.get('palette', './palettes/palette.png')
palette = download_image(palette)
all_parameters['palette'] = palette
all_parameters['gradient'] = int(params.get('gradient', 16))
all_parameters['lake'] = bool(params.get('lake', True))
lake_palette = params.get('lake_palette', './palettes/lake_palette.png')
lake_palette = download_image(lake_palette)
all_parameters['lake_palette'] = lake_palette
all_parameters['levels'] = int(params.get('levels', 16))
all_parameters['shift_palette'] = int(params.get('shift_palette', 0))
all_parameters['shift_palette_lake'] = int(params.get('shift_palette_lake',0))
if not noserverIsOn:
all_parameters['xmin'] = float(params.get('xmin', -2.5))
all_parameters['xmax'] = float(params.get('xmax', 2.5))
all_parameters['ymin'] = float(params.get('ymin', -2.5))
all_parameters['ymax'] = float(params.get('ymax', 2.5))
all_parameters["z_initial"]= [float(params.get('z_initial_r', 0.0)),
float(params.get('z_initial_i', 0.0)),
float(params.get('z_initial_j', 0.0)),
float(params.get('z_initial_k', 0.0)),
float(params.get('z_initial_l', 0.0)),
float(params.get('z_initial_m', 0.0)),
float(params.get('z_initial_n', 0.0)),
float(params.get('z_initial_o', 0.0))]
all_parameters['juliaset_c'] = [float(params.get('juliaset_c_r', -0.8)),
float(params.get('juliaset_c_i', 0.16)),
float(params.get('juliaset_c_j', 0.0)),
float(params.get('juliaset_c_k', 0.0)),
float(params.get('juliaset_c_l', 0.0)),
float(params.get('juliaset_c_m', 0.0)),
float(params.get('juliaset_c_n', 0.0)),
float(params.get('juliaset_c_o', 0.0))]
else:
grid_length = int(params.get('grid_length', 3))
column_aim = min(int(params.get('column_aim', 2)), grid_length)
row_aim = min(int(params.get('row_aim', 2)), grid_length)
coordinates = [[column_aim,row_aim,grid_length]]
all_parameters['column_aim'] = column_aim
all_parameters['row_aim'] = row_aim
all_parameters['grid_length'] = grid_length
all_parameters['coordinates'] = coordinates
all_parameters['continue_aim'] = bool(params.get('continue_aim', False))
if all_parameters['continue_aim'] and all_parameters['grid_length'] != 1:
xmin, xmax, ymin, ymax = divide_in_squares(coordinates, all_parameters['xmin'], all_parameters['xmax'], all_parameters['ymin'], all_parameters['ymax'])
all_parameters['xmin'], all_parameters['xmax'], all_parameters['ymin'], all_parameters['ymax'] = xmin, xmax, ymin, ymax
else:
all_parameters['xmin'] = float(params.get('xmin', -2.5))
all_parameters['xmax'] = float(params.get('xmax', 2.5))
all_parameters['ymin'] = float(params.get('ymin', -2.5))
all_parameters['ymax'] = float(params.get('ymax', 2.5))
zinitial = params.get('z_initial', [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
juliasetp = params.get('juliaset_c', [-0.8, 0.16, 0.0, 0.6, 0.0, 0.0, 0.0, 0.0])
all_parameters["z_initial"]= [float(zinitial[0]),
float(zinitial[1]),
float(zinitial[2]),
float(zinitial[3]),
float(zinitial[4]),
float(zinitial[5]),
float(zinitial[6]),
float(zinitial[7])]
all_parameters["juliaset_c"]= [float(juliasetp[0]),
float(juliasetp[1]),
float(juliasetp[2]),
float(juliasetp[3]),
float(juliasetp[4]),
float(juliasetp[5]),
float(juliasetp[6]),
float(juliasetp[7])]
print("\nYour coordinates: ", all_parameters['xmin'], all_parameters['xmax'], all_parameters['ymin'], all_parameters['ymax'], "\n")
print(re.sub(r"[^\x00-\x7F]+", "", all_parameters['expression'].replace(" ", ""))[:511])
all_parameters["newton_epsilon"]= float(params.get('newton_epsilon', 0.000001))
all_parameters["sigma"]= float(params.get('sigma', 10.0))
all_parameters["rho"]= float(params.get('rho', 28.0))
all_parameters["beta"]= float(params.get('beta', 2.66666666))
all_parameters["dt"]= float(params.get('dt', 0.01))
all_parameters["rotation_angle"] = float(params.get('rotation_angle', 0.0))
all_parameters["axis"] = int(params.get('axis', -1))
all_parameters["max_point_size"] = int(params.get('max_point_size', 1))
all_parameters["n_points"] = int(params.get('n_points',3))
all_parameters["array_size"] = int(params.get('array_size',1))
fractalize_image = (params.get('fractalize_image', False))
fractalize_image = download_image(fractalize_image) if fractalize_image else False
all_parameters['fractalize_image'] = fractalize_image
all_parameters['fast_mode'] = bool(params.get('fast_mode', True))
#zoom = False
#max_zoom = 20
if all_parameters['use_palette'] and not os.path.exists(palette):
print(f"Palette file does not exist: {palette}")
return failed_img
if all_parameters['lake'] and not os.path.exists(lake_palette):
print(f"Lake palette file does not exist: {lake_palette}")
return failed_img
json_data = json.dumps(all_parameters)
process = subprocess.Popen(
[python_exe, "runner.py", "-json_data", json_data],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
start_time = time.time()
while True:
if process.poll() is not None:
print("Took: ",time.time() - start_time,"seconds")
break
if timeout > 0 and (time.time() - start_time) > timeout:
process.kill()
print("Timeout")
return failed_img
elif stop_gen_event.is_set():
process.kill()
print("Generation stopped")
return failed_img
time.sleep(0.2)
stdout, stderr = process.communicate()
print(stdout)
if stderr:
print(f"Error: {stderr}")
if stdout:
try:
return json.loads(stdout.strip())
except Exception as e:
print(f"An Error Occurred: {e}")
return failed_img
return failed_img
def server(port, timeout):
global stop_gen_event
PORT = port
DIRECTORY = ""
class MyHttpRequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
global current_client_id
content_type = self.headers.get('Content-Type')
if content_type != 'application/json; charset=utf-8':
self.send_response(400)
self.end_headers()
return
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
try:
received_params = json.loads(body.decode('utf-8'))
except json.JSONDecodeError:
self.send_response(400)
self.end_headers()
return
if self.path == '/stop':
tab_id = str(received_params.get('tabId', False))
if received_params.get('action', False) == 'stop' and tab_id == current_client_id:
stop_gen_event.set()
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(json.dumps({'message': 'Stopping process...'}).encode('utf-8'))
else:
self.send_response(400)
self.end_headers()
return
# Fractal generation request
# required_keys = ['width', 'height', 'max_iter', 'top_colors', 'max_grains', 'juliaset_c_real', 'juliaset_c_imag', 'xmin', 'xmax', 'ymin', 'ymax', 'palette', 'lake_palette']
if not generating.is_set() :
generating.set()
stop_gen_event.clear()
set_current_gen(str(received_params.get('tab_id', 0)))
try:
fractal_result = ",".join(process_form_data(received_params, timeout))
except Exception as e:
print(f"An Error Occurred: {e}")
fractal_result = ",".join(failed_img)
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(fractal_result.encode('utf-8'))
generating.clear()
elif generating.is_set():
print("On queue...")
while generating.is_set():
time.sleep(0.25)
# if all(key in received_params for key in required_keys):
generating.set()
stop_gen_event.clear()
set_current_gen(str(received_params.get('tab_id', 0)))
try:
fractal_result = ",".join(process_form_data(received_params, timeout))
except Exception as e:
print(f"An Error Occurred: {e}")
fractal_result = ",".join(failed_img)
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(fractal_result.encode('utf-8'))
generating.clear()
# return
else:
self.send_response(400)
self.end_headers()
def translate_path(self, path):
return SimpleHTTPRequestHandler.translate_path(self, f'/{DIRECTORY}{path}')
class ThreadingHTTPServer(ThreadingMixIn, TCPServer):
pass
def signal_handler(signum, frame):
print(f"Signal {signum} received. Shutting down.")
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)
Handler = MyHttpRequestHandler
with ThreadingHTTPServer(("", PORT), Handler) as httpd:
print(f"\nAnother Fractal Generator running at: http://localhost:{PORT}\n")
httpd.serve_forever()
def main():
global all_parameters
parser = argparse.ArgumentParser(description='Process some arguments.')
# Add the --noserver flag
parser.add_argument(
'-noserver',
action='store_true',
help='If specified, the server will not be started.'
)
parser.add_argument(
'-port',
type=int,
default=8888,
help='Port to serve. Defaults to 8888 if not specified.'
)
parser.add_argument(
'-e',
type=str,
help='Expression to use "z^2+c".'
)
parser.add_argument(
'-d',
action='store_true',
help='Don\'t save expressions on last_expressions.txt'
)
parser.add_argument(
'-timeout',
type=int,
default=0,
help='Time limit in seconds.'
)
args = parser.parse_args()
all_parameters["save_expressions"] = not args.d
result = subprocess.run(
[python_exe, "runner.py", "-returndict"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout = result.stdout
stderr = result.stderr
if stderr:
print(f"Error: {stderr}")
parameters = json.loads(stdout)
all_parameters = parameters
if args.noserver:
if args.e:
parameters['expression'] = str(args.e)
# Let's Run
process_form_data(parameters, args.timeout, True)
# for i in range(36):
# all_parameters['expression'] = f"rotation(z*z+c,pi/{36-(i+1)}, 1k)" #(1/35)*i
# time.sleep(1)
# n_coordinates is how many times it will use the array coordinates.
#if all_parameters['video_out']:
#imgs_to_video(all_parameters)
else:
server(args.port, args.timeout)
if __name__ == "__main__":
main()