async def render_json(storyboard: Storyboard): # storyboard = json.loads(payload) render_job = Job(layout=LayoutName.EASY_LAYOUT.value, storyboard=storyboard) render_job_worker.run_job(render_job) d = render_job.to_dict() return Response(content=json.dumps(d), media_type="application/json")
def test_compile_pdf_tex_file_not_found(self): project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) job.tex_file_path = "/no/valid/tex/file.tex" error_message = f"The .tex file: {job.tex_file_path} cannot found!" CompileTexStep.run(job) self.assertEqual(job.status, Status.COMPILE_PDF_ERROR) self.assertEqual(job.status_data["message"], error_message) remove_files(job.tex_file_path.replace(".tex", ""))
def run_job(job: Job, steps: List[Step] = None): if not steps: job.status_data[ "job_worker_error"] = "The specified list of steps, is empty or None. The default list will used instead!" steps = JobWorker.steps for step in steps: if job.status is Status.VALID: step.run(job) if job.status is Status.VALID: job.step = StepType.FINISHED
def test_compile_pdf_cannot_compile(self): project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) error_message = f"The .pdf file cannot compiled with the tex file." base_file_name = "tex_file_not_found" base_file_path = os.path.join(job.project.output_directory, base_file_name) job.tex_file_path = base_file_path + ".tex" write_file(job.tex_file_path, "nothing is in here!") CompileTexStep.run(job) self.assertEqual(job.status, Status.COMPILE_PDF_ERROR) self.assertEqual(job.status_data["message"], error_message) self.assertIsNotNone(job.status_data["log_file"]) remove_files(base_file_path)
def run(job: Job): if job.status is not Status.VALID: return job.step = StepType.VALIDATE_LAYOUT if hasattr(LayoutNameReverse, job.layout): return hasattr(LayoutName, getattr(LayoutNameReverse, job.layout).value) return False
def run(job: Job): # TODO add a regex matching for paths and if it's a path, check if the path exists if job.status is not Status.VALID: return job.step = StepType.VALIDATE_DATA required_data = getattr(Layouts, job.layout).value.get_required_frame_data() for index, frame in enumerate(job.project.storyboard.frames): for data in required_data: if not frame.get(data, False): job.status = Status.INVALID_DATA if not job.status_data.get("missing_data", False): job.status_data["missing_data"] = dict() job.status_data["missing_data"][str(index)] = missing_data.format( str(index), data ) else: type_a = type(frame.get(data)) type_b = required_data.get(data) if type_a is not type_b: job.status = Status.INVALID_DATA if not job.status_data.get("wrong_data_type", False): job.status_data["wrong_data_type"] = dict() job.status_data["wrong_data_type"][ # the structure with a number as string in the dict is dirty # todo Change number as string inside the dict! str(index) ] = wrong_type.format( index, data, type_a.__name__, type_b.__name__ )
def test_run_valid_frame_data(self): project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) for x in range(2): job.project.storyboard.frames.append( dict(image=sample_image_path, image_description="image_description")) FrameDataValidationStep.run(job) self.assertEqual(job.status, Status.VALID)
def test_compile_pdf_valid(self): project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) generate_example_tex_file(job) CompileTexStep.run(job) self.assertEqual(job.status, Status.VALID) self.assertIsNotNone(job.pdf_file_path) self.assertTrue(os.path.exists(job.pdf_file_path)) remove_files(job.tex_file_path.replace(".tex", ""))
def run(job: Job): if job.status is not Status.VALID: return job.step = StepType.COMPILE_TEX if not os.path.exists(job.tex_file_path): job.status = Status.COMPILE_PDF_ERROR job.status_data = dict( message=f"The .tex file: {job.tex_file_path} cannot found!") return base_file_path = f"{job.tex_file_path.replace('.tex', '')}" log_file_path = base_file_path + ".log" pdf_file_path = base_file_path + ".pdf" cmd = [ f"{latex_compiler}", f"-output-directory={job.project.output_directory}", "-interaction=nonstopmode", f"{job.tex_file_path}", ] subprocess.run(cmd, stdout=subprocess.PIPE) if os.path.exists(pdf_file_path): job.pdf_file_path = pdf_file_path else: job.status = Status.COMPILE_PDF_ERROR with open(log_file_path, "r") as log_file: log_file_content = log_file.read() job.status_data = dict( message=f"The .pdf file cannot compiled with the tex file.", log_file=log_file_content, )
def test_run_data_is_missing(self): error_layout = "Frame_{}: {} is missing" project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) job.project.storyboard.frames.append(dict(image="path_to_file")) job.project.storyboard.frames.append( dict(image_description="image_description")) FrameDataValidationStep.run(job) self.assertEqual(job.status, Status.INVALID_DATA) self.assertEqual( job.status_data["missing_data"]["0"], error_layout.format(0, "image_description"), ) self.assertEqual(job.status_data["missing_data"]["1"], error_layout.format(1, "image"))
def test_run_data_mixed_errors(self): wrong_type_error = "Frame_{}: {} is from type {} instead of {}" missing_data_error = "Frame_{}: {} is missing" project = generate_project() job = Job(layout=LayoutName.EASY_LAYOUT.value, project=project) job.project.storyboard.frames.append(dict(image=120)) job.project.storyboard.frames.append( dict(image="path_to_file", image_description=120)) FrameDataValidationStep.run(job) self.assertEqual(job.status, Status.INVALID_DATA) self.assertEqual( job.status_data["missing_data"]["0"], missing_data_error.format(0, "image_description"), ) self.assertEqual( job.status_data["wrong_data_type"]["1"], wrong_type_error.format(1, "image_description", int.__name__, str.__name__), )
def test_run_file_already_exists(self): GenerateTexFileStep.get_file_name = get_tex_file_name job = Job( layout=LayoutName.EASY_LAYOUT.value, project=generate_sample_project() ) for x in range(2): job.project.storyboard.frames.append( dict(image=sample_image_path, image_description="image_description") ) tex_file_path = os.path.join( job.project.output_directory, get_tex_file_name(job.project.storyboard.title), ) write_file(tex_file_path, "nothing special") GenerateTexFileStep.run(job) self.assertEqual(job.status, Status.GENERATE_TEX_ERROR) self.assertEqual( job.status_data["message"], f"Cannot save the .tex file, path {tex_file_path} already exists", ) clear_output_directory(job)
def test_run_valid(self): GenerateTexFileStep.get_file_name = get_tex_file_name job = Job( layout=LayoutName.EASY_LAYOUT.value, project=generate_sample_project() ) GenerateTexFileStep.run(job) expected_tex_file_path = os.path.join( job.project.output_directory, get_tex_file_name(job.project.storyboard.title), ) self.assertEqual(job.status, Status.VALID) self.assertTrue(os.path.exists(job.tex_file_path)) file_content = load_file_as_string(job.tex_file_path) self.assertEqual(job.tex_file_path, expected_tex_file_path) self.assertEqual( file_content, expected_file_content.replace("%*imagepath*", job.project.images_directory), ) clear_output_directory(job)
def generate_job(): return Job(layout=LayoutName.EASY_LAYOUT.value, project=generate_sample_project())
def test_run_with_invalid_layout(self): job = Job(layout="SuperHeroLayout", project=generate_sample_project()) self.assertFalse(LayoutValidationStep.run(job))
def test_run_with_valid_layout(self): job = Job(layout=LayoutName.EASY_LAYOUT.value, project=generate_sample_project()) self.assertTrue(LayoutValidationStep.run(job))