From 60c378a2894abfe9e48174c0cb45d9f53046c6d4 Mon Sep 17 00:00:00 2001 From: Nelson Lojo Date: Sun, 7 Apr 2024 20:29:25 +0000 Subject: [PATCH 1/7] moved all generation files into same directory --- generate_fpp.py => fppgen/__main__.py | 0 {lib => fppgen/lib}/autograde.py | 0 {lib => fppgen/lib}/consts.py | 0 {lib => fppgen/lib}/generate_test.py | 0 {lib => fppgen/lib}/io_helpers.py | 0 {lib => fppgen/lib}/name_visitor.py | 0 {lib => fppgen/lib}/parse.py | 0 {lib => fppgen/lib}/tokens.py | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename generate_fpp.py => fppgen/__main__.py (100%) rename {lib => fppgen/lib}/autograde.py (100%) rename {lib => fppgen/lib}/consts.py (100%) rename {lib => fppgen/lib}/generate_test.py (100%) rename {lib => fppgen/lib}/io_helpers.py (100%) rename {lib => fppgen/lib}/name_visitor.py (100%) rename {lib => fppgen/lib}/parse.py (100%) rename {lib => fppgen/lib}/tokens.py (100%) diff --git a/generate_fpp.py b/fppgen/__main__.py similarity index 100% rename from generate_fpp.py rename to fppgen/__main__.py diff --git a/lib/autograde.py b/fppgen/lib/autograde.py similarity index 100% rename from lib/autograde.py rename to fppgen/lib/autograde.py diff --git a/lib/consts.py b/fppgen/lib/consts.py similarity index 100% rename from lib/consts.py rename to fppgen/lib/consts.py diff --git a/lib/generate_test.py b/fppgen/lib/generate_test.py similarity index 100% rename from lib/generate_test.py rename to fppgen/lib/generate_test.py diff --git a/lib/io_helpers.py b/fppgen/lib/io_helpers.py similarity index 100% rename from lib/io_helpers.py rename to fppgen/lib/io_helpers.py diff --git a/lib/name_visitor.py b/fppgen/lib/name_visitor.py similarity index 100% rename from lib/name_visitor.py rename to fppgen/lib/name_visitor.py diff --git a/lib/parse.py b/fppgen/lib/parse.py similarity index 100% rename from lib/parse.py rename to fppgen/lib/parse.py diff --git a/lib/tokens.py b/fppgen/lib/tokens.py similarity index 100% rename from lib/tokens.py rename to fppgen/lib/tokens.py From 164fd7a2c7cf207445adc37a831c734c4a5d6c1c Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Wed, 29 May 2024 23:34:35 +0000 Subject: [PATCH 2/7] provide python version to devcontainer --- .devcontainer/csxxx-example/devcontainer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.devcontainer/csxxx-example/devcontainer.json b/.devcontainer/csxxx-example/devcontainer.json index d2c1e8d..699e22a 100644 --- a/.devcontainer/csxxx-example/devcontainer.json +++ b/.devcontainer/csxxx-example/devcontainer.json @@ -9,7 +9,9 @@ // "postAttachCommand" : "export HOST_JOBS_DIR=/tmp/pl_jobqueue && sudo docker run -it --rm -p 4000:3000 -v $HOST_JOBS_DIR:/jobs -v /pl-ucb-csxxx/:/course -v /var/run/docker.sock:/var/run/docker.sock ucbcbt/ucb-prairielearn:latest", "features": { - "ghcr.io/devcontainers/features/python:1": {}, + "ghcr.io/devcontainers/features/python:1": { + "version" : "3.10" + }, // "ghcr.io/devcontainers/features/docker-in-docker:2": {} "ghcr.io/devcontainers/features/docker-outside-of-docker:1": {} } From 49c62fc8b8b4cd3e1ad4bb65a49ceed3cd5adcef Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Thu, 30 May 2024 01:55:51 +0000 Subject: [PATCH 3/7] added pre/post text to fppgen --- fppgen/__main__.py | 45 +++++++++++++++++++++++++++++++++++++++-- fppgen/lib/autograde.py | 12 ++++++----- fppgen/lib/consts.py | 2 ++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/fppgen/__main__.py b/fppgen/__main__.py index d42eb4e..6ae5531 100644 --- a/fppgen/__main__.py +++ b/fppgen/__main__.py @@ -52,13 +52,49 @@ def dump(self) -> str: def generate_question_html( prompt_code: str, *, + prefix_code: str = '', + suffix_code: str = '', question_text: str = None, tab: str = ' ', + display_format: str = '', setup_names: List[AnnotatedName] = None, answer_names: List[AnnotatedName] = None ) -> str: """Turn an extracted prompt string into a question html file body""" indented = prompt_code.replace('\n', '\n' + tab) + if prefix_code != '' or suffix_code != '': + xml_items = [ + "", + tab + indented, + "" + ] + + if prefix_code != '': + xml_items = [ + "", + tab + prefix_code.replace('\n', '\n' + tab), + "" + ] + xml_items + + if suffix_code != '': + xml_items += [ + "", + tab + suffix_code.replace('\n', '\n' + tab), + "" + ] + + indented = f"\n{tab}".join(xml_items) + + xml_tag_dict = { } + if prefix_code != '' or suffix_code != '': + xml_tag_dict['format'] = "bottom" + if display_format != '': + xml_tag_dict['format'] = display_format + + xml_tags = " ".join([ + f'{key}="{value}"' + for key, value in xml_tag_dict.items() + ]) if question_text is None: question_text = tab + '' @@ -95,9 +131,9 @@ def format_annotated_name(name: AnnotatedName) -> str: - + {tab}{indented} -""".format(question_text=question_text, tab=tab, indented=indented) +""".format(question_text=question_text, tab=tab, indented=indented, xml_tags=xml_tags) def generate_info_json(question_name: str, autograder: AutograderConfig, *, indent=4) -> str: @@ -178,7 +214,9 @@ def remove_region(key, default=''): setup_code = remove_region('setup_code', SETUP_CODE_DEFAULT) answer_code = remove_region('answer_code') server_code = remove_region('server') + prefix_code = remove_region('prefix_code') prompt_code = remove_region('prompt_code') + suffix_code = remove_region('suffix_code') question_text = remove_region('question_text') if options.verbosity > 0: print('- Populating {} ...'.format(question_dir)) @@ -192,7 +230,10 @@ def remove_region(key, default=''): question_html = generate_question_html( prompt_code, + prefix_code=prefix_code, + suffix_code=suffix_code, question_text=question_text, + # display_format=..., # TODO: get this working setup_names=setup_names, # show_required removed: # answer_names=answer_names if show_required else None diff --git a/fppgen/lib/autograde.py b/fppgen/lib/autograde.py index b420adb..c383dd5 100644 --- a/fppgen/lib/autograde.py +++ b/fppgen/lib/autograde.py @@ -1,10 +1,12 @@ from abc import ABC, abstractmethod from typing import Dict, Union -from json import loads, dumps +from json import json_loads, json_dumps +from yaml import yaml_loads, YAMLError +from subprocess import run, PIPE from os import makedirs, path, popen from lib.name_visitor import AnnotatedName, generate_server, SERVER_DEFAULT -from lib.consts import TEST_DEFAULT +from lib.consts import TEST_DEFAULT, GenerationError from lib.io_helpers import write_to, Bcolors from lib.generate_test import make_test_file @@ -75,7 +77,7 @@ def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, t test_region = test_region if test_region != "" else TEST_DEFAULT try: try: - json = loads(test_region) + json = json_loads(test_region) success, test_file = True, make_test_file(json) except Exception as e: success, test_file = False, test_region @@ -90,7 +92,7 @@ def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, t test_file = test_region write_to(test_dir, 'test.py', test_file) - write_to(test_dir, 'ans.py', answer_code) + write_to(test_dir, 'ans.py', "\n".join([pre_code, answer_code, post_code])) write_to(test_dir, 'setup_code.py', setup_code) def generate_server(self, setup_code: str, answer_code: str, *, @@ -121,7 +123,7 @@ def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, t if log_details: Bcolors.info(' - Generating grader metadata') - metadata = dumps({ + metadata = json_dumps({ "submission_file": "script.rb", "submission_root": "", "submit_to_line" : -1, diff --git a/fppgen/lib/consts.py b/fppgen/lib/consts.py index c911abd..a671144 100644 --- a/fppgen/lib/consts.py +++ b/fppgen/lib/consts.py @@ -42,6 +42,8 @@ def fail(*args, **kwargs): def info(*args, **kwargs): Bcolors.printf(Bcolors.OK_BLUE, *args, **kwargs) +class GenerationError(Exception): + pass TEST_DEFAULT: Final[str] = """# AUTO-GENERATED FILE # go to https://prairielearn.readthedocs.io/en/latest/python-grader/#teststestpy for more info From babc6df1737db8be74b34db51d59e0ee06d42c8f Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Thu, 30 May 2024 18:06:02 -0700 Subject: [PATCH 4/7] upstreaming pre/post text fixes --- fppgen/__main__.py | 5 ++++- fppgen/lib/autograde.py | 7 ++++--- pl-faded-parsons-question.mustache | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/fppgen/__main__.py b/fppgen/__main__.py index 6ae5531..7ca1f25 100644 --- a/fppgen/__main__.py +++ b/fppgen/__main__.py @@ -265,7 +265,9 @@ def remove_region(key, default=''): answer_code, setup_code, test_region, - log_details= options.verbosity > 0 + pre_code = prefix_code, + post_code = suffix_code, + log_details = options.verbosity > 0 ) if metadata: @@ -304,6 +306,7 @@ def generate_one(source_path, force_json=False): options = Options(args) options.force_generate_json = force_json + source_path = path.abspath(source_path) try: generate_fpp_question( source_path, diff --git a/fppgen/lib/autograde.py b/fppgen/lib/autograde.py index c383dd5..4353524 100644 --- a/fppgen/lib/autograde.py +++ b/fppgen/lib/autograde.py @@ -24,7 +24,8 @@ def info_json_update(self) -> Dict[str, Union[str, Dict[str, Union[bool, str, in pass @abstractmethod - def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, pre_code: str='', post_code: str='', log_details: bool= True) -> None: + def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, + pre_code: str = '', post_code: str = '', log_details: bool = True) -> None: pass def clean_tests_dir(self, test_dir: str) -> None: @@ -73,7 +74,7 @@ def info_json_update(self) -> Dict[str, Union[str, Dict[str, Union[bool, str, in } def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, - pre_code: str='', post_code: str='', log_details: bool= True) -> None: + pre_code: str = '', post_code: str = '', log_details: bool = True) -> None: test_region = test_region if test_region != "" else TEST_DEFAULT try: try: @@ -115,7 +116,7 @@ def info_json_update(self) -> Dict[str, Union[str, Dict[str, Union[bool, str, in } def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, - pre_code: str='', post_code: str='', log_details: bool= True) -> None: + pre_code: str = '', post_code: str = '', log_details: bool = True) -> None: app_dir = path.join(path.dirname(f"{test_dir}/"), "app") spec_dir = path.join(path.dirname(f"{app_dir}/"), "spec") makedirs(spec_dir, exist_ok=True) diff --git a/pl-faded-parsons-question.mustache b/pl-faded-parsons-question.mustache index ec647c8..0c7e87e 100644 --- a/pl-faded-parsons-question.mustache +++ b/pl-faded-parsons-question.mustache @@ -26,7 +26,7 @@ {{/scrambled}} {{#pre_text}} - {{{text}}} + {{{text}}} {{/pre_text}} {{#given}} @@ -40,7 +40,7 @@ {{/given}} {{#post_text}} - {{{text}}} + {{{text}}} {{/post_text}} From 3359933ae1694d8a639e98d6fff352fbd9b0016a Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Thu, 30 May 2024 18:31:16 -0700 Subject: [PATCH 5/7] remove rspec autograder changes --- fppgen/lib/autograde.py | 10 ++++------ fppgen/lib/consts.py | 2 -- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/fppgen/lib/autograde.py b/fppgen/lib/autograde.py index 4353524..f5969c1 100644 --- a/fppgen/lib/autograde.py +++ b/fppgen/lib/autograde.py @@ -1,12 +1,10 @@ from abc import ABC, abstractmethod from typing import Dict, Union -from json import json_loads, json_dumps -from yaml import yaml_loads, YAMLError -from subprocess import run, PIPE +from json import loads, dumps from os import makedirs, path, popen from lib.name_visitor import AnnotatedName, generate_server, SERVER_DEFAULT -from lib.consts import TEST_DEFAULT, GenerationError +from lib.consts import TEST_DEFAULT from lib.io_helpers import write_to, Bcolors from lib.generate_test import make_test_file @@ -78,7 +76,7 @@ def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, t test_region = test_region if test_region != "" else TEST_DEFAULT try: try: - json = json_loads(test_region) + json = loads(test_region) success, test_file = True, make_test_file(json) except Exception as e: success, test_file = False, test_region @@ -124,7 +122,7 @@ def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, t if log_details: Bcolors.info(' - Generating grader metadata') - metadata = json_dumps({ + metadata = dumps({ "submission_file": "script.rb", "submission_root": "", "submit_to_line" : -1, diff --git a/fppgen/lib/consts.py b/fppgen/lib/consts.py index a671144..c911abd 100644 --- a/fppgen/lib/consts.py +++ b/fppgen/lib/consts.py @@ -42,8 +42,6 @@ def fail(*args, **kwargs): def info(*args, **kwargs): Bcolors.printf(Bcolors.OK_BLUE, *args, **kwargs) -class GenerationError(Exception): - pass TEST_DEFAULT: Final[str] = """# AUTO-GENERATED FILE # go to https://prairielearn.readthedocs.io/en/latest/python-grader/#teststestpy for more info From 281071c6114ce2417cd7e9c1b4668430aeb28d1a Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Sun, 9 Jun 2024 19:26:33 -0700 Subject: [PATCH 6/7] recover new pre/post code xml tag --- pl-faded-parsons-question.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pl-faded-parsons-question.mustache b/pl-faded-parsons-question.mustache index d8d5e6a..518994c 100644 --- a/pl-faded-parsons-question.mustache +++ b/pl-faded-parsons-question.mustache @@ -22,7 +22,7 @@ {{/scrambled}} {{#pre_text}} - {{{text}}} +
 {{{text}}} 
{{/pre_text}} {{#given}}
@@ -34,7 +34,7 @@
{{/given}} {{#post_text}} - {{{text}}} +
 {{{text}}} 
{{/post_text}} From 6e488e367f84d545f6dcd6a4b1884609abb383b0 Mon Sep 17 00:00:00 2001 From: Nelson Lojo <31297176+nelson-lojo@users.noreply.github.com> Date: Sun, 9 Jun 2024 19:28:44 -0700 Subject: [PATCH 7/7] drop excess whitespace --- fppgen/__main__.py | 1 - fppgen/lib/autograde.py | 2 -- pl-faded-parsons-question.mustache | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/fppgen/__main__.py b/fppgen/__main__.py index 838c88f..7ca1f25 100644 --- a/fppgen/__main__.py +++ b/fppgen/__main__.py @@ -131,7 +131,6 @@ def format_annotated_name(name: AnnotatedName) -> str: - {tab}{indented} """.format(question_text=question_text, tab=tab, indented=indented, xml_tags=xml_tags) diff --git a/fppgen/lib/autograde.py b/fppgen/lib/autograde.py index e382078..f5969c1 100644 --- a/fppgen/lib/autograde.py +++ b/fppgen/lib/autograde.py @@ -73,7 +73,6 @@ def info_json_update(self) -> Dict[str, Union[str, Dict[str, Union[bool, str, in def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, pre_code: str = '', post_code: str = '', log_details: bool = True) -> None: - test_region = test_region if test_region != "" else TEST_DEFAULT try: try: @@ -116,7 +115,6 @@ def info_json_update(self) -> Dict[str, Union[str, Dict[str, Union[bool, str, in def populate_tests_dir(self, test_dir: str, answer_code: str, setup_code: str, test_region: str, pre_code: str = '', post_code: str = '', log_details: bool = True) -> None: - app_dir = path.join(path.dirname(f"{test_dir}/"), "app") spec_dir = path.join(path.dirname(f"{app_dir}/"), "spec") makedirs(spec_dir, exist_ok=True) diff --git a/pl-faded-parsons-question.mustache b/pl-faded-parsons-question.mustache index 518994c..9972309 100644 --- a/pl-faded-parsons-question.mustache +++ b/pl-faded-parsons-question.mustache @@ -34,7 +34,7 @@ {{/given}} {{#post_text}} -
 {{{text}}} 
+
 {{{text}}} 
{{/post_text}}