def render_PUT(self, request): """ Handler for PUT requests of environment """ @defer.inlineCallbacks def extract_args(result): print("in extract args", result) name = result["name"] or "" description = result.get("description") or "" status = result.get("status") or "live" id = self.envId defer.returnValue((yield self.environmentManager.update_environment( id=id, name=name, description=description, status=status))) r = ResponseGenerator( request, status=200, contentType="application/pollapli.environment+json", resource="environment", rootUri=self.rootUri) d = RequestParser(request, "environment", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args, errback=r._build_response) d.addBoth(r._build_response) d.callback(None) return NOT_DONE_YET
def render_POST(self, request): """ Handler for POST requests of task status """ @defer.inlineCallbacks def extract_args(result): start = result.get("start") pause = result.get("pause") stop = result.get("stop") print("task status: start", start, "pause", pause, "stop", stop) if start: defer.returnValue( (yield self.environmentManager.get_environment( self.envId).get_task(self.taskId).start())) elif pause: defer.returnValue( (yield self.environmentManager.get_environment( self.envId).get_task(self.taskId).pause())) elif stop: defer.returnValue( (yield self.environmentManager.get_environment( self.envId).get_task(self.taskId).stop())) r = ResponseGenerator( request, status=201, contentType="application/pollapli.task.status+json", resource="taskstatus", rootUri=self.rootUri) d = RequestParser(request, "task status", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args, errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self, request): """ Handler for POST requests of environments extract the data from the request body to add a new environment """ @defer.inlineCallbacks def extract_args(result): name = result["name"] or "" description = result.get("description") or "" status = result.get("status") or "live" defer.returnValue((yield self.environmentManager.add_environment( name=name, description=description, status=status))) r = ResponseGenerator( request, status=201, contentType="application/pollapli.environment+json", resource="environment", rootUri=self.rootUri) d = RequestParser(request, "environment", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args, errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of files """ def save_file(result): def find_fileName(): savedPosition = request.content.tell() try: request.content.seek(0) request.content.readline() match = re.search(r'filename="([^"]+)"', request.content.readline()) if match: return match.group(1) else: return None finally: request.content.seek(savedPosition) fileName=find_fileName() saved_file=open(os.path.join(FileManager.dataPath,"printFiles",fileName),'w') saved_file.write(result.get("datafile")[0]) saved_file.close() print("POST REQUESt",request,"content",str(request.content)) r=ResponseGenerator(request,status=201,contentType="application/pollapli.fileList+json",resource="files",rootUri=self.rootUri) d=RequestParser(request,"files",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=save_file,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests of tasks """ r=ResponseGenerator(request,status=200,contentType="application/pollapli.taskList+json",resource="tasks",rootUri=self.rootUri) d=RequestParser(request,"task",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(self.environmentManager.get_environment(self.envId).get_tasks,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests for a single update """ r=ResponseGenerator(request,status=200,contentType="application/pollapli.update.Status+json",resource="update status",rootUri=self.rootUri) d=RequestParser(request,"update status",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(lambda params: UpdateManager.get_update(id=self.updateId),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests of driver types """ r=ResponseGenerator(request,status=200,contentType="application/pollapli.driverTypeList+json",resource="driverTypes",rootUri=self.rootUri) d=RequestParser(request,"driverTypes",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params:DriverManager.get_driver_types(params),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of tasks extract the data from the request body to add a new task """ r=ResponseGenerator(request,status=201,contentType="application/pollapli.task+json",resource="task",rootUri=self.rootUri) d=RequestParser(request,"task",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params:self.environmentManager.get_environment(self.envId).add_task(**params),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_DELETE(self,request): """ Handler for DELETE requests of file WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes the file """ r=ResponseGenerator(request,status=200) d=RequestParser(request,"files",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params:FileManager.delete_file(self.id),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_PUT(self,request): """ Handler for PUT requests of node """ r=ResponseGenerator(request,status=200,contentType="application/pollapli.node+json",resource="node",rootUri=self.rootUri) d=RequestParser(request,"node",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() #d.addCallbacks(callback=lambda params:self.environmentManager.get_environment(self.envId).update_node(id=self.nodeId,**params),errback=r._build_response) d.addCallbacks(callback=lambda params:self.environmentManager.get_environment(self.envId).update_node(**params),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests of driver status """ def extract_args(result): return(self.environmentManager.get_environment(self.envId).get_node(self.nodeId).get_driver()) r=ResponseGenerator(request,contentType="application/pollapli.driver.status+json",resource="driver status",rootUri=self.rootUri) d=RequestParser(request,"driver status",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_DELETE(self,request): """ Handler for DELETE requests for the current node WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes the current node completely """ r=ResponseGenerator(request,status=200,rootUri=self.rootUri) d=RequestParser(request,"node",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(lambda params:self.environmentManager.get_environment(self.envId).delete_node(self.nodeId),errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_DELETE(self,request): """ Handler for DELETE requests of nodes WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes ALL nodes """ print("NODE CLEARING") r=ResponseGenerator(request,contentType="application/pollapli.nodeList+json",status=200,rootUri=self.rootUri) d=RequestParser(request,"node",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params:self.environmentManager.get_environment(self.envId).clear_nodes() ,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_DELETE(self, request): """ Handler for DELETE requests of files WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes ALL files """ r = ResponseGenerator(request, status=200) d = RequestParser(request, "files", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params: FileManager.delete_files(), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_DELETE(self, request): """ Handler for DELETE requests for the current node WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes the current node completely """ r = ResponseGenerator(request, status=200, rootUri=self.rootUri) d = RequestParser(request, "node", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(lambda params: self.environmentManager.get_environment( self.envId).delete_node(self.nodeId), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of driver """ @defer.inlineCallbacks def extract_args(result): # print("in extract args",result) defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_node(self.nodeId).set_driver(**result))) r=ResponseGenerator(request,status=201,contentType="application/pollapli.driver+json",resource="driver",rootUri=self.rootUri) d=RequestParser(request,"driver",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_PUT(self,request): """ Handler for PUT requests for the driver """ @defer.inlineCallbacks def extract_args(result): name=result["name"] or "" description=result.get("description") or "" id=self.connectorId defer.returnValue((yield self.environmentManager.get_environment(self.envId).update_node(id=id,name=name,description=description))) r=ResponseGenerator(request,status=200,contentType="application/pollapli.driver+json",resource="driver",rootUri=self.rootUri) d=RequestParser(request,"driver",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests of node """ r = ResponseGenerator(request, status=200, contentType="application/pollapli.node+json", resource="node", rootUri=self.rootUri) d = RequestParser(request, "node", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(lambda params: self.environmentManager.get_environment( self.envId).get_node(self.nodeId), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of environments extract the data from the request body to add a new environment """ @defer.inlineCallbacks def extract_args(result): name=result["name"] or "" description=result.get("description") or "" status=result.get("status") or "live" defer.returnValue((yield self.environmentManager.add_environment(name=name,description=description,status=status))) r=ResponseGenerator(request,status=201,contentType="application/pollapli.environment+json",resource="environment",rootUri=self.rootUri) d=RequestParser(request,"environment",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of connector status """ @defer.inlineCallbacks def extract_args(result): if result["connected"]: mode=result.get("mode") or 1 defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_node(self.nodeId).connect(mode=mode))) else: defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_node(self.nodeId).disconnect())) r=ResponseGenerator(request,status=200,contentType="application/pollapli.driver.status+json",resource="driver status",rootUri=self.rootUri) d=RequestParser(request,"driver status",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_GET(self, request): """ Handler for GET requests for a single update """ r = ResponseGenerator( request, status=200, contentType="application/pollapli.update.Status+json", resource="update status", rootUri=self.rootUri) d = RequestParser(request, "update status", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks( lambda params: UpdateManager.get_update(id=self.updateId), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self, request): """ Handler for POST requests of tasks extract the data from the request body to add a new task """ r = ResponseGenerator(request, status=201, contentType="application/pollapli.task+json", resource="task", rootUri=self.rootUri) d = RequestParser(request, "task", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params: self.environmentManager. get_environment(self.envId).add_task(**params), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_DELETE(self, request): """ Handler for DELETE requests of nodes WARNING !! needs to be used very carefully, with confirmation on the client side, as it deletes ALL nodes """ print("NODE CLEARING") r = ResponseGenerator(request, contentType="application/pollapli.nodeList+json", status=200, rootUri=self.rootUri) d = RequestParser(request, "node", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=lambda params: self.environmentManager. get_environment(self.envId).clear_nodes(), errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST request for a specific update's status: this allows to start the download/setup process """ def dostuff(result): if result["start"]: name=UpdateManager.get_update(id=self.updateId).name print("update to download",name) UpdateManager.downloadAndInstall_update(name) else: print("uh oh") r=ResponseGenerator(request,status=200,contentType="application/pollapli.update.status+json",resource="update status",rootUri=self.rootUri) d=RequestParser(request,"update status",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=dostuff,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_PUT(self,request): """ Handler for PUT requests of environment """ @defer.inlineCallbacks def extract_args(result): print("in extract args",result) name=result["name"] or "" description=result.get("description") or "" status=result.get("status") or "live" id=self.envId defer.returnValue((yield self.environmentManager.update_environment(id=id,name=name,description=description,status=status))) r=ResponseGenerator(request,status=200,contentType="application/pollapli.environment+json",resource="environment",rootUri=self.rootUri) d=RequestParser(request,"environment",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) d.callback(None) return NOT_DONE_YET
def render_POST(self, request): """ Handler for POST requests of files """ def save_file(result): def find_fileName(): savedPosition = request.content.tell() try: request.content.seek(0) request.content.readline() match = re.search(r'filename="([^"]+)"', request.content.readline()) if match: return match.group(1) else: return None finally: request.content.seek(savedPosition) fileName = find_fileName() saved_file = open( os.path.join(FileManager.dataPath, "printFiles", fileName), 'w') saved_file.write(result.get("datafile")[0]) saved_file.close() print("POST REQUESt", request, "content", str(request.content)) r = ResponseGenerator(request, status=201, contentType="application/pollapli.fileList+json", resource="files", rootUri=self.rootUri) d = RequestParser(request, "files", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=save_file, errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET
def render_POST(self,request): """ Handler for POST requests of task status """ @defer.inlineCallbacks def extract_args(result): start=result.get("start") pause=result.get("pause") stop=result.get("stop") print("task status: start",start,"pause",pause,"stop",stop) if start: defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_task(self.taskId).start())) elif pause: defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_task(self.taskId).pause())) elif stop: defer.returnValue((yield self.environmentManager.get_environment(self.envId).get_task(self.taskId).stop())) r=ResponseGenerator(request,status=201,contentType="application/pollapli.task.status+json",resource="taskstatus",rootUri=self.rootUri) d=RequestParser(request,"task status",self.valid_contentTypes,self.validGetParams).ValidateAndParseParams() d.addCallbacks(extract_args,errback=r._build_response) d.addBoth(r._build_response) request._call=reactor.callLater(0,d.callback,None) return NOT_DONE_YET
def render_POST(self, request): """ Handler for POST request for a specific update's status: this allows to start the download/setup process """ def dostuff(result): if result["start"]: name = UpdateManager.get_update(id=self.updateId).name print("update to download", name) UpdateManager.downloadAndInstall_update(name) else: print("uh oh") r = ResponseGenerator( request, status=200, contentType="application/pollapli.update.status+json", resource="update status", rootUri=self.rootUri) d = RequestParser(request, "update status", self.valid_contentTypes, self.validGetParams).ValidateAndParseParams() d.addCallbacks(callback=dostuff, errback=r._build_response) d.addBoth(r._build_response) request._call = reactor.callLater(0, d.callback, None) return NOT_DONE_YET