def handle_post_thread(self, data, path): self.send_response(HTTPStatus.OK) self.send_header("Content-type", "application/JSON") self.end_headers() if "code[]" not in data: data["code[]"] = [""] if path == "/cancel": self.cancellation_event.set() if path == "/process2": self.cancellation_event.clear( ) # Make sure we don't have lingering cancellation requests from before code = data["code[]"] curr_i = int(data["curr_i"][0]) curr_f = int(data["curr_f"][0]) global_frame_id = int(data["globalFrameID"][0]) visualize_tail_calls = data["tailViz"][0] == "true" self.wfile.write( bytes( handle(code, curr_i, curr_f, global_frame_id, visualize_tail_calls, cancellation_event=self.cancellation_event), "utf-8")) elif path == "/save": code = data["code[]"] filename = data["filename"][0] do_save = data["do_save"][0] == "true" if do_save: save(code, filename) self.wfile.write( bytes( json.dumps({ "result": "success", "stripped": strip_comments(code) }), "utf-8")) elif path == "/instant": code = data["code[]"] global_frame_id = int(data["globalFrameID"][0]) self.wfile.write(bytes(instant(code, global_frame_id), "utf-8")) elif path == "/reformat": code = data["code[]"] javastyle = data["javastyle"][0] == "true" self.wfile.write( bytes( json.dumps({ "result": "success", "formatted": prettify(code, javastyle) }), "utf-8")) elif path == "/test": self.cancellation_event.clear( ) # Make sure we don't have lingering cancellation requests from before output = cancelable_subprocess_call( self.cancellation_event, (sys.argv[0], os.path.splitext(ok_interface.__file__)[0] + ".py"), -1, sys.executable, subprocess.PIPE, subprocess.PIPE, None) self.wfile.write(output.split(ok_interface.BEGIN_OUTPUT)[1]) elif path == "/list_files": self.wfile.write(bytes(json.dumps(get_scm_files()), "utf-8")) elif path == "/read_file": filename = data["filename"][0] self.wfile.write(bytes(json.dumps(read_file(filename)), "utf-8")) elif path == "/new_file": filename = data["filename"][0] self.wfile.write( bytes(json.dumps({"success": new_file(filename)}), "utf-8")) elif path == "/save_state": global state for key, val in json.loads(data["state"][0]).items(): if key == "states": if "states" not in state: state["states"] = val else: merge(state["states"], val) else: state[key] = val if "settings" in state: save_config("settings", state["settings"]) elif path == "/load_state": if "states" not in state: self.wfile.write(b"fail") else: self.wfile.write(bytes(json.dumps(state), "utf-8")) elif path == "/load_settings": try: if "settings" not in state: state["settings"] = {} for key, val in load_config("settings").items(): state["settings"][key] = val except FileNotFoundError: self.wfile.write(b"fail") else: self.wfile.write(bytes(json.dumps(state["settings"]), "utf-8")) elif path == "/documentation": query = data.get("query", [""])[0] self.wfile.write(bytes(json.dumps(search(query)), "utf-8")) elif path == "/kill": # This is (only) fine because we're in a different thread than the actual server self.server.shutdown() self.server.socket.close()
import os from threading import Thread import cv2 import sys import persistence from screeninfo import get_monitors from smooth_transition_state import SmoothTransitionState from touch_input_handler import TouchInputHandler from pan_zoom_state import PanZoomState config = persistence.load_config() runtime_settings = persistence.load_runtime_settings() KEY_CODE_BACKSPACE = 8 KEY_CODE_ENTER = 13 KEY_CODE_ESCAPE = 27 KEY_CODE_SPACE = 32 KEY_CODE_ARROW_LEFT = 65361 KEY_CODE_ARROW_UP = 65362 KEY_CODE_ARROW_RIGHT = 65363 KEY_CODE_ARROW_DOWN = 65364 DELTA = 100 video_device = "/dev/video0" if len(sys.argv) > 1: device_arg = sys.argv[1] kernel_name = "/dev/" + device_arg device_id_symlink = "/dev/v4l/by-id/" + device_arg if os.path.exists(kernel_name):
def handle_post_thread(self, data, path): if (b'code[]' not in data): data[b'code[]'] = [b''] if (path == '/cancel'): self.cancellation_event.set() self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() if (path == '/process2'): self.cancellation_event.clear() code = [x.decode('utf-8') for x in data[b'code[]']] curr_i = int(data[b'curr_i'][0]) curr_f = int(data[b'curr_f'][0]) global_frame_id = int(data[b'globalFrameID'][0]) self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write( bytes( handle(code, curr_i, curr_f, global_frame_id, cancellation_event=self.cancellation_event), 'utf-8')) elif (path == '/save'): code = [x.decode('utf-8') for x in data[b'code[]']] filename = data[b'filename'][0] do_save = (data[b'do_save'][0] == b'true') if do_save: save(code, filename) self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write( bytes( json.dumps({ 'result': 'success', 'stripped': strip_comments(code), }), 'utf-8')) elif (path == '/instant'): code = [x.decode('utf-8') for x in data[b'code[]']] global_frame_id = int(data[b'globalFrameID'][0]) self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write(bytes(instant(code, global_frame_id), 'utf-8')) elif (path == '/reformat'): code = [x.decode('utf-8') for x in data[b'code[]']] javastyle = (data[b'javastyle'][0] == b'true') self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write( bytes( json.dumps({ 'result': 'success', 'formatted': prettify(code, javastyle), }), 'utf-8')) elif (path == '/test'): self.cancellation_event.clear() output = cancelable_subprocess_call( self.cancellation_event, (sys.argv[0], (os.path.splitext(ok_interface.__file__)[0] + '.py')), (-1), sys.executable, subprocess.PIPE, subprocess.PIPE, None) self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write(output) elif (path == '/list_files'): self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write(bytes(json.dumps(get_scm_files()), 'utf-8')) elif (path == '/read_file'): filename = data[b'filename'][0] self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write(bytes(json.dumps(read_file(filename)), 'utf-8')) elif (path == '/new_file'): filename = data[b'filename'][0].decode('utf-8') self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() self.wfile.write( bytes(json.dumps({ 'success': new_file(filename), }), 'utf-8')) elif (path == '/save_state'): global state for (key, val) in json.loads(data[b'state'][0].decode('utf-8')).items(): if (key == 'states'): if ('states' not in state): state['states'] = val else: merge(state['states'], val) else: state[key] = val if ('settings' in state): save_config('settings', state['settings']) self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() elif (path == '/load_state'): self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() if ('states' not in state): self.wfile.write(b'fail') else: self.wfile.write(bytes(json.dumps(state), 'utf-8')) elif (path == '/load_settings'): self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() try: if ('settings' not in state): state['settings'] = {} for (key, val) in load_config('settings').items(): state['settings'][key] = val except FileNotFoundError: self.wfile.write(b'fail') else: self.wfile.write(bytes(json.dumps(state['settings']), 'utf-8')) elif (path == '/documentation'): self.send_response(HTTPStatus.OK, 'test') self.send_header('Content-type', 'application/JSON') self.end_headers() query = data.get(b'query', [b''])[0].decode('utf-8') self.wfile.write(bytes(json.dumps(search(query)), 'utf-8')) elif (path == '/kill'): self.server.shutdown() self.server.socket.close()