Exemple #1
0
def _bf_answer_obj(question_str, parameters_str, question_name,
                   background, snapshot, reference_snapshot):
    # type: (str, str, str, bool, str, Optional[str]) -> Union[str, Dict]

    json.loads(parameters_str)  # a syntactic check for parametersStr
    if not question_name:
        question_name = Options.default_question_prefix + "_" + get_uuid()

    # Upload the question
    json_data = workhelper.get_data_upload_question(bf_session, question_name,
                                                    question_str,
                                                    parameters_str)
    resthelper.get_json_response(bf_session,
                                 CoordConsts.SVC_RSC_UPLOAD_QUESTION, json_data)

    # Answer the question
    work_item = workhelper.get_workitem_answer(bf_session, question_name,
                                               snapshot, reference_snapshot)
    answer_dict = workhelper.execute(work_item, bf_session, background)
    if background:
        return work_item.id
    return answer.from_string(answer_dict["answer"])
Exemple #2
0
def _bf_answer_obj(question_str, parameters_str, question_name, background,
                   snapshot, reference_snapshot, extra_args):
    # type: (str, str, str, bool, str, Optional[str], Optional[Dict[str, Any]]) -> Union[Answer, str]
    from pybatfish.client.commands import bf_session

    json.loads(parameters_str)  # a syntactic check for parametersStr
    if not question_name:
        question_name = Options.default_question_prefix + "_" + get_uuid()

    # Upload the question
    json_data = workhelper.get_data_upload_question(bf_session, question_name,
                                                    question_str,
                                                    parameters_str)
    resthelper.get_json_response(bf_session,
                                 CoordConsts.SVC_RSC_UPLOAD_QUESTION,
                                 json_data)

    # Answer the question
    work_item = workhelper.get_workitem_answer(bf_session, question_name,
                                               snapshot, reference_snapshot)
    workhelper.execute(work_item, bf_session, background, extra_args)

    if background:
        return work_item.id

    # get the answer
    answer_bytes = resthelper.get_answer(bf_session, snapshot, question_name,
                                         reference_snapshot)

    # In Python 3.x, answer needs to be decoded before it can be used
    # for things like json.loads (<= 3.6).
    if six.PY3:
        answer_string = answer_bytes.decode(encoding="utf-8")
    else:
        answer_string = answer_bytes
    answer_obj = json.loads(answer_string)

    return answer.from_string(answer_obj[1]['answer'])