def parseCommand(self, code): # Check Input if not code: return ServerResponse() # Check syntax try: self.logger.debug("parseCommand: Reading command...") s = ServerRequest.decode(code) if s.cmd not in self.fcns.keys(): raise Exception("An unknown command code ({}) was passed.".format(s.cmd)) except Exception as e: self.logger.exception("parseCommand: An exception was raised.") return ServerResponse(err=True, errmsg=str(e)) self.logger.debug("parseCommands: Parsing complete.") # Execute commands try: self.logger.debug("parseCommand: Recieved code {}; executing {}".format(s.cmd, self.fcns[s.cmd])) v = self.fcns[s.cmd](*s.args, **s.kwargs) self.logger.debug("parseCommands: Done.") return ServerResponse(val=v) except Exception as e: self.logger.exception("parseCommand: Unable to execute command.") return ServerResponse(err=True, errmsg=str(e))# Return excecution error
def getresp(self, request): assert type(request) == ServerRequest resp = ServerResponse.decode(self.send(request.encode())) if not resp.err: return resp.val else: raise Exception(resp.errmsg)
def getresp(self, request): assert type(request) == ServerRequest resp = ServerResponse.decode( self.send(request.encode().encode("utf-8"))) if not resp.err: return resp.val else: raise ServerError(resp.errmsg)
def printer_getall(self): msg = ServerRequest(cmd=self.com.printer.getall) resp = ServerResponse.decode(self.send(msg.encode())) if not resp.err: assert type(resp.val) == list return natsorted(resp.val) else: raise Exception(resp.errmsg)
def rate_getall(self): msg = ServerRequest(cmd=self.com.rate.getall) resp = ServerResponse.decode(self.send(msg.encode())) if not resp.err: assert type(resp.val) == list return natsorted([PrintRate.loadjson(r) for r in resp.val], key=lambda x: x.rateid) else: raise Exception(resp.errmsg)
def printer_delete(self, printer, auth_userid, auth_passwd): msg = ServerRequest(cmd=self.com.printer.delete) msg.args["printer"] = printer msg.args["auth_userid"] = auth_userid msg.args["auth_passwd"] = auth_passwd resp = ServerResponse.decode(self.send(msg.encode())) if resp.err: raise Exception(resp.errmsg)
def getUsers(self): msg = ServerRequest(cmd=self.com.user.getall) c_logger.debug("getUsers: Sending request to server...") resp = ServerResponse.decode(self.send(msg.encode())) if not resp.err: return resp.val else: raise Exception(resp.errmsg)
def rate_add(self, rateid, lengthrate, timerate, auth_userid, auth_passwd): msg = ServerRequest(cmd=self.com.rate.add) msg.args["rateid"] = rateid msg.args["lengthrate"] = lengthrate msg.args["timerate"] = timerate msg.args["auth_userid"] = auth_userid msg.args["auth_passwd"] = auth_passwd resp = ServerResponse.decode(self.send(msg.encode())) if resp.err: raise Exception(resp.errmsg)
def user_issuper(self, userid, passwd): c_logger.debug("issuper: Checking if '{}' is an elevated user.".format(userid)) msg = ServerRequest(cmd=self.com.user.issuper) msg.args["userid"] = userid msg.args["passwd"] = passwd c_logger.debug("issuper: Sending request to server...") resp = ServerResponse.decode(self.send(msg.encode())) if not resp.err: c_logger.debug("issuper: Done.") return resp.val else: raise Exception(resp.errmsg)
def verifyUser(self, userid, passwd): c_logger.info("verifyUser: Verifying user credentials...") msg = ServerRequest(cmd=self.com.user.verify) msg.args["userid"] = userid msg.args["passwd"] = passwd c_logger.debug("verifyUser: Sending request to server...") resp = self.send(msg.encode()) resp = ServerResponse.decode(resp) if not resp.err: return resp.val else: raise Exception(resp.errmsg)
def print_add(self, userid, passwd, length, duration, printer, rate, paidby="", note=""): msg = ServerRequest(cmd=self.com.print.add) msg.args["userid"] = userid msg.args["passwd"] = passwd msg.args["length"] = length msg.args["duration"] = duration msg.args["printer"] = printer msg.args["rate"] = rate if paidby: msg.args["paidby"] = paidby if note: msg.args["note"] = note resp = ServerResponse.decode(self.send(msg.encode())) if not resp.err: return resp.val else: raise Exception(resp.errmsg)