Skip to content

Commit

Permalink
chore(html): fix minor syntax and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hetangmodi-crest committed Aug 8, 2024
1 parent cfac087 commit 3e4c4a3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions splunk_add_on_ucc_framework/generators/file_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -59,7 +59,7 @@ class FileClass(NamedTuple):
DefaultXml,
InputsXml,
RedirectXml,
AlertHtml,
AlertActionsHtml,
]
]
file_path: Union[str, List[str]]
Expand Down Expand Up @@ -116,8 +116,8 @@ class FileClass(NamedTuple):
),
FileClass(
"_.html",
AlertHtml,
AlertActionsHtml,
["default", "data", "ui", "alerts"],
AlertHtml.__description__,
AlertActionsHtml.__description__,
),
]
8 changes: 3 additions & 5 deletions splunk_add_on_ucc_framework/generators/file_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions splunk_add_on_ucc_framework/generators/html_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 3e4c4a3

Please sign in to comment.