def test_parse_shell_pairs(): request = TestRequest() request.POST[constants.NUM_PAIRS] = 2 request.POST["%s2" % (constants.SHELL_ID,)] = "0" request.POST["%s2" % (constants.OFFSET,)] = "0" request.POST["%s1" % (constants.SHELL_ID,)] = "1" request.POST["%s1" % (constants.OFFSET,)] = "1" parsed_pairs = utils.parse_shell_pairs(request) assert parsed_pairs[0][0] == "1" assert parsed_pairs[0][1] == 1 assert parsed_pairs[1][0] == "0" assert parsed_pairs[1][1] == 0 request.POST[constants.NUM_PAIRS] = 1 assert len(utils.parse_shell_pairs(request)) == 1 request.POST[constants.NUM_PAIRS] = "most definitely not a number" try: utils.parse_shell_pairs(request) except ValueError: pass else: assert ( False ), "parse_shell_pairs did not throw an exception when trying to convert a malformed string to integer"
def retrieve_output(request): if not _running_with_spawning(request): result = simplejson.dumps({constants.NOT_RUNNING_SPAWNING: True}) return HttpResponse(result, mimetype="application/json") shell_manager = ShellManager.global_instance() username = request.user.username hue_instance_id = request.META[constants.HUE_INSTANCE_ID] try: shell_pairs = utils.parse_shell_pairs(request) except ValueError: shell_pairs = [] result = shell_manager.retrieve_output(username, hue_instance_id, shell_pairs) for key, value in result.iteritems(): if isinstance(value, dict) and constants.OUTPUT in value: log_format = '%s %s - shell_id:%s - Output: "%s"' log_args = (request.META.get('REMOTE_ADDR'), username, key, repr(value[constants.OUTPUT])) SHELL_OUTPUT_LOGGER.info(log_format % log_args) for shell in result: if 'output' in result[shell]: result[shell]['output'] = remove_colors(result[shell]['output']) return HttpResponse(simplejson.dumps(result), mimetype="application/json")
def add_to_output(request): if not _running_with_spawning(request): result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True }) return HttpResponse(result, mimetype="application/json") shell_manager = ShellManager.global_instance() username = request.user.username hue_instance_id = request.META[constants.HUE_INSTANCE_ID] try: shell_pairs = utils.parse_shell_pairs(request) except ValueError: shell_pairs = [] result = shell_manager.add_to_output(username, hue_instance_id, shell_pairs) return HttpResponse(simplejson.dumps(result), mimetype="application/json")
def test_parse_shell_pairs(): request = TestRequest() request.POST[constants.NUM_PAIRS] = 2 request.POST["%s2" % (constants.SHELL_ID, )] = '0' request.POST["%s2" % (constants.OFFSET, )] = '0' request.POST["%s1" % (constants.SHELL_ID, )] = '1' request.POST["%s1" % (constants.OFFSET, )] = '1' parsed_pairs = utils.parse_shell_pairs(request) assert parsed_pairs[0][0] == '1' assert parsed_pairs[0][1] == 1 assert parsed_pairs[1][0] == '0' assert parsed_pairs[1][1] == 0 request.POST[constants.NUM_PAIRS] = 1 assert len(utils.parse_shell_pairs(request)) == 1 request.POST[constants.NUM_PAIRS] = 'most definitely not a number' try: utils.parse_shell_pairs(request) except ValueError: pass else: assert False, "parse_shell_pairs did not throw an exception when trying to convert a malformed string to integer"
def add_to_output(request): if not _running_with_spawning(request): result = simplejson.dumps({constants.NOT_RUNNING_SPAWNING: True}) return HttpResponse(result, mimetype="application/json") shell_manager = ShellManager.global_instance() username = request.user.username hue_instance_id = request.META[constants.HUE_INSTANCE_ID] try: shell_pairs = utils.parse_shell_pairs(request) except ValueError: shell_pairs = [] result = shell_manager.add_to_output(username, hue_instance_id, shell_pairs) return HttpResponse(simplejson.dumps(result), mimetype="application/json")