Skip to content
Snippets Groups Projects

Resolve "front-matter linter"

Merged Remi PLANEL requested to merge front-matter-linter into main
1 file
+ 3
1
Compare changes
  • Side-by-side
  • Inline
import typer
from typing_extensions import Annotated
from typing import Optional, List
from pathlib import Path
from pydantic import BaseModel, ValidationError
from pydantic_yaml import parse_yaml_raw_as, to_yaml_str
import frontmatter
from enum import Enum
from rich.console import Console
from rich import print as rprint
from rich.layout import Layout
from rich.panel import Panel
console = Console()
app = typer.Typer()
class LayoutEnum(str, Enum):
article = "article"
db = "db"
class TableArticle(BaseModel):
doi: str
class TableColumns(BaseModel):
article: TableArticle
Sensor: str
Activator: str
Effector: str
PFAM: str
class RelevantAbstract(BaseModel):
doi: str
class FrontMatter(BaseModel):
title: str
layout: LayoutEnum
tableColumns: TableColumns
relevantAbstracts: List[RelevantAbstract]
contributors: List[str]
@app.command()
def lint(
file: Annotated[
Path,
typer.Option(
exists=False,
file_okay=True,
writable=True,
),
],
):
console.rule(f"[bold blue]{file.name}", style="blue")
with open(file) as f:
metadata, _ = frontmatter.parse(f.read())
# print(metadata)
try:
FrontMatter.model_validate(metadata)
except ValidationError as exc:
# print(repr(exc.errors()[0]["type"]))
# print(repr(exc))
# print(exc.errors())
# pprint(exc.errors(), expand_all=True)
for err in exc.errors():
console.print(
f"[red]{err['msg']} : {err['type']} {' -> '.join(err['loc'])}"
)
else:
console.print("[green] Everything is alright")
Loading