Skip to content

Commit

Permalink
fix AG output duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
LarynQi committed Nov 9, 2023
1 parent 89cfebc commit bf578b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions client/protocols/grading.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ def grade(questions, messages, env=None, verbose=True):
if not verbose and (failed > 0 or locked > 0):
# Stop at the first failed test
break

format.print_progress_bar('Test summary', passed, failed, locked,
verbose=verbose)
print()

autograder_output = ''.join(output.get_log(log_id))

messages['grading'] = analytics

autograder_output = ''.join(output.get_log(log_id))
messages['autograder_output'] = autograder_output

protocol = GradingProtocol
4 changes: 0 additions & 4 deletions client/protocols/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
import sys
import re

import logging

from client.utils.printer import print_error

log = logging.getLogger(__name__)

class HelpProtocol(models.Protocol):

SERVER = 'https://61a-bot-backend.zamfi.net'
Expand Down
4 changes: 4 additions & 0 deletions client/sources/ok_test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,15 @@ def _run_case(self, test_name, suite_number, case, case_number):
output.remove_log(log_id)

if not success or self.verbose:
output.disable_all_logs()
print(''.join(output_log))
output.enable_all_logs()
if not success:
short_name = self.test.get_short_name()
# TODO: Change when in notebook mode
print('Run only this test case with '
'"python3 ok -q {} --suite {} --case {}"'.format(
short_name, suite_number, case_number))


return success
33 changes: 31 additions & 2 deletions client/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, stdout=sys.stdout):
self._devnull = io.open(os.devnull, 'w', encoding=getattr(stdout, 'encoding', 'utf-8'))
self._logs = {}
self._num_logs = 0
self._disabled_logs = set()

def on(self):
"""Allows print statements to emit to standard output."""
Expand Down Expand Up @@ -56,12 +57,28 @@ def write(self, msg):
msg -- str
"""
self._current_stream.write(msg)
for log in self._logs.values():
log.append(msg)
for log_id, log in self._logs.items():
if log_id not in self._disabled_logs:
log.append(msg)

def flush(self):
self._current_stream.flush()

def disable_log(self, log_id):
self._disabled_logs.add(log_id)

def enable_log(self, log_id):
self.disable_log(log_id)
self._disabled_logs.remove(log_id)

def disable_all_logs(self):
for log_id in self._logs:
self.disable_log(log_id)

def enable_all_logs(self):
for log_id in self._logs:
self.enable_log(log_id)

# TODO(albert): rewrite this to be cleaner.
def __getattr__(self, attr):
return getattr(self._current_stream, attr)
Expand All @@ -86,6 +103,18 @@ def remove_log(log_id):
def remove_all_logs():
_logger.remove_all_logs()

def disable_log(log_id):
_logger.disable_log(log_id)

def enable_log(log_id):
_logger.enable_log(log_id)

def disable_all_logs():
_logger.disable_all_logs()

def enable_all_logs():
_logger.enable_all_logs()

class DisableLog:
re_enable = False
def __enter__(self):
Expand Down

0 comments on commit bf578b6

Please sign in to comment.