def reformat_files(src, dest=None): if dest is None: dest = src with open(src) as src: formatted = prettify([src.read()]) with open(dest, "w") as dest: dest.write(formatted + "\n") exit()
def output(self): result = "" if self.setup_out.success(): result += self.setup_out.prompt else: result += self.setup_out.representation() result += "\n\n" result += "\n\n".join(x.representation() for x in self.cases_out) return formatter.prettify([result]) + "\n"
def reformat_files(src, dest=None): with open(src) as src: formatted = prettify([src.read()]) if dest: with open(dest, "w") as dest: dest.write(formatted + "\n") else: print(formatted) exit()
def reformat_files(src, dest=None, check=False): if dest is None: dest = src with open(src) as src: original = src.read() formatted = prettify([original]) + "\n" if check: if original != formatted: print("\n".join( unified_diff(original.splitlines(), formatted.splitlines(), fromfile="Original", tofile="Formatted"))) exit(1) with open(dest, "w") as dest: dest.write(formatted) exit()
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()
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()