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