def load_from_path(path: str): if not os.path.exists(path): raise FileNotFoundError storyboard: Storyboard if os.path.isdir(path): tmp_storyboard_path = os.path.join(path, "storyboard.json") if not os.path.exists(tmp_storyboard_path): raise StoryboardFileNotFound( f"No storyboard file found in {tmp_storyboard_path}." f"Please create a new project or try another path.") storyboard_path = tmp_storyboard_path else: storyboard_path = path path = get_directory_of_file(path) file_type = os.path.splitext(storyboard_path)[1] if file_type == ".json": file_string = load_file_as_string(storyboard_path) file_json = json.loads(file_string) if not Storyboard.is_valid_storyboard_dict(file_json): raise InvalidStoryboard( f"The chosen storyboard is not valid, please check, that all required attributes are set!" ) else: raise StoryboardFileNotFound( f"Storyboard file if from type {file_type}, " f"instead of .json. Please check that the file has the ending .json and is a valid storyboard file!" ) storyboard: Storyboard = Storyboard.generate_from_file(storyboard_path) return Project(path=path, storyboard=storyboard)
def test_save_project_current_project(self): project_handler = ProjectHandler() project = generate_sample_project() project_handler.current_project = project storyboard = Storyboard.generate_from_file( os.path.join(project.path, "storyboard.json")) self.assertEqual(project.storyboard, storyboard) project.storyboard.title = "Is now different" project_handler.save_project() storyboard_after = Storyboard.generate_from_file( os.path.join(project.path, "storyboard.json")) self.assertEqual(project.storyboard, storyboard_after) self.assertNotEqual(storyboard_after, storyboard) remove_project(project_handler.current_project.path)
def test_save_project(self): generate_temporary_project_folder() expected_storyboard = Storyboard(title="test storyboard", author="Bernhard Brueckenpfeiler", frames=[]) project = Project.load_from_path(project_folder_path) project.storyboard = expected_storyboard project.save_project() storyboard = Storyboard.generate_from_file( os.path.join(project_folder_path, "storyboard.json")) self.assertEquals(storyboard, expected_storyboard) remove_temporary_project_folder()
def generate_new_project(path: str): if not os.path.exists(path) or len(os.listdir(path)) is not 0: raise DirectoryIsNotEmpty( f"The directory {path} is not empty or does not exists! " f"Please create an empty directory for the project.") storyboard_path = os.path.join(path, "storyboard.json") if not os.path.exists(storyboard_path): copyfile( os.path.join(sample_data_path, "sample_storyboard.json"), storyboard_path, ) os.mkdir(os.path.join(path, "output")) os.mkdir(os.path.join(path, "images")) storyboard: Storyboard = Storyboard.generate_from_file(storyboard_path) return Project(path=path, storyboard=storyboard)
def test_generate_from_file_valid(self): json_string = json.dumps( dict( title="test storyboard", author="Bernhard Brueckenpfeiler", frames=[ dict( image=os.path.join(sample_data_path, "sample_image.jpg"), image_description="image_description", ) ], )) storyboard_file_path = os.path.join( "/tmp/", f"temporary_test_file_{str(hash('test_directory'))}.json") write_file(storyboard_file_path, json_string) storyboard = Storyboard.generate_from_file(storyboard_file_path) self.assertEqual(storyboard, generate_storyboard())