Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre- and post- code inclusion to fppgen #31

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
50 changes: 47 additions & 3 deletions fppgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
"<code-lines>",
tab + indented,
"</code-lines>"
]

if prefix_code != '':
xml_items = [
"<pre-text>",
tab + prefix_code.replace('\n', '\n' + tab),
"</pre-text>"
] + xml_items

if suffix_code != '':
xml_items += [
"<post-text>",
tab + suffix_code.replace('\n', '\n' + tab),
"</post-text>"
]

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 + '<!-- Write the question prompt here -->'
Expand Down Expand Up @@ -95,9 +131,9 @@ def format_annotated_name(name: AnnotatedName) -> str:
</pl-question-panel>

<!-- see README for where the various parts of question live -->
<pl-faded-parsons>
<pl-faded-parsons {xml_tags}>
{tab}{indented}
</pl-faded-parsons>""".format(question_text=question_text, tab=tab, indented=indented)
</pl-faded-parsons>""".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:
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -224,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:
Expand Down Expand Up @@ -263,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,
Expand Down
9 changes: 5 additions & 4 deletions fppgen/lib/autograde.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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:
Expand Down Expand Up @@ -71,7 +72,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:
nelson-lojo marked this conversation as resolved.
Show resolved Hide resolved
test_region = test_region if test_region != "" else TEST_DEFAULT
try:
try:
Expand All @@ -90,7 +91,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, *,
Expand All @@ -113,7 +114,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)
Expand Down
4 changes: 2 additions & 2 deletions pl-faded-parsons-question.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
{{/scrambled}}
{{#pre_text}}
<pre class="prettyprint" language="{{{language}}}"> {{{text}}} </pre>
<pre class="prettyprint" {{#language}}language="{{{language}}}"{{/language}}> {{{text}}} </pre>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we checked this works? Inner "{{{language}}}" might need be to "{{..}}" or something like it

{{/pre_text}}
{{#given}}
<div id="solution-{{uuid}}" class="solution-code-tray codeline-tray soft-border col-sm-{{#narrow}}6{{/narrow}}{{#wide}}12{{/wide}} {{^wide}}px-1{{/wide}}">
Expand All @@ -35,7 +35,7 @@
</div>
{{/given}}
{{#post_text}}
<pre class="prettyprint" language="{{{language}}}"> {{{text}}} </pre>
<pre class="prettyprint" {{#language}}language="{{{language}}}"{{/language}}> {{{text}}} </pre>
{{/post_text}}
</div>

Expand Down