def transform_old_cld_file(filepath: Path): """ Transform .cld file to new format. NB If file is in the wrong location, or not in class data at all, a folder with the class name will be created in class_data. :param filepath: Path :return: None """ if not filepath.exists(): print(f'File {filepath} does not exist.') return try: old_class_data = load_from_json_file(filepath) # Handle corrupt or mal-formatted JSON files. except json.decoder.JSONDecodeError: print(f'Something went wrong with decoding {filepath}:\n' f'The file might be corrupted or is not a supported format.') return if data_is_new_format(old_class_data): print(f'It looks like {filepath.name} is already in the new format.') return class_name = filepath.stem new_class = transform_data(class_name, old_class_data) new_class_data_path_name = write_classlist_to_file(new_class) print(f'Transformed {new_class.name} data file ' f'to new data format in {new_class_data_path_name}')
def load_chart_data(chart_data_path: str) -> dict: """ Load class data from chart data ('.cdf') file. :param chart_data_path: Path or str :return: dict """ return load_from_json_file(chart_data_path)
def load_class_data(class_name: str): """ Load class data from a class data ('.cld') file. Data will be a dict with format: keys: student name values: list currently only containing the avatar filename/None. :param class_name: str :return: dict """ class_data_filename = class_name + CLASSLIST_DATA_FILE_TYPE classlist_data_path = CLASSLIST_DATA_PATH.joinpath(class_name, class_data_filename) class_data_dict = load_from_json_file(classlist_data_path) return class_data_dict
def test_load_from_json_file(self): with patch('dionysus_app.file_functions.open', mock_open(read_data=self.test_file_json_data_to_convert)): assert load_from_json_file( self.mock_file_path) == self.converted_json_data
def test_load_from_json_file(self, json_file_data, loaded_object): mock_file_path = Path('test_file_path') with patch('dionysus_app.file_functions.open', mock_open(read_data=json_file_data)): assert load_from_json_file(mock_file_path) == loaded_object