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

79 lines
2.4 KiB
Python
Raw Normal View History

2024-02-26 15:33:26 +01:00
from modules.cluster import Cluster
from typing import List
import os
2024-03-05 11:01:51 +01:00
2024-02-26 15:33:26 +01:00
class Galaxy:
2024-03-05 11:01:51 +01:00
def __init__(
self,
galaxy_name: str,
json_file_name: str,
authors: List[str],
description: str,
):
2024-03-01 16:30:49 +01:00
self.galaxy_name = galaxy_name
self.json_file_name = json_file_name
2024-02-26 15:33:26 +01:00
self.authors = authors
self.description = description
2024-03-01 16:30:49 +01: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 11:01:51 +01:00
self.clusters[uuid] = Cluster(
uuid=uuid, galaxy=self, description=description, value=value, meta=meta
)
2024-03-01 16:30:49 +01:00
def write_entry(self, path):
2024-03-04 14:39:41 +01: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 16:30:49 +01: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 11:01:51 +01:00
2024-02-26 15:33:26 +01:00
def _create_metadata_entry(self):
2024-03-01 16:30:49 +01:00
entry = ""
entry += "---\n"
entry += f"title: {self.galaxy_name}\n"
2024-02-26 15:33:26 +01:00
meta_description = self.description.replace('"', "-")
2024-03-01 16:30:49 +01:00
entry += f"description: {meta_description}\n"
entry += "---\n"
return entry
2024-02-26 15:33:26 +01:00
def _create_title_entry(self):
2024-03-01 16:30:49 +01:00
entry = ""
entry += f"# {self.galaxy_name}\n"
return entry
2024-02-26 15:33:26 +01:00
def _create_description_entry(self):
2024-03-01 16:30:49 +01:00
entry = ""
entry += f"{self.description}\n"
return entry
2024-02-26 15:33:26 +01:00
def _create_authors_entry(self):
2024-03-01 16:30:49 +01:00
entry = ""
2024-02-26 15:33:26 +01:00
if self.authors:
2024-03-01 16:30:49 +01: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 15:33:26 +01:00
for author in self.authors:
2024-03-01 16:30:49 +01:00
entry += f" |{author}|\n"
return entry
2024-02-26 15:33:26 +01:00
2024-03-01 16:30:49 +01:00
def _create_clusters_entry(self):
entry = ""
for cluster in self.clusters.values():
entry += cluster.generate_entry()
2024-03-05 11:01:51 +01:00
return entry