Esempio n. 1
0
def update(course=False):
    """
    Update the data of courses and or exercises from server.
    """
    if course:
        with Spinner.context(msg="Updated course metadata.",
                             waitmsg="Updating course metadata."):
            for course in api.get_courses():
                old = None
                try:
                    old = Course.get(Course.tid == course["id"])
                except peewee.DoesNotExist:
                    old = None
                if old:
                    old.details_url = course["details_url"]
                    old.save()
                    continue
                Course.create(tid=course["id"],
                              name=course["name"],
                              details_url=course["details_url"])
    else:
        selected = Course.get_selected()

        # with Spinner.context(msg="Updated exercise metadata.",
        #                     waitmsg="Updating exercise metadata."):
        print("Updating exercise data.")
        for exercise in api.get_exercises(selected):
            old = None
            try:
                old = Exercise.byid(exercise["id"])
            except peewee.DoesNotExist:
                old = None
            if old is not None:
                old.name = exercise["name"]
                old.course = selected.id
                old.is_attempted = exercise["attempted"]
                old.is_completed = exercise["completed"]
                old.deadline = exercise.get("deadline")
                old.is_downloaded = os.path.isdir(old.path())
                old.return_url = exercise["return_url"]
                old.zip_url = exercise["zip_url"]
                old.submissions_url = exercise["exercise_submissions_url"]
                old.save()
                download_exercise(old, update=True)
            else:
                ex = Exercise.create(tid=exercise["id"],
                                     name=exercise["name"],
                                     course=selected.id,
                                     is_attempted=exercise["attempted"],
                                     is_completed=exercise["completed"],
                                     deadline=exercise.get("deadline"),
                                     return_url=exercise["return_url"],
                                     zip_url=exercise["zip_url"],
                                     submissions_url=exercise[("exercise_"
                                                               "submissions_"
                                                               "url")])
                ex.is_downloaded = os.path.isdir(ex.path())
                ex.save()
Esempio n. 2
0
def update(course=False):
    """
    Update the data of courses and or exercises from server.
    """
    if course:
        with Spinner.context(msg="Updated course metadata.",
                             waitmsg="Updating course metadata."):
            for course in api.get_courses():
                old = None
                try:
                    old = Course.get(Course.tid == course["id"])
                except peewee.DoesNotExist:
                    old = None
                if old:
                    old.details_url = course["details_url"]
                    old.save()
                    continue
                Course.create(tid=course["id"], name=course["name"],
                              details_url=course["details_url"])
    else:
        selected = Course.get_selected()

        # with Spinner.context(msg="Updated exercise metadata.",
        #                     waitmsg="Updating exercise metadata."):
        print("Updating exercise data.")
        for exercise in api.get_exercises(selected):
            old = None
            try:
                old = Exercise.byid(exercise["id"])
            except peewee.DoesNotExist:
                old = None
            if old is not None:
                old.name = exercise["name"]
                old.course = selected.id
                old.is_attempted = exercise["attempted"]
                old.is_completed = exercise["completed"]
                old.deadline = exercise.get("deadline")
                old.is_downloaded = os.path.isdir(old.path())
                old.return_url = exercise["return_url"]
                old.zip_url = exercise["zip_url"]
                old.submissions_url = exercise["exercise_submissions_url"]
                old.save()
                download_exercise(old, update=True)
            else:
                ex = Exercise.create(tid=exercise["id"],
                                     name=exercise["name"],
                                     course=selected.id,
                                     is_attempted=exercise["attempted"],
                                     is_completed=exercise["completed"],
                                     deadline=exercise.get("deadline"),
                                     return_url=exercise["return_url"],
                                     zip_url=exercise["zip_url"],
                                     submissions_url=exercise[("exercise_"
                                                               "submissions_"
                                                               "url")])
                ex.is_downloaded = os.path.isdir(ex.path())
                ex.save()
Esempio n. 3
0
def submit(course, tid=None, pastebin=False, review=False):
    """
    Submit the selected exercise to the server.
    """
    if tid is not None:
        return submit_exercise(Exercise.byid(tid),
                               pastebin=pastebin,
                               request_review=review)
    else:
        sel = Exercise.get_selected()
        if not sel:
            raise NoExerciseSelected()
        return submit_exercise(sel, pastebin=pastebin, request_review=review)
Esempio n. 4
0
def test(course, tid=None, time=None):
    """
    Run tests on the selected exercise.
    """
    if time is not None:
        conf.tests_show_time = time
    if tid is not None:
        return run_test(Exercise.byid(tid))
    else:
        sel = Exercise.get_selected()
        if not sel:
            raise NoExerciseSelected()
        return run_test(sel)
Esempio n. 5
0
def test(course, tid=None, time=None):
    """
    Run tests on the selected exercise.
    """
    if time is not None:
        conf.tests_show_time = time
    if tid is not None:
        return run_test(Exercise.byid(tid))
    else:
        sel = Exercise.get_selected()
        if not sel:
            raise NoExerciseSelected()
        return run_test(sel)
Esempio n. 6
0
def submit(course, tid=None, pastebin=False, review=False):
    """
    Submit the selected exercise to the server.
    """
    if tid is not None:
        return submit_exercise(Exercise.byid(tid),
                               pastebin=pastebin,
                               request_review=review)
    else:
        sel = Exercise.get_selected()
        if not sel:
            raise NoExerciseSelected()
        return submit_exercise(sel, pastebin=pastebin, request_review=review)
Esempio n. 7
0
def select(course=False, tid=None, auto=False):
    """
    Select a course or an exercise.
    """
    if course:
        update(course=True)
        course = None
        try:
            course = Course.get_selected()
        except NoCourseSelected:
            pass

        ret = {}
        if not tid:
            ret = Menu.launch("Select a course",
                              Course.select().execute(),
                              course)
        else:
            ret["item"] = Course.get(Course.tid == tid)
        if "item" in ret:
            ret["item"].set_select()
            update()
            if ret["item"].path == "":
                select_a_path(auto=auto)
            # Selects the first exercise in this course
            skip()
            return
        else:
            print("You can select the course with `tmc select --course`")
            return
    else:
        selected = None
        try:
            selected = Exercise.get_selected()
        except NoExerciseSelected:
            pass

        ret = {}
        if not tid:
            ret = Menu.launch("Select an exercise",
                              Course.get_selected().exercises,
                              selected)
        else:
            ret["item"] = Exercise.byid(tid)
        if "item" in ret:
            ret["item"].set_select()
            print("Selected {}".format(ret["item"]))
Esempio n. 8
0
def select(course=False, tid=None, auto=False):
    """
    Select a course or an exercise.
    """
    if course:
        update(course=True)
        course = None
        try:
            course = Course.get_selected()
        except NoCourseSelected:
            pass

        ret = {}
        if not tid:
            ret = Menu.launch("Select a course",
                              Course.select().execute(), course)
        else:
            ret["item"] = Course.get(Course.tid == tid)
        if "item" in ret:
            ret["item"].set_select()
            update()
            if ret["item"].path == "":
                select_a_path(auto=auto)
            # Selects the first exercise in this course
            skip()
            return
        else:
            print("You can select the course with `tmc select --course`")
            return
    else:
        selected = None
        try:
            selected = Exercise.get_selected()
        except NoExerciseSelected:
            pass

        ret = {}
        if not tid:
            ret = Menu.launch("Select an exercise",
                              Course.get_selected().exercises, selected)
        else:
            ret["item"] = Exercise.byid(tid)
        if "item" in ret:
            ret["item"].set_select()
            print("Selected {}".format(ret["item"]))
Esempio n. 9
0
def test_download_single():
    """
    Downloading works
    """
    _, _, ex = run_command(["download", "-f", "-i", exercise_id])
    assert ex is None

    from tmc.models import Exercise

    assert Exercise.get_selected().is_downloaded == True
Esempio n. 10
0
def test_download_single():
    """
    Downloading works
    """
    _, _, ex = run_command(["download", "-f", "-i", exercise_id])
    assert ex is None

    from tmc.models import Exercise

    assert Exercise.get_selected().is_downloaded == True
Esempio n. 11
0
def skip(course, num=1):
    """
    Go to the next exercise.
    """
    sel = None
    try:
        sel = Exercise.get_selected()
        if sel.course.tid != course.tid:
            sel = None
    except NoExerciseSelected:
        pass

    if sel is None:
        sel = course.exercises.first()
    else:
        try:
            sel = Exercise.get(Exercise.id == sel.id + num)
        except peewee.DoesNotExist:
            print("There are no more exercises in this course.")
            return False

    sel.set_select()
    list_all(single=sel)
Esempio n. 12
0
def skip(course, num=1):
    """
    Go to the next exercise.
    """
    sel = None
    try:
        sel = Exercise.get_selected()
        if sel.course.tid != course.tid:
            sel = None
    except NoExerciseSelected:
        pass

    if sel is None:
        sel = course.exercises.first()
    else:
        try:
            sel = Exercise.get(Exercise.id == sel.id + num)
        except peewee.DoesNotExist:
            print("There are no more exercises in this course.")
            return False

    sel.set_select()
    list_all(single=sel)
Esempio n. 13
0
def test_test_fail():
    """
    Testing can fail
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(fail_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command("test")
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == True
    assert "Results:" in stdout
    assert "\033[31m" in stderr and "\033[0m" in stderr
Esempio n. 14
0
def test_test_fail():
    """
    Testing can fail
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(fail_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command("test")
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == True
    assert "Results:" in stdout
    assert "\033[31m" in stderr and "\033[0m" in stderr
Esempio n. 15
0
def test_submit_fail():
    """
    Submitted exercise can fail
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(fail_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command("submit")
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == True
    assert "Results:" in stdout
    uri = os.getenv("TMC_URI", server_uri)
    assert "Submission URL: " + uri + "submissions/" in stdout
    assert "Pastebin: " + uri + "paste/" not in stdout
    assert "Requested a review" not in stdout
    assert "\033[31m" in stderr and "\033[0m" in stderr
Esempio n. 16
0
def test_submit_fail():
    """
    Submitted exercise can fail
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(fail_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command("submit")
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == True
    assert "Results:" in stdout
    uri = os.getenv("TMC_URI", server_uri)
    assert "Submission URL: " + uri + "submissions/" in stdout
    assert "Pastebin: " + uri + "paste/" not in stdout
    assert "Requested a review" not in stdout
    assert "\033[31m" in stderr and "\033[0m" in stderr
Esempio n. 17
0
def test_submit_success():
    """
    Submitted exercise can succeed
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(success_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command(["submit", "-p", "-r"])
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == False
    assert "Results:" in stdout
    assert "Points [1]" in stdout
    assert "Requested a review" in stdout
    uri = os.getenv("TMC_URI", server_uri)
    assert "Submission URL: " + uri + "submissions/" in stdout
    assert "Pastebin: " + uri + "paste/" in stdout

    assert len(stderr) == 0
Esempio n. 18
0
def test_submit_success():
    """
    Submitted exercise can succeed
    """
    from tmc.models import Exercise

    fpath = path.join(Exercise.get_selected().path(), "src", "Nimi.java")
    with open(fpath, "w") as f:
        f.write(success_file)

    os.environ["TMC_TESTING"] = "1"
    wasexit = False
    stdout, stderr, exception = run_command(["submit", "-p", "-r"])
    if type(exception) == TMCExit:
        wasexit = True
    assert wasexit == False
    assert "Results:" in stdout
    assert "Points [1]" in stdout
    assert "Requested a review" in stdout
    uri = os.getenv("TMC_URI", server_uri)
    assert "Submission URL: " + uri + "submissions/" in stdout
    assert "Pastebin: " + uri + "paste/" in stdout

    assert len(stderr) == 0
Esempio n. 19
0
 def inner(*args, **kwargs):
     exercise = Exercise.get_selected()
     return func(exercise, *args, **kwargs)
Esempio n. 20
0
 def dl(id):
     download_exercise(Exercise.get(Exercise.tid == id),
                       force=force,
                       update_java=upgradejava,
                       update=update)
Esempio n. 21
0
 def inner(*args, **kwargs):
     exercise = Exercise.get_selected()
     return func(exercise, *args, **kwargs)
Esempio n. 22
0
 def dl(id):
     download_exercise(Exercise.get(Exercise.tid == id),
                       force=force,
                       update_java=upgradejava,
                       update=update)