def _parse_question(self, schema, questionnaire): """Parse a question element :param schema: The question schema :returns: A Question object :raises: SchemaParserException """ question_type = ParserUtils.get_required_string(schema, "type") question = self.question_factory.create(question_type.upper()) question.type = question_type question.id = ParserUtils.get_required_string(schema, "id") question.title = ParserUtils.get_required_string(schema, "title") question.number = ParserUtils.get_optional_string(schema, "number") question.description = ParserUtils.get_optional_string( schema, "description") question.skip_condition = ParserUtils.get_optional( schema, "skip_condition") question.guidance = ParserUtils.get_optional(schema, "guidance") # register the question questionnaire.register(question) if 'answers' in schema.keys(): for answer_schema in schema['answers']: question.add_answer( self._parse_answer(answer_schema, questionnaire)) else: raise SchemaParserException( 'Question must contain at least one answer') return question
def test_get_optional(self): schema = { "property": 'tada!' } try: self.assertEqual(ParserUtils.get_optional(schema, 'property'), 'tada!') except: self.fail('An unexpected exception was thrown') try: notfound = ParserUtils.get_optional(schema, 'notfound') self.assertIsNone(notfound) except: self.fail('An unexpected exception was thrown')
def _parse_question(self, schema, questionnaire): """Parse a question element :param schema: The question schema :returns: A Question object :raises: SchemaParserException """ try: question_type = ParserUtils.get_required_string(schema, "type") question = self.question_factory.create(question_type.upper()) question.type = question_type question.id = ParserUtils.get_required_string(schema, "id") question.title = ParserUtils.get_required_string(schema, "title") question.description = ParserUtils.get_required_string(schema, "description") question.skip_condition = self._parse_skip_condition(ParserUtils.get_optional(schema, "skip_condition")) # register the question questionnaire.register(question) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e if 'answers' in schema.keys(): for answer_schema in schema['answers']: question.add_answer(self._parse_answer(answer_schema, questionnaire)) else: raise SchemaParserException('Question must contain at least one answer') return question
def _parse_display(self, schema): """ Parse a display element :param schema: the display element :return: A display object """ display = Display() properties = ParserUtils.get_optional(schema, "properties") if properties: display.properties = self._parse_properties(properties) return display
def _parse_answer(self, schema, questionnaire): """Parse a answer element :param schema: The answer schema :returns: A Answer object :raises: SchemaParserException """ try: answer_type = ParserUtils.get_required_string(schema, 'type') answer_id = ParserUtils.get_required_string(schema, 'id') answer = self.answer_factory.create(answer_type.upper(), answer_id) answer.type = answer_type answer.code = ParserUtils.get_required_string(schema, 'q_code') answer.label = ParserUtils.get_optional_string(schema, 'label') answer.guidance = ParserUtils.get_optional_string(schema, 'guidance') answer.mandatory = ParserUtils.get_required_boolean(schema, 'mandatory') answer.options = ParserUtils.get_optional_array(schema, 'options') answer.alias = ParserUtils.get_optional_string(schema, 'alias') display = ParserUtils.get_optional(schema, "display") if display: answer.display = self._parse_display(display) if 'validation' in schema.keys(): self._parse_validation(answer, schema['validation']) # register the answer questionnaire.register(answer) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e return answer
def test_get_optional(self): schema = {"property": 'tada!'} self.assertEqual(ParserUtils.get_optional(schema, 'property'), 'tada!') notfound = ParserUtils.get_optional(schema, 'notfound') self.assertIsNone(notfound)