reduce code, same code exeucted on 3 function grouped on new function

This commit is contained in:
AleaJactaEst 2018-02-13 20:42:03 +01:00
parent ac8b1b30cd
commit 342766c2b5

View file

@ -172,7 +172,7 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
self.send_header('Content-type', 'application/json') self.send_header('Content-type', 'application/json')
self.end_headers() self.end_headers()
def _command_log(self): def _extract_input_data(self):
""" sub request log (send log on specific process) """ """ sub request log (send log on specific process) """
if 'content-type' in self.headers: if 'content-type' in self.headers:
ctype = self.headers['content-type'] ctype = self.headers['content-type']
@ -180,19 +180,25 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
ctype = 'text' ctype = 'text'
if ctype != 'application/json': if ctype != 'application/json':
logging.error("Received request with bad content-type") logging.error("Received request with bad content-type")
self.send_response(400, "bad content-type") self.send_error(400, "bad content-type")
self.end_headers() self.end_headers()
return return None
try: try:
sizemsg = int(self.headers['content-length']) sizemsg = int(self.headers['content-length'])
except (TypeError, KeyError, ValueError): except (TypeError, KeyError, ValueError):
logging.error("Received request with bad content-length") logging.error("Received request with bad content-length")
self.send_response(400, "bad content-length") self.send_error(400, "bad content-length")
self.end_headers() self.end_headers()
return return None
msg = self.rfile.read(sizemsg) msg = self.rfile.read(sizemsg)
msgjson = json.loads(msg.decode()) msgjson = json.loads(msg.decode())
return msgjson
def _command_log(self):
""" sub request log (send log on specific process) """
msgjson = self._extract_input_data()
if msgjson == None:
return
logging.debug(msgjson) logging.debug(msgjson)
if 'name' not in msgjson: if 'name' not in msgjson:
@ -269,25 +275,9 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
def _send_action(self): def _send_action(self):
""" send specific action on one program """ """ send specific action on one program """
if 'content-type' in self.headers: msgjson = self._extract_input_data()
ctype = self.headers['content-type'] if msgjson == None:
else:
ctype = 'text'
if ctype != 'application/json':
logging.error("Bad content-type")
self.send_response(400, "bad content-type")
self.end_headers()
return return
try:
sizemsg = int(self.headers['content-length'])
except (TypeError, KeyError, ValueError):
logging.error("Bad content-length")
self.send_response(400, "bad content-length")
self.end_headers()
return
msg = self.rfile.read(sizemsg)
msgjson = json.loads(msg.decode())
logging.debug(msgjson) logging.debug(msgjson)
if 'name' not in msgjson: if 'name' not in msgjson:
@ -305,16 +295,10 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
logging.error("Missing param action '%s'" % name) logging.error("Missing param action '%s'" % name)
return return
action = '' action = msgjson['action']
try:
action = msgjson['action']
except KeyError:
logging.error("Impossible to read first-line '%s'" % msgjson['action'])
self.send_error(400, 'Impossible to read action')
return
logging.debug("%s:%s" % (name, action)) logging.debug("%s:%s" % (name, action))
self.server.listEvent[name].set() self.server.listEvent[name].set()
self.server.listQueueIn[name].put("ACTION %s" % action) self.server.listQueueIn[name].put("STDIN %s" % action)
logging.debug("message envoye: %s" % (name)) logging.debug("message envoye: %s" % (name))
try: try:
@ -331,24 +315,10 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
:param str command: command (START, STOP, STATUS, ... ) :param str command: command (START, STOP, STATUS, ... )
""" """
if 'content-type' in self.headers: msgjson = self._extract_input_data()
ctype = self.headers['content-type'] if msgjson == None:
else:
ctype = 'text'
if ctype != 'application/json':
logging.error("Bad content-type")
self.send_response(400, "Bad content-type")
self.end_headers()
return return
try:
sizemsg = int(self.headers['content-length'])
except (TypeError, KeyError, ValueError):
logging.error("Bad content-length")
self.send_response(400, "Bad content-length")
self.end_headers()
return
msg = self.rfile.read(sizemsg)
msgjson = json.loads(msg.decode())
if 'name' not in msgjson: if 'name' not in msgjson:
self.send_error(400, 'Missing param name') self.send_error(400, 'Missing param name')
logging.error("Missing param name") logging.error("Missing param name")
@ -743,7 +713,7 @@ class ManageCommand():
msg = self.queueIn.get(timeout=4) msg = self.queueIn.get(timeout=4)
except queue.Empty: except queue.Empty:
self.event.clear() self.event.clear()
logging.debug("pas de message recu pour %s" % self.name) logging.debug("[%s] Queue empty (no message)" % self.name)
return return
logging.debug("command : '%s'" % msg) logging.debug("command : '%s'" % msg)
command = msg.split()[0] command = msg.split()[0]
@ -763,9 +733,11 @@ class ManageCommand():
try: try:
firstline = int(msg.split(maxsplit=1)[1]) firstline = int(msg.split(maxsplit=1)[1])
except ValueError: except ValueError:
logging.warning("Bad value for param first-line (need integer)")
firstline = 0 firstline = 0
self.queueOut.put(self.getlog(firstline)) self.queueOut.put(self.getlog(firstline))
else: else:
logging.warning("Bad command (%s)" % command)
self.queueOut.put("error : command unknown") self.queueOut.put("error : command unknown")
self.event.clear() self.event.clear()
self.stop() self.stop()
@ -931,6 +903,7 @@ class Manager():
queueOut, queueOut,
event)) event))
threadCommand.start() threadCommand.start()
self.threadCommand.append(threadCommand)
if self.launch_program: if self.launch_program:
event.set() event.set()
queueIn.put("START") queueIn.put("START")
@ -938,10 +911,9 @@ class Manager():
item = queueOut.get(timeout=4) item = queueOut.get(timeout=4)
except queue.Empty: except queue.Empty:
item = "" item = ""
logging.debug("pas de message recu pour %s" % name) logging.debug("[%s] Queue empty (no message)" % name)
return return
logging.info("%s => %s" % (name, item)) logging.info("%s => %s" % (name, item))
self.threadCommand.append(threadCommand)
def receive_signal(self, signum, frame): def receive_signal(self, signum, frame):
""" Managed signal """ """ Managed signal """