From 045b7d6c28a820fa7d5548d068b602d03fb29160 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 3 Dec 2024 14:01:00 +0100 Subject: [PATCH] api/modules: seperate frontmatter parsing from extraction --- pkgs/clan-cli/clan_cli/api/disk.py | 2 +- pkgs/clan-cli/clan_cli/api/modules.py | 38 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/api/disk.py b/pkgs/clan-cli/clan_cli/api/disk.py index 07427f2ff..53b3e9427 100644 --- a/pkgs/clan-cli/clan_cli/api/disk.py +++ b/pkgs/clan-cli/clan_cli/api/disk.py @@ -184,7 +184,7 @@ def set_machine_disk_schema( raise ClanError(msg, description=f"Valid options: {ph.options}") header = f"""# --- -# schema = "{schema_name}"; +# schema = "{schema_name}" # --- # This file was automatically generated! # CHANGING this configuration requires wiping and reinstalling the machine diff --git a/pkgs/clan-cli/clan_cli/api/modules.py b/pkgs/clan-cli/clan_cli/api/modules.py index 5ee5e942f..31ef7541c 100644 --- a/pkgs/clan-cli/clan_cli/api/modules.py +++ b/pkgs/clan-cli/clan_cli/api/modules.py @@ -59,19 +59,12 @@ class Frontmatter: raise ValueError(msg) -def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatter, str]: +def parse_frontmatter(readme_content: str) -> tuple[dict[str, Any] | None, str]: """ - Extracts TOML frontmatter from a README file content. - - Parameters: - - readme_content (str): The content of the README file as a string. - - Returns: - - str: The extracted frontmatter as a string. - - str: The content of the README file without the frontmatter. + Extracts TOML frontmatter from a string Raises: - - ValueError: If the README does not contain valid frontmatter. + - ClanError: If the toml frontmatter is invalid """ # Pattern to match YAML frontmatter enclosed by triple-dashed lines frontmatter_pattern = r"^---\s+(.*?)\s+---\s?+(.*)$" @@ -92,11 +85,32 @@ def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatte msg = f"Error parsing TOML frontmatter: {e}" raise ClanError( msg, - description=f"Invalid TOML frontmatter. {err_scope}", + description="Invalid TOML frontmatter", location="extract_frontmatter", ) from e - return Frontmatter(**frontmatter_parsed), remaining_content + return frontmatter_parsed, remaining_content + return None, readme_content + + +def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatter, str]: + """ + Extracts TOML frontmatter from a README file content. + + Parameters: + - readme_content (str): The content of the README file as a string. + + Returns: + - str: The extracted frontmatter as a string. + - str: The content of the README file without the frontmatter. + + Raises: + - ValueError: If the README does not contain valid frontmatter. + """ + frontmatter_raw, remaining_content = parse_frontmatter(readme_content) + + if frontmatter_raw: + return Frontmatter(**frontmatter_raw), remaining_content # If no frontmatter is found, raise an error msg = "Invalid README: Frontmatter not found."