def test_run_apifile_modified(tmpdir): c = {'a': 'b', 'c': 'd', 'state': {'nodes': 0}} apifile = tmpdir.join('api_file.json') apifile.write_text(j_dump(c), 'utf-8') nodelist = tmpdir.join('nodelist.json') nodelist.write_text(j_dump({ 'version': 1, 'nodes': [ {'status': {'clients': 23, 'online': True}}, {'status': {'clients': 42, 'online': False}} ], 'updated_at': 'never' }), 'utf-8') side = tmpdir.join('a.json') car = tmpdir.join('c.yaml') assert tmpdir.listdir() == [apifile, nodelist] assert run([ str(apifile), '-n', str(nodelist), '-s', str(side), str(car) ]) is False assert sorted(tmpdir.listdir()) == sorted([side, apifile, car, nodelist]) assert j_load(side.read_text('utf-8')) == 'b' assert y_load(car.read_text('utf-8')) == 'd' c['state']['nodes'] = 1 assert j_load(apifile.read_text('utf-8')) == c assert tmpdir.remove() is None
def save_tutorial_data(self, tutorials): path = p_join(self._meta_path, f'03_tutorials.json') with open(path, 'w') as fp: j_dump({k: v.to_json() for k, v in tutorials.items()}, fp, indent=4)
def save_channel_json(dict_channel, dirname): jsons_dir = dirname + "jsons/" full_path = jsons_dir + "/channel_info.json" fixed_dict = dict_channel.copy() with open(full_path, 'w') as _file: fixed_dict = dict_recursive_format(fixed_dict) j_dump(fixed_dict, _file, ensure_ascii=False, indent=4)
def dumpJSON(cls, filename, data): with open(filename, "w") as fp: j_dump(data, fp, separators=(',', ':'), cls=CustomJSONEncoder, sort_keys=False, indent=4)
def save_students(self, tutorial_id, students): directory = ensure_folder_exists(p_join(self._meta_path, "students")) path = p_join(directory, f'students_{tutorial_id}.json') with open(path, 'w') as fp: out_data = [ student.to_json_dict() for student in students[tutorial_id] ] j_dump(out_data, fp, indent=4)
def write_json(self, dict_, file_path): print(file_path) self.ensure_dir(file_path) j_dump(dict_, co_open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4)
def save_exchanged_students(self, students, mode): if mode == 'imported': file_name = "imported_students.json" elif mode == 'exported': file_name = "exported_students.json" else: raise ValueError(f"Unknown mode '{mode}' (storage.py: save_exchanged_students)") directory = ensure_folder_exists(p_join(self._meta_path, "students")) path = p_join(directory, file_name) with open(path, 'w') as fp: j_dump(students, fp, indent=4)
def update_exercise_meta(self, muesli, exercise_number): tutorial_id = self.my_tutorial_ids[0] exercise_id = muesli.get_exercise_id(tutorial_id, self.muesli_data.exercise_prefix, exercise_number) max_credits = muesli.get_max_credits_of(tutorial_id, exercise_id) max_credits = [[f'{self.muesli_data.feedback.task_prefix}{i + 1}', v] for i, v in enumerate(max_credits)] data = { "title": f'{self.muesli_data.feedback.exercise_title}{exercise_number}', "exercise_id": exercise_id, "max_credits": max_credits, } path = self._get_exercise_meta_path(exercise_number) with open(path, 'w', encoding='utf-8') as fp: j_dump(data, fp)
def download_submissions_of_my_students(self, moodle: MoodleSession, exercise_number, printer): printer.inform('Connecting to Moodle and collecting data.') printer.inform('This may take a few seconds.') submissions = moodle.find_submissions( self.moodle_data.course_id, self.moodle_data.exercise_prefix, exercise_number, printer ) printer.inform(f"Found a total of {len(submissions)} for '{self.moodle_data.exercise_prefix}{exercise_number}'") my_students = self.my_students my_students = {student.moodle_student_id: student for student in my_students} submissions = [submission for submission in submissions if submission.moodle_student_id in my_students] printer.inform(f"Found {len(submissions)} submissions for me") folder = os.path.join( self.storage_config.root, self.storage_config.submission_root, f'{self.storage_config.exercise_template}{exercise_number}', self.storage_config.raw_folder ) ensure_folder_exists(folder) for submission in submissions: with open(os.path.join(folder, submission.file_name), 'wb') as fp: try: printer.inform(f"Downloading submission of {my_students[submission.moodle_student_id]} ... ", end='') moodle.download(submission.url, fp) printer.confirm('[Ok]') except Exception as e: printer.error('[Err]') printer.error(str(e)) with open(os.path.join(folder, "meta.json"), 'w') as fp: try: printer.inform(f'Write meta data ... ', end='') j_dump([s.__dict__ for s in submissions], fp, indent=4) printer.confirm('[Ok]') except Exception as e: printer.error('[Err]') printer.error(str(e))
def test_run_dump_apifile(tmpdir, capsys): c = {'a': 'b', 'c': 'd'} apifile = tmpdir.join('api_file.json') apifile.write_text(j_dump(c), 'utf-8') assert run([str(apifile), '-d']) is True out, err = capsys.readouterr() assert pformat(c) in out assert err == '' assert tmpdir.remove() is None
def test_run_apifile_not_modified(tmpdir): c = {'a': 'b', 'c': 'd'} apifile = tmpdir.join('api_file.json') apifile.write_text(j_dump(c), 'utf-8') nodelist = tmpdir.join('nodelist.json') nodelist.write_text(j_dump({ 'will': {'not': {'be': 'recognized'}} }), 'utf-8') side = tmpdir.join('wrong_extension.txt') car = tmpdir.join('double..dot.yaml') assert run([ str(apifile), '-n', str(nodelist), '-s', str(side), str(car) ]) is True assert tmpdir.remove() is None
def save_meta_to_folder(self, directory): ensure_folder_exists(directory) meta_data = dict() meta_data["credits_per_task"] = self._credits_per_task meta_data["names"] = [ student.muesli_name for student in self._students ] meta_data["muesli_ids"] = [ student.muesli_student_id for student in self._students ] meta_data["muesli_mails"] = [ student.muesli_mail for student in self._students ] with open(os.path.join(directory, "meta.json"), 'w', encoding="utf-8") as fp: j_dump(meta_data, fp, indent=4) feedback_path = os.path.join(directory, self._file_name) with open(feedback_path, 'w', encoding="utf-8") as fp: for line in self._feedback: print(line, file=fp)
def dump_struct(content, as_yaml=False): ''' Contextmanager to pickle either *json* or *yaml* into a string :param content: data to pickle :param as_yaml: output as *yaml* instead of *json* :yield str: pickled ``content`` ''' try: yield y_dump(content, indent=4, default_flow_style=False) if as_yaml else j_dump( content, indent=2, sort_keys=True) except TypeError: yield None
def dump_struct(content, as_yaml=False): ''' Contextmanager to pickle either *json* or *yaml* into a string :param content: data to pickle :param as_yaml: output as *yaml* instead of *json* :yield str: pickled ``content`` ''' try: yield y_dump( content, indent=4, default_flow_style=False ) if as_yaml else j_dump( content, indent=2, sort_keys=True ) except TypeError: yield None
def test_dump_struct_json(): for tj in ['', 'a', {}, {'a': 'b'}]: with dump_struct(tj, as_yaml=False) as t: assert t == j_dump( tj, indent=2, sort_keys=True )
def save_tutorial_ids(self, ids, mode='my'): path = p_join(self._meta_path, f'02_{mode}_ids.json') with open(path, 'w') as fp: j_dump(ids, fp, indent=4)
def save_my_name(self, my_name): path = p_join(self._meta_path, f'01_my_name.json') with open(path, 'w') as fp: j_dump(my_name, fp, indent=4)
def save_presented_scores(self, presented_score): directory = ensure_folder_exists(p_join(self._meta_path, "students")) path = p_join(directory, "presented_information.json") with open(path, 'w') as fp: j_dump(presented_score, fp, indent=4)
def save_messages_json(events, path): with open(path, 'w') as _file: fixed_dict = events.to_dict() fixed_dict = dict_recursive_format(fixed_dict) j_dump(fixed_dict, _file, ensure_ascii=False, indent=4)
def save_student_name_matches(self, student_name_matches): path = p_join(self._meta_path, f'04_student_name_matches.json') with open(path, 'w') as fp: j_dump(student_name_matches, fp)
def test_dump_struct_json(): for tj in ['', 'a', {}, {'a': 'b'}]: with dump_struct(tj, as_yaml=False) as t: assert t == j_dump(tj, indent=2, sort_keys=True)
def write_json(dict_, file_path): j_dump(dict_, co_open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4)