2016-12-29 19:50:42 +00:00
|
|
|
import cairosvg
|
2016-07-17 17:12:13 +00:00
|
|
|
import cgi
|
2016-12-27 22:00:13 +00:00
|
|
|
import os
|
2016-07-17 17:12:13 +00:00
|
|
|
import sys
|
2016-12-29 19:50:42 +00:00
|
|
|
import time
|
2016-07-17 17:12:13 +00:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
from modules.pcgenerator import PCGenerator
|
2017-03-04 13:47:23 +00:00
|
|
|
from modules.pcgenerator import calibrate
|
2016-07-17 17:12:13 +00:00
|
|
|
|
|
|
|
|
def pcgenerator_get(handler):
|
|
|
|
|
|
2016-12-27 22:00:13 +00:00
|
|
|
f = open("{}/../templates/{}".format(
|
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
2016-07-17 17:12:13 +00:00
|
|
|
"pcgenerator.html"))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
handler.send_response(200)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(f.read())
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
2016-12-27 22:00:13 +00:00
|
|
|
handler.log_error("%s", traceback.format_exception(exc_type, exc_value,exc_traceback))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.wfile.write(
|
|
|
|
|
"<h1>Aw, snap! We seem to have a problem.</h1><p><b>")
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
repr(traceback.format_exception(exc_type, exc_value,exc_traceback)))
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
"</b><p>Please report this error via private message to "
|
|
|
|
|
"<a href='http://www.ravelry.com/people/beebell'>beebell on Ravelry</a>. "
|
|
|
|
|
"It will be helpful if you include the pattern you uploaded to help me "
|
|
|
|
|
"diagnose the issue.")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
def pcgenerator_post(handler):
|
|
|
|
|
|
|
|
|
|
try:
|
2016-12-29 19:50:42 +00:00
|
|
|
ctype, pdict = cgi.parse_header(handler.headers.getheader('Content-Type'))
|
2016-07-17 17:12:13 +00:00
|
|
|
if ctype == 'multipart/form-data':
|
|
|
|
|
query=cgi.parse_multipart(handler.rfile, pdict)
|
|
|
|
|
|
2017-03-04 15:15:43 +00:00
|
|
|
calibrate_only = query.get('test', [''])[0] == 'test'
|
|
|
|
|
if calibrate_only:
|
2017-03-04 13:47:23 +00:00
|
|
|
result = calibrate()
|
|
|
|
|
filename_template = 'attachment; filename="calibrate-{}.{}"'
|
2016-07-17 17:12:13 +00:00
|
|
|
else:
|
2017-03-04 13:47:23 +00:00
|
|
|
upfilecontent = query.get('upfile')
|
|
|
|
|
if len(upfilecontent[0]) > 4000:
|
|
|
|
|
handler.send_response(302)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write("Sorry. Your file cannot exceed 2500 bytes!")
|
|
|
|
|
else:
|
|
|
|
|
machine_type = query.get('machine')
|
|
|
|
|
vert_repeat = query.get('vert')
|
|
|
|
|
convert_to_png = query.get('png', [''])[0] == 'png'
|
|
|
|
|
|
|
|
|
|
generator = PCGenerator(
|
|
|
|
|
handler,
|
|
|
|
|
upfilecontent[0],
|
|
|
|
|
machine_type[0],
|
|
|
|
|
int(vert_repeat[0]))
|
|
|
|
|
result = generator.generate()
|
|
|
|
|
filename_template = 'attachment; filename="punchcard-{}.{}"'
|
2016-07-17 17:12:13 +00:00
|
|
|
|
|
|
|
|
handler.send_response(200)
|
2016-12-29 19:50:42 +00:00
|
|
|
|
|
|
|
|
if convert_to_png:
|
|
|
|
|
result = cairosvg.svg2png(bytestring=result)
|
|
|
|
|
handler.send_header('Content-type', 'image/png')
|
|
|
|
|
handler.send_header('Content-Disposition', filename_template.format(int(time.time()), "png"))
|
|
|
|
|
else:
|
|
|
|
|
handler.send_header('Content-type', 'image/svg+xml')
|
|
|
|
|
handler.send_header('Content-Disposition', filename_template.format(int(time.time()), "svg"))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(result)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
except ValueError as e:
|
2016-12-27 22:00:13 +00:00
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
|
|
|
handler.log_error("%s", traceback.format_exception(exc_type, exc_value,exc_traceback))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.send_response(302)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(
|
2016-12-27 22:00:13 +00:00
|
|
|
"<h1>Aw, snap! We seem to have a problem.</h1><p><b>")
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
repr(traceback.format_exception(exc_type, exc_value,exc_traceback)))
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.wfile.write(e)
|
2016-12-27 22:00:13 +00:00
|
|
|
handler.wfile.write(
|
|
|
|
|
"</b><p>Please report this error via private message to "
|
|
|
|
|
"<a href='http://www.ravelry.com/people/beebell'>beebell on Ravelry</a>. "
|
|
|
|
|
"It will be helpful if you include the pattern you uploaded to help me "
|
|
|
|
|
"diagnose the issue.")
|
2016-07-17 17:12:13 +00:00
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
2016-12-27 22:00:13 +00:00
|
|
|
handler.log_error("%s", traceback.format_exception(exc_type, exc_value,exc_traceback))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.send_response(302)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
"<h1>Aw, snap! We seem to have a problem.</h1><p><b>")
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
repr(traceback.format_exception(exc_type, exc_value,exc_traceback)))
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
"</b><p>Please report this error via private message to "
|
|
|
|
|
"<a href='http://www.ravelry.com/people/beebell'>beebell on Ravelry</a>. "
|
|
|
|
|
"It will be helpful if you include the pattern you uploaded to help me "
|
|
|
|
|
"diagnose the issue.")
|
|
|
|
|
|
|
|
|
|
def calculator_get(handler):
|
|
|
|
|
|
2016-12-27 22:00:13 +00:00
|
|
|
f = open("{}/../templates/{}".format(
|
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
2016-07-17 17:12:13 +00:00
|
|
|
"calculator.html"))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
handler.send_response(200)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(f.read())
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
2016-12-27 22:00:13 +00:00
|
|
|
handler.log_error("%s", traceback.format_exception(exc_type, exc_value,exc_traceback))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.wfile.write(
|
|
|
|
|
"<h1>Aw, snap! We seem to have a problem.</h1><p><b>")
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
repr(traceback.format_exception(exc_type, exc_value,exc_traceback)))
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
"</b><p>Please report this error via private message to "
|
|
|
|
|
"<a href='http://www.ravelry.com/people/beebell'>beebell on Ravelry</a>. "
|
|
|
|
|
"It will be helpful if you include the pattern you uploaded to help me "
|
|
|
|
|
"diagnose the issue.")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
def index_get(handler):
|
2016-12-27 22:00:13 +00:00
|
|
|
f = open("{}/../templates/{}".format(
|
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
|
"index.html"))
|
2016-07-17 17:12:13 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
handler.send_response(200)
|
|
|
|
|
handler.send_header('Content-type', 'text/html')
|
|
|
|
|
handler.end_headers()
|
|
|
|
|
handler.wfile.write(f.read())
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
2016-12-27 22:00:13 +00:00
|
|
|
handler.log_error("%s", traceback.format_exception(exc_type, exc_value,exc_traceback))
|
|
|
|
|
|
2016-07-17 17:12:13 +00:00
|
|
|
handler.wfile.write(
|
|
|
|
|
"<h1>Aw, snap! We seem to have a problem.</h1><p><b>")
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
repr(traceback.format_exception(exc_type, exc_value,exc_traceback)))
|
|
|
|
|
handler.wfile.write(
|
|
|
|
|
"</b><p>Please report this error via private message to "
|
|
|
|
|
"<a href='http://www.ravelry.com/people/beebell'>beebell on Ravelry</a>. "
|
|
|
|
|
"It will be helpful if you include the pattern you uploaded to help me "
|
|
|
|
|
"diagnose the issue.")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
f.close()
|