コード例 #1
0
ファイル: content.py プロジェクト: eliesmr4/ULearn
 def make_value():
     return verify.Term(verify.BOOLEAN, value)
コード例 #2
0
ファイル: content.py プロジェクト: eliesmr4/ULearn
class AssessmentParser13(object):
    """Grammar and parser for the assessment."""

    string = (QuotedString('\'', escChar='\\', multiline=True)
              ^ QuotedString('"', escChar='\\', multiline=True))

    boolean = (Literal('true').setParseAction(make_bool(True))
               ^ Literal('false').setParseAction(make_bool(False)))

    float = Combine(Word(nums) +
                    Optional(Literal('.') +
                             Word(nums))).setParseAction(make_float)

    choice_decl = (string ^ Combine(sep('correct(') + string + sep(')')).
                   setParseAction(lambda x: verify.Term(verify.CORRECT, x[0])))

    regex = (Regex('/(.*)/i') ^ Combine(
        sep('regex(') + QuotedString('"', escChar='\\') +
        sep(')')).setParseAction(lambda x: verify.Term(verify.REGEX, x[0])))

    question_decl = (sep('{') + Each(
        Optional(key('questionHTML') + sep(':') + string +
                 Optional(sep(','))) +
        Optional(key('lesson') + sep(':') + string + Optional(sep(','))) +
        Optional(
            key('correctAnswerString') + sep(':') + string +
            Optional(sep(','))) + Optional(
                key('correctAnswerRegex') + sep(':') + regex +
                Optional(sep(','))) + Optional(
                    key('correctAnswerNumeric') + sep(':') + float +
                    Optional(sep(','))) +
        Optional(key('multiLine') + sep(':') + boolean + Optional(sep(','))) +
        Optional(
            key('choices') + sep(':') + sep('[') +
            Group(list_of(choice_decl)).setParseAction(make_list) + sep(']') +
            Optional(sep(',')))) + sep('}')).setParseAction(make_dict)

    assessment_grammar = (sep('assessment') + sep('=') + sep('{') + Each(
        Optional(
            key('assessmentName') + sep(':') + string + Optional(sep(','))) +
        Optional(key('preamble') + sep(':') + string + Optional(sep(','))) +
        Optional(
            key('checkAnswers') + sep(':') + boolean + Optional(sep(','))) +
        Optional(
            key('questionsList') + sep(':') + sep('[') +
            Group(list_of(question_decl)).setParseAction(make_list) +
            sep(']') + Optional(sep(',')))) + sep('}') +
                          Optional(sep(';'))).setParseAction(make_dict)

    @classmethod
    def parse_string(cls, content):
        return cls.assessment_grammar.parseString(content)

    @classmethod
    def parse_string_in_scope(cls, content, scope, root_name):
        """Parses assessment text following grammar."""
        if 'assessment' != root_name:
            raise Exception('Unsupported schema: %s', root_name)

        # we need to extract the results as a dictionary; so we remove the
        # outer array holding it
        ast = cls.parse_string(content).asList()
        if len(ast) == 1:
            ast = ast[0]

        return dict(scope.items() + {'__builtins__': {}}.items() +
                    {root_name: ast}.items())
コード例 #3
0
ファイル: content.py プロジェクト: eliesmr4/ULearn
class ActivityParser13(object):
    """Grammar and parser for the activity."""

    variable = Word(alphas)
    integer = Word(nums).setParseAction(make_int)
    string = (QuotedString('\'', escChar='\\', multiline=True)
              ^ QuotedString('"', escChar='\\', multiline=True))
    boolean = (Literal('true').setParseAction(make_bool(True))
               ^ Literal('false').setParseAction(make_bool(False)))

    regex = (Regex('/(.*)/i') ^ Combine(
        sep('regex(') + QuotedString('"', escChar='\\') +
        sep(')')).setParseAction(lambda x: verify.Term(verify.REGEX, x[0])))

    choice_decl = Group(
        sep('[') + string + sep(',') + boolean + sep(',') + string + sep(']'))

    choices_decl = Group(sep('[') + Optional(list_of(choice_decl)) +
                         sep(']')).setParseAction(make_list)

    multiple_choice_decl = (key('questionType') + sep(':') +
                            key('multiple choice') + Optional(sep(',')))

    multiple_choice = (sep('{') + multiple_choice_decl + Each(
        Optional(key('questionHTML') + sep(':') + string +
                 Optional(sep(','))) +
        Optional(
            key('choices') + sep(':') + choices_decl + Optional(sep(',')))) +
                       sep('}')).setParseAction(make_dict)

    free_text_decl = (key('questionType') + sep(':') + key('freetext') +
                      Optional(sep(',')))

    free_text = (sep('{') + free_text_decl + Each(
        Optional(key('questionHTML') + sep(':') + string +
                 Optional(sep(','))) + Optional(
                     key('correctAnswerRegex') + sep(':') + regex +
                     Optional(sep(','))) + Optional(
                         key('correctAnswerOutput') + sep(':') + string +
                         Optional(sep(','))) + Optional(
                             key('incorrectAnswerOutput') + sep(':') + string +
                             Optional(sep(','))) +
        Optional(
            key('showAnswerPrompt') + sep(':') + string + Optional(sep(','))) +
        Optional(
            key('showAnswerOutput') + sep(':') + string + Optional(sep(','))) +
        Optional(key('outputHeight') + sep(':') + string + Optional(sep(','))))
                 + sep('}')).setParseAction(make_dict)

    question_list_decl = (sep('{') + Each(
        Optional(key('questionHTML') + sep(':') + string + Optional(sep(',')))
        + Optional(
            key('choices') + sep(':') + sep('[') +
            Group(list_of(string)).setParseAction(make_list) + sep(']') +
            Optional(sep(','))) + Optional(
                key('correctIndex') + sep(':') +
                (integer ^ (sep('[') + Group(list_of(integer)).setParseAction(
                    make_list) + sep(']'))) + Optional(sep(','))) +
        Optional(key('multiSelect') + sep(':') + boolean +
                 Optional(sep(','))), ) + sep('}')).setParseAction(make_dict)

    questions_list_decl = Group(
        sep('[') + Optional(list_of(question_list_decl)) +
        sep(']')).setParseAction(make_list)

    multiple_choice_group_decl = (key('questionType') + sep(':') +
                                  key('multiple choice group') +
                                  Optional(sep(',')))

    multiple_choice_group = (sep('{') + multiple_choice_group_decl + Each(
        Optional(
            key('questionGroupHTML') + sep(':') + string +
            Optional(sep(','))) + Optional(
                key('allCorrectMinCount') + sep(':') + integer +
                Optional(sep(','))) + Optional(
                    key('allCorrectOutput') + sep(':') + string +
                    Optional(sep(','))) + Optional(
                        key('someIncorrectOutput') + sep(':') + string +
                        Optional(sep(','))) + Optional(
                            key('questionsList') + sep(':') +
                            questions_list_decl + Optional(sep(',')))) +
                             sep('}')).setParseAction(make_dict)

    activity_grammar = (sep('activity') + sep('=') + sep('[') + Optional(
        list_of(string ^ multiple_choice ^ free_text ^ multiple_choice_group))
                        + sep(']') + Optional(sep(';')))

    @classmethod
    def parse_string(cls, content):
        return cls.activity_grammar.parseString(content)

    @classmethod
    def parse_string_in_scope(cls, content, scope, root_name):
        """Parses activity text following grammar."""
        if 'activity' != root_name:
            raise Exception('Unsupported schema: %s', root_name)
        return dict(scope.items() + {'__builtins__': {}}.items() +
                    {root_name: cls.parse_string(content).asList()}.items())