Exemplo n.º 1
0
def load_markio(data, parent, update=False, incr_slug=False, validate=True):
    """
    Creates a CodingIoQuestion object from a Markio object or source
    string and saves the resulting question in the database.

    Args:
        source:
            A string with the Markio source code.
        parent:
            The parent page object.

    Returns:
        A CodingIoQuestion instance.
    """

    from src.questions.coding_io.models import CodingIoQuestion

    if isinstance(data, Markio):
        md = data
    else:
        md = parse_markio(data)

    md.validate()
    question = CodingIoQuestion(title=md.title)
    update_markio_data(question, md)
    parent.add_child(instance=question)
    question.full_clean_all()
    question.save()
    return question
Exemplo n.º 2
0
def load_markio(data, parent, update=False, incr_slug=False, validate=True):
    """
    Creates a CodingIoQuestion object from a Markio object or source
    string and saves the resulting question in the database.

    Args:
        source:
            A string with the Markio source code.
        parent:
            The parent page object.

    Returns:
        A CodingIoQuestion instance.
    """

    from questions.coding_io.models import CodingIoQuestion

    if isinstance(data, Markio):
        md = data
    else:
        md = parse_markio(data)

    md.validate()
    question = CodingIoQuestion(title=md.title)
    update_markio_data(question, md)
    parent.add_child(instance=question)
    question.full_clean_all()
    question.save()
    return question
Exemplo n.º 3
0
def import_markio_from_path(path, parent):
    """
    Like load_markio(), but reads source from the given file path.
    """

    with open(path) as F:
        data = parse_markio(F)
    return load_markio(data, parent)
Exemplo n.º 4
0
def import_markio_from_path(path, parent):
    """
    Like load_markio(), but reads source from the given file path.
    """

    with open(path) as F:
        data = parse_markio(F)
    return load_markio(data, parent)
Exemplo n.º 5
0
def read_markio(path, debug=False):
    """
    Return a Markio AST from given args.
    """

    import markio
    with open(path) as F:
        try:
            return markio.parse_markio(F)
        except SyntaxError as ex:
            if debug:
                raise
            print('Error in markio file: ' + str(ex))
            sys.exit(1)
Exemplo n.º 6
0
def read_markio(path, debug=False):
    """
    Return a Markio AST from given args.
    """

    import markio
    with open(path) as F:
        try:
            return markio.parse_markio(F)
        except SyntaxError as ex:
            if debug:
                raise
            print('Error in markio file: ' + str(ex))
            sys.exit(1)
Exemplo n.º 7
0
def update_markio_data(question, data):
    """
    Update question parameters from Markio file.
    """

    md = data if hasattr(data, 'title') else parse_markio(data)

    # Load simple data from markio
    question.title = md.title or question.title
    question.short_description = (md.short_description or
                                  question.short_description)
    question.timeout = md.timeout or question.timeout
    question.author_name = md.author or question.author_name
    question.pre_tests_source = md.tests_source or question.pre_tests_source
    question.post_tests_source = md.hidden_tests_source
    if md.language is not None:
        question.language = get_programming_language(md.language)
    if md.points is not None:
        question.points_total = md.points
    if md.stars is not None:
        question.starts_total = md.stars

    # Load main description
    question.body = markdown_to_blocks(md.description)

    # Add answer keys
    answer_keys = OrderedDict()
    for (lang, answer_key) in md.answer_key.items():
        language = get_programming_language(lang)
        key = question.answers.create(question=question,
                                      language=language,
                                      source=answer_key)
        answer_keys[lang] = key
    for (lang, placeholder) in md.placeholder.items():
        if placeholder is None:
            question.default_placeholder = placeholder
        try:
            answer_keys[lang].placeholder = placeholder
        except KeyError:
            language = get_programming_language(lang)
            question.answers.create(language=language,
                                    placeholder=placeholder)
    question.__answers = list(answer_keys.values())
    question.answers = question.__answers
Exemplo n.º 8
0
def update_markio_data(question, data):
    """
    Update question parameters from Markio file.
    """

    md = data if hasattr(data, 'title') else parse_markio(data)

    # Load simple data from markio
    question.title = md.title or question.title
    question.short_description = (md.short_description
                                  or question.short_description)
    question.timeout = md.timeout or question.timeout
    question.author_name = md.author or question.author_name
    question.pre_tests_source = md.tests_source or question.pre_tests_source
    question.post_tests_source = md.hidden_tests_source
    if md.language is not None:
        question.language = programming_language(md.language)
    if md.points is not None:
        question.points_total = md.points
    if md.stars is not None:
        question.starts_total = md.stars

    # Load main description
    question.body = markdown_to_blocks(md.description)

    # Add answer keys
    answer_keys = OrderedDict()
    for (lang, answer_key) in md.answer_key.items():
        language = programming_language(lang)
        key = question.answers.create(question=question,
                                      language=language,
                                      source=answer_key)
        answer_keys[lang] = key
    for (lang, placeholder) in md.placeholder.items():
        if placeholder is None:
            question.default_placeholder = placeholder
        try:
            answer_keys[lang].placeholder = placeholder
        except KeyError:
            language = programming_language(lang)
            question.answers.create(language=language, placeholder=placeholder)
    question.__answers = list(answer_keys.values())
    question.answers = question.__answers
Exemplo n.º 9
0
def test_dump_markio_exports_successfully(db):
    question = example('simple')
    md_source = question.dump_markio()
    md = parse_markio(source('simple.md'))
    assert md_source == md.source()
Exemplo n.º 10
0
def test_dump_markio_exports_successfully(db):
    question = example('simple')
    md_source = question.dump_markio()
    md = parse_markio(source('simple.md'))
    assert md_source == md.source()
Exemplo n.º 11
0
def hello_world():
    path = os.path.join(DIRNAME, 'examples', 'hello-world.md')
    data = open(path).read()
    return parse_markio(data)
Exemplo n.º 12
0
def markio(idx):
    return parse_markio(src(idx))
Exemplo n.º 13
0
def hello_world():
    path = os.path.join(DIRNAME, 'examples', 'hello-world.md')
    data = open(path).read()
    return parse_markio(data)
Exemplo n.º 14
0
def markio(idx):
    return parse_markio(src(idx))