def _load_template(self, template_name, handler_input, **kwargs): # type: (str, HandlerInput, Any) -> TemplateContent """Iterate through the list of loaders and load the given template. :param template_name: name of response template :type template_name: str :param handler_input: Handler Input instance with Request Envelope containing Request. :type handler_input: :py:class:`ask_sdk_core.handler_input.HandlerInput` :param kwargs: Additional keyword arguments for loader and renderer. :return: Template Content object :rtype: :py:class:`ask_sdk_core.view_resolvers.TemplateContent` :raises: :py:class:`ask_sdk_core.exceptions.TemplateResolverException` if none of the loaders failed to load the template or any exception raised during loading of the template. """ for template_loader in self.template_loaders: try: template_content = template_loader.load( handler_input, template_name, **kwargs) if template_content is not None: return template_content except Exception as e: raise TemplateLoaderException( "Failed to load the template:" " {} using {} with error : {}".format( template_name, template_loader, str(e))) raise TemplateLoaderException( "Unable to load template: {} using " "provided loaders.".format(template_name))
def load(self, handler_input, template_name, **kwargs): # type: (HandlerInput, str, Any) -> Optional[TemplateContent] """Loads the given input template into a TemplateContent object. This function takes in handlerInput and template_name as args and iterate over generated path combinations obtained from enumerator and find the absolute file path of the template and loads its content as a string to :py:class:`ask_sdk_core.view_resolvers.TemplateContent` object.In optional keyword arguments we can pass the file extension of the template to be loaded. :param handler_input: Handler Input instance with Request Envelope containing Request. :type handler_input: :py:class:`ask_sdk_core.handler_input.HandlerInput` :param template_name: Template name to be loaded :type template_name: str :param **kwargs: Optional arguments that loader takes. :return: (optional) TemplateContent :rtype: :py:class:`ask_sdk_core.view_resolvers.TemplateContent` :raises: :py:class:`ask_sdk_core.exceptions.TemplateResolverException` if loading of the template fails and ValueError if template_name is null """ template_name = assert_not_null(attribute=template_name, value='Template Name') try: file_extension = kwargs.get('file_ext', None) for file_path in self.enumerator.generate_combinations( handler_input=handler_input, template_name=template_name): file_path = append_extension_if_not_exists( file_path, file_extension) abs_file_path = os.path.join(self.dir_path, file_path) cache_content = self.template_cache.get(key=abs_file_path) if cache_content is None: if os.path.exists(abs_file_path): with io.open(abs_file_path, mode="r", encoding=self.encoding) as f: template_content = TemplateContent( content_data=f.read().encode(self.encoding), encoding=self.encoding) self.template_cache.put( key=abs_file_path, template_content=template_content) return template_content else: return cache_content return None except Exception as e: raise TemplateLoaderException("Failed to load the template : {} " "error : {}".format( template_name, str(e)))
def test_exceptions_raised_in_load(self): self.test_enumerator.generate_combinations.side_effect = ( TemplateLoaderException("test enumeration exception")) with self.assertRaises(TemplateLoaderException) as exc: _loader = self.test_loader.load( handler_input=self.test_handler_input, template_name=self.test_template_name) self.assertEqual( "Failed to load the template : test " "error : test enumeration exception", str(exc.exception), "FileSystemLoader did not raise TemplateResolverException" "when enumeration throws error")
def test_process_template_raise_exception_at_render(self): with self.assertRaises(TemplateRendererException) as exc: self.test_loader.load.return_value = self.test_template_content self.test_renderer.render.side_effect = TemplateLoaderException( "Renderer Error") self.test_template_factory.process_template( template_name=self.test_template_name, data_map=self.test_data_map, handler_input=self.test_handler_input) self.assertEqual( "Failed to render template: {} using {} with error: " "{}".format(self.test_template_content, self.test_renderer, "Renderer Error"), str(exc.exception), "TemplateFactory did not raise " "TemplateResolverException if none of provided " "loaders were unable to load the templates.")
def test_process_template_raise_exception_at_load(self): with self.assertRaises(TemplateLoaderException) as exc: self.test_loader.load.side_effect = TemplateLoaderException( "Test Error") self.test_template_factory.process_template( template_name=self.test_template_name, data_map=self.test_data_map, handler_input=self.test_handler_input) self.assertEqual( "Failed to load the template: {} using {} with error " ": {}".format(self.test_template_name, self.test_loader, "Test Error"), str(exc.exception), "TemplateFactory did not raise " "TemplateResolverException if none" " of provided loaders were unable" " to load the templates.")