misp-galaxy/tools/mkdocs/modules/galaxy.py

83 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-02-26 14:33:26 +00:00
from modules.cluster import Cluster
from typing import List
import os
2024-03-05 10:01:51 +00:00
2024-02-26 14:33:26 +00:00
class Galaxy:
2024-03-05 10:01:51 +00:00
def __init__(
self,
galaxy_name: str,
json_file_name: str,
authors: List[str],
description: str,
):
2024-03-01 15:30:49 +00:00
self.galaxy_name = galaxy_name
self.json_file_name = json_file_name
2024-02-26 14:33:26 +00:00
self.authors = authors
self.description = description
2024-03-01 15:30:49 +00:00
self.clusters = {} # Maps uuid to Cluster objects
def add_cluster(self, uuid, description, value, meta):
if uuid not in self.clusters:
2024-03-05 10:01:51 +00:00
self.clusters[uuid] = Cluster(
uuid=uuid, galaxy=self, description=description, value=value, meta=meta
)
2024-03-01 15:30:49 +00:00
def write_entry(self, path):
2024-03-04 13:39:41 +00:00
galaxy_path = os.path.join(path, f"{self.json_file_name}".replace(".json", ""))
if not os.path.exists(galaxy_path):
os.mkdir(galaxy_path)
with open(os.path.join(galaxy_path, "index.md"), "w") as index:
2024-03-01 15:30:49 +00:00
index.write(self.generate_entry())
def generate_entry(self):
entry = ""
entry += self._create_metadata_entry()
entry += self._create_title_entry()
entry += self._create_description_entry()
entry += self._create_authors_entry()
entry += self._create_clusters_entry()
return entry
2024-03-05 10:01:51 +00:00
2024-02-26 14:33:26 +00:00
def _create_metadata_entry(self):
2024-03-01 15:30:49 +00:00
entry = ""
entry += "---\n"
entry += f"title: {self.galaxy_name}\n"
2024-02-26 14:33:26 +00:00
meta_description = self.description.replace('"', "-")
2024-03-01 15:30:49 +00:00
entry += f"description: {meta_description}\n"
entry += "---\n"
return entry
2024-02-26 14:33:26 +00:00
def _create_title_entry(self):
2024-03-01 15:30:49 +00:00
entry = ""
entry += f"[Hide Navigation](#){{ .md-button #toggle-navigation }}\n"
entry += f"[Hide TOC](#){{ .md-button #toggle-toc }}\n"
entry += f"<div class=\"clearfix\"></div>\n"
2024-03-14 09:51:22 +00:00
entry += f"[Edit :material-pencil:](https://github.com/MISP/misp-galaxy/edit/main/clusters/{self.json_file_name}){{ .md-button }}\n"
2024-03-01 15:30:49 +00:00
entry += f"# {self.galaxy_name}\n"
return entry
2024-02-26 14:33:26 +00:00
def _create_description_entry(self):
2024-03-01 15:30:49 +00:00
entry = ""
entry += f"{self.description}\n"
return entry
2024-02-26 14:33:26 +00:00
def _create_authors_entry(self):
2024-03-01 15:30:49 +00:00
entry = ""
2024-02-26 14:33:26 +00:00
if self.authors:
2024-03-01 15:30:49 +00:00
entry += f"\n"
entry += f'??? info "Authors"\n'
entry += f"\n"
entry += f" | Authors and/or Contributors|\n"
entry += f" |----------------------------|\n"
2024-02-26 14:33:26 +00:00
for author in self.authors:
2024-03-01 15:30:49 +00:00
entry += f" |{author}|\n"
return entry
2024-02-26 14:33:26 +00:00
2024-03-01 15:30:49 +00:00
def _create_clusters_entry(self):
entry = ""
for cluster in self.clusters.values():
entry += cluster.generate_entry()
2024-03-05 10:01:51 +00:00
return entry