From 3e4c4a359a8db73ed1e549fba7e2fb2666449eec Mon Sep 17 00:00:00 2001 From: hetangmodi-crest Date: Thu, 8 Aug 2024 15:36:04 +0530 Subject: [PATCH] chore(html): fix minor syntax and optimization --- .../generators/conf_files/conf_generator.py | 12 ++-- .../generators/file_const.py | 8 +-- .../generators/file_generator.py | 8 +-- .../generators/html_files/__init__.py | 4 +- ...t_html.py => create_alert_actions_html.py} | 63 ++++++++++--------- .../generators/html_files/html_generator.py | 8 +-- .../generators/xml_files/xml_generator.py | 10 ++- 7 files changed, 54 insertions(+), 59 deletions(-) rename splunk_add_on_ucc_framework/generators/html_files/{create_alert_html.py => create_alert_actions_html.py} (60%) diff --git a/splunk_add_on_ucc_framework/generators/conf_files/conf_generator.py b/splunk_add_on_ucc_framework/generators/conf_files/conf_generator.py index d5523e126..4d8af2973 100644 --- a/splunk_add_on_ucc_framework/generators/conf_files/conf_generator.py +++ b/splunk_add_on_ucc_framework/generators/conf_files/conf_generator.py @@ -13,11 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from abc import abstractmethod from typing import Any, Dict, Union, NoReturn - from splunk_add_on_ucc_framework.global_config import GlobalConfig - from ..file_generator import FileGenerator @@ -34,17 +31,16 @@ def __init__( super().__init__(global_config, input_dir, output_dir, **kwargs) self.conf_file = ".conf" + def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: + # parse self._global_config and set the require attributes for self + raise NotImplementedError() + def generate(self) -> Dict[str, str]: conf_files: Dict[str, str] = {} conf_files.update(self.generate_conf()) conf_files.update(self.generate_conf_spec()) return conf_files - @abstractmethod - def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: - # parse self._global_config and set the require attributes for self - raise NotImplementedError - def generate_conf(self) -> Dict[str, str]: # logic to pass the configs to template file # uses the attributes set in _set_attributes method to render the template diff --git a/splunk_add_on_ucc_framework/generators/file_const.py b/splunk_add_on_ucc_framework/generators/file_const.py index 54ae69f84..3503ce0c9 100644 --- a/splunk_add_on_ucc_framework/generators/file_const.py +++ b/splunk_add_on_ucc_framework/generators/file_const.py @@ -35,7 +35,7 @@ RedirectXml, ) -from splunk_add_on_ucc_framework.generators.html_files import AlertHtml +from splunk_add_on_ucc_framework.generators.html_files import AlertActionsHtml __all__ = ["FileClass", "GEN_FILE_LIST"] @@ -59,7 +59,7 @@ class FileClass(NamedTuple): DefaultXml, InputsXml, RedirectXml, - AlertHtml, + AlertActionsHtml, ] ] file_path: Union[str, List[str]] @@ -116,8 +116,8 @@ class FileClass(NamedTuple): ), FileClass( "_.html", - AlertHtml, + AlertActionsHtml, ["default", "data", "ui", "alerts"], - AlertHtml.__description__, + AlertActionsHtml.__description__, ), ] diff --git a/splunk_add_on_ucc_framework/generators/file_generator.py b/splunk_add_on_ucc_framework/generators/file_generator.py index d4337b5c5..64df89a47 100644 --- a/splunk_add_on_ucc_framework/generators/file_generator.py +++ b/splunk_add_on_ucc_framework/generators/file_generator.py @@ -14,7 +14,7 @@ # limitations under the License. # import logging -from abc import ABC, abstractmethod +from abc import ABC from os.path import realpath, sep from typing import Any, Dict, List, Union, NoReturn @@ -67,13 +67,11 @@ def __init__( self._gc_schema = None self._set_attributes(**kwargs) - @abstractmethod def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: - raise NotImplementedError + raise NotImplementedError() - @abstractmethod def generate(self) -> Dict[str, str]: - raise NotImplementedError + raise NotImplementedError() def _get_output_dir(self) -> str: return sep.join([realpath(self._output_dir), self._addon_name]) diff --git a/splunk_add_on_ucc_framework/generators/html_files/__init__.py b/splunk_add_on_ucc_framework/generators/html_files/__init__.py index 97a5d742a..f30fac8d3 100644 --- a/splunk_add_on_ucc_framework/generators/html_files/__init__.py +++ b/splunk_add_on_ucc_framework/generators/html_files/__init__.py @@ -14,6 +14,6 @@ # limitations under the License. # from .html_generator import HTMLGenerator -from .create_alert_html import AlertHtml +from .create_alert_actions_html import AlertActionsHtml -__all__ = ["HTMLGenerator", "AlertHtml"] +__all__ = ["HTMLGenerator", "AlertActionsHtml"] diff --git a/splunk_add_on_ucc_framework/generators/html_files/create_alert_html.py b/splunk_add_on_ucc_framework/generators/html_files/create_alert_actions_html.py similarity index 60% rename from splunk_add_on_ucc_framework/generators/html_files/create_alert_html.py rename to splunk_add_on_ucc_framework/generators/html_files/create_alert_actions_html.py index 126d63765..b9a57404f 100644 --- a/splunk_add_on_ucc_framework/generators/html_files/create_alert_html.py +++ b/splunk_add_on_ucc_framework/generators/html_files/create_alert_actions_html.py @@ -24,7 +24,7 @@ from re import search -class AlertHtml(HTMLGenerator): +class AlertActionsHtml(HTMLGenerator): __description__ = ( " Generates `alert_name.html` file based on alerts configuration present in globalConfig" " in `default/data/ui/alerts` folder." @@ -48,34 +48,39 @@ def _set_attributes(self, **kwargs: Dict[str, Any]) -> None: ) schema_content = envs["schema.content"] self._alert_settings = schema_content["modular_alerts"] - for self.alert in self._alert_settings: - self.generate_html() def generate_html(self) -> Dict[str, str]: - if self._global_config and not self._global_config.has_alerts(): + if (not self._global_config) or ( + self._global_config and not self._global_config.has_alerts() + ): return super().generate_html() - self.set_template_and_render( - template_file_path=["html_templates"], file_name="mod_alert.html.template" - ) - rendered_content = self._template.render( - mod_alert=self.alert, home_page=self._html_home - ) - text = linesep.join( - [s for s in rendered_content.splitlines() if not search(r"^\s*$", s)] - ) - file_name = f"{self.alert[ac.SHORT_NAME] + '.html'}" - file_path = self.get_file_output_path( - [ - "default", - "data", - "ui", - "alerts", - file_name, - ] - ) - self.writer( - file_name=file_name, - file_path=file_path, - content=text, - ) - return {file_name: file_path} + + alert_details: Dict[str, str] = {} + for self.alert in self._alert_settings: + self.set_template_and_render( + template_file_path=["html_templates"], + file_name="mod_alert.html.template", + ) + rendered_content = self._template.render( + mod_alert=self.alert, home_page=self._html_home + ) + text = linesep.join( + [s for s in rendered_content.splitlines() if not search(r"^\s*$", s)] + ) + file_name = f"{self.alert[ac.SHORT_NAME] + '.html'}" + file_path = self.get_file_output_path( + [ + "default", + "data", + "ui", + "alerts", + file_name, + ] + ) + self.writer( + file_name=file_name, + file_path=file_path, + content=text, + ) + alert_details.update({file_name: file_path}) + return alert_details diff --git a/splunk_add_on_ucc_framework/generators/html_files/html_generator.py b/splunk_add_on_ucc_framework/generators/html_files/html_generator.py index 2be7ed9ae..ee098234e 100644 --- a/splunk_add_on_ucc_framework/generators/html_files/html_generator.py +++ b/splunk_add_on_ucc_framework/generators/html_files/html_generator.py @@ -30,15 +30,13 @@ def __init__( ) -> None: super().__init__(global_config, input_dir, output_dir, **kwargs) - def generate(self) -> Dict[str, str]: - html_files: Dict[str, str] = {} - html_files.update(self.generate_html()) - return html_files - def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: # parse self._global_config and set the require attributes for self raise NotImplementedError() + def generate(self) -> Dict[str, str]: + return self.generate_html() + def generate_html(self) -> Dict[str, str]: # uses the attributes set in _set_attributes method to set the required attributes # uses set_template_and_render to load and render the HTML template. diff --git a/splunk_add_on_ucc_framework/generators/xml_files/xml_generator.py b/splunk_add_on_ucc_framework/generators/xml_files/xml_generator.py index 61129336c..5da721fca 100644 --- a/splunk_add_on_ucc_framework/generators/xml_files/xml_generator.py +++ b/splunk_add_on_ucc_framework/generators/xml_files/xml_generator.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from abc import abstractmethod from ..file_generator import FileGenerator from typing import Dict, Any, Union, NoReturn from splunk_add_on_ucc_framework.global_config import GlobalConfig @@ -31,16 +30,15 @@ def __init__( ) -> None: super().__init__(global_config, input_dir, output_dir, **kwargs) + def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: + # parse self._global_config and set the require attributes for self + raise NotImplementedError() + def generate(self) -> Dict[str, str]: xml_files: Dict[str, str] = {} xml_files.update(self.generate_xml()) return xml_files - @abstractmethod - def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]: - # parse self._global_config and set the require attributes for self - raise NotImplementedError() - def generate_xml(self) -> Dict[str, str]: # uses the attributes set in _set_attributes method to set the required attributes # use self.writer function to create the xml file.