def update_roles(astudent): """Initialize the role list for the student and its 'ancestors'""" if 'roles' in astudent.__dict__: return astudent.roles_filename = os.path.join(astudent.file, 'roles') if astudent.filename in default_roles: astudent.roles = default_roles[astudent.filename] else: default = "['Student']" utilities.write(astudent.roles_filename, default, overwrite=False) try: astudent.roles = eval(utilities.read(astudent.roles_filename)) except: print('BUG', astudent, utilities.read(astudent.roles_filename)) astudent.roles = eval(default) astudent.current_role = astudent.roles[0] astudent.old_role = None for role in astudent.roles: update_roles( student.student(role) )
def update_student_acls(astudent): """Read ACL for a student or a role from the file""" if 'acls' in astudent.__dict__: return False astudent.acls = StudentAcls(os.path.join(astudent.file, 'acls')) try: c = utilities.read(astudent.acls.filename) except: pass else: if c: astudent.acls.acls = eval(c) for role in astudent.roles: update_student_acls(student.student(role)) return True
def execute(state, plugin, argument): if not argument: p = state.plugins_dict['answered_other'] if p.heart_content is not None: p.heart_content = ( '<p><a class="delete_one" href="?action_destroy_student=' + state.form['answered_other'] + '"> </a>' + p.heart_content) return '' stats = statistics.question_stats() roles = set() for e in stats.all_students: try: for role in eval(utilities.read(e.roles_filename)): roles.add(role) except: pass if argument == '1': students = [ e for e in stats.all_students if e.the_number_of_good_answers < 2 ] else: students = [student.student(argument)] s = [] for e in students: if e == state.student: continue if e.filename in roles: continue s.append('%s(%s)' % (e.name, e.the_number_of_good_answers)) e.destroy() plugin.heart_content = ('<p class="deleted"><br>' + '<br>\n'.join(s)) state.question = None return ''
def highlight_file(path, lexer=None, formatter=None): """Return a syntax-highlighted string corresponding to the program contained in a file. * path is the name of the file (relative to the questions directory) * lexer (optional) is a Pygmentize lexer. By default, the lexer is guessed from the filename and content. * formatter (optional) is a Pygmentize formatter. By default, the code will be formatted in HTML on a white background. """ path = os.path.join(QUENLIG.configuration.questions, path) if not os.path.exists(path): raise ValueError("Could not find template file '{}'.".format(path)) code = utilities.read(path) try: import pygments from pygments.lexers import guess_lexer_for_filename from pygments.formatters import HtmlFormatter except ImportError: # Pygments is not available, just surround the code with <pre> return '<pre style="background: white">{}</pre>'.format( cgi.escape(code)) if lexer is None: lexer = guess_lexer_for_filename(path, code) if formatter is None: formatter = HtmlFormatter( # inline formatting directives (avoid the need for a # separate style section): noclasses=True, prestyles="background: white", cssclass="source") return pygments.highlight(code, lexer, formatter)
def __call__(self, student_answer, state=None): utilities.write('xxx.c', student_answer + '\n') error = os.system("gcc -Wall xxx.c 2>xxx.errors") error_text = utilities.read('xxx.errors') if error: return False, '<pre>' + error_text + '</pre>' f = subprocess.Popen( ["./a.out"] + self.c_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) result = f.communicate(self.c_input) ok, comment = self.children[0](result[0], state) if error_text != '': message = 'Message du compilateur : <pre>' + error_text + '</pre><hr>' else: message = '' return (ok, message + 'Ce que ce programme affiche : <pre>' + result[0] + '</pre>' + comment)
def add_gap_fill(question, template=None, template_file=None, feedback=False, highlight=False, lexer=None, formatter=None, **kwargs): """Convenience wrapper around add() to add "fill-in the gaps" questions. ``question`` is the text of the question. ``template`` is the text to fill-in. It containts directives of the form {{{expected}}}, each of them will be replaced by a gap, and a test will be generated to check that the user answers with the expected string. Alternatively, one may provide a ``template_file`` argument and the file's content will be used. ``highlight`` is a Boolean saying whether the template should be highlighted. If so, it will use the lexer and formatter passed as argument, or guess the lexer and use its own formatter. ``feedback`` is a boolean saying whether to provide feedback on incorrect answers to student. Other arguments are forwarded to ``add``. Example: add_gap_fill( name="skel-lock2", question="Fill in the gaps:", template="void f({{{int}}}) {return {{{42}}};}", highlight=True, lexer=CppLexer() ) """ if template_file is None and template is None: raise ValueError( "Please provide either template or template_file keyword argument") if template_file is not None and template is not None: raise ValueError( "Please provide either template or template_file keyword argument," " but not both") if template_file is not None: path = os.path.join(QUENLIG.configuration.questions, template_file) if not os.path.exists(path): raise ValueError( "Could not find template file '{}' in question '{}'.".format( path, kwargs['name'])) template = utilities.read(path) if highlight: H = build_highlighter(lexer, formatter, template, template_file) else: def H(x): return x new_template, tests = process_template(template, H, feedback) question += ("{{{$}}}" + # Start the answer part. """<div class="source"><pre style="background: white">""" + new_template + """</pre></div>""") tests.append(Good()) # Always True add(question=question, tests=tests, function_name='add_gap_fill', **kwargs)
command = ('ulimit -t ' + str(cpu_limit) + '\n' + 'ulimit -v ' + str(memory_limit) + '\n' + 'cd ' + directory + '\n' + 'chmod 777 . 2>/dev/null\n' + 'sudo -u nobody ' + command_line + ' 2>&1\n') f = subprocess.Popen( command, shell=True, stdin=stdin, stdout=stdout, stderr=stderr, ) f.directory = directory return f head = utilities.read('interactive.html') def send(output, text): output.write('<script>char(' + repr(text) + ')</script>') output.flush() def html(interactive): input = interactive.pipe_in output = interactive.output try: while True: send(output, input.readline()) except socket.error: interactive.stop()