Skip to content

Commit

Permalink
Refactor flight report infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
lkluft committed Jul 16, 2024
1 parent a6391fb commit 2160a58
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 118 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ _build
__pycache__/
*.py[cod]
.ipynb_checkpoints

# Auto-generated files
orcestra_book/operation.md
4 changes: 2 additions & 2 deletions orcestra_book/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
title: ORCESTRA
author: The ORCESTRA Community
logo: logos/orcestra.png
exclude_patterns: ["_templates/*"]

# Force re-execution of notebooks on each build.
# See https://jupyterbook.org/content/execute.html
Expand All @@ -30,8 +31,7 @@ sphinx:
local_extensions:
apastyle: _ext/
bracket_citation_style: _ext/
category_role: _ext/
create_flight_table: _ext/
flight_reports: _ext/
config:
bibtex_default_style: myapastyle
bibtex_reference_style: author_year_round
Expand Down
63 changes: 0 additions & 63 deletions orcestra_book/_ext/category_role.py

This file was deleted.

51 changes: 0 additions & 51 deletions orcestra_book/_ext/create_flight_table.py

This file was deleted.

106 changes: 106 additions & 0 deletions orcestra_book/_ext/flight_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3
import os
import pathlib
import subprocess
from datetime import datetime
from docutils import nodes
from functools import lru_cache, partial

import yaml
from jinja2 import Template
from sphinx.util.docutils import SphinxRole


@lru_cache
def load_frontmatter(path, derive_flight=False):
with open(path, "r") as fp:
frontmatter = next(yaml.safe_load_all(fp))

if derive_flight:
takeoff = frontmatter["takeoff"]
landing = frontmatter["landing"]

frontmatter["expr_date"] = takeoff.strftime("%d %B %Y")
frontmatter["expr_takeoff"] = takeoff.strftime("%X")
frontmatter["expr_landing"] = landing.strftime("%X")
frontmatter["expr_categories"] = map(
lambda s: f"{{cat}}`{s}`", frontmatter.get("categories", [])
)

return frontmatter


@lru_cache
def create_badge(cat_id):
"""Return an HTML node based on a category id."""
with open("orcestra_book/flight_reports/categories.yaml", "r") as fp:
cat_tier = {
key: attrs["tier"]
for key, attrs in yaml.safe_load(fp)["categories"].items()
}.get(cat_id, "unknown")

span = f'<span class="badge cat-{cat_tier} cat-{cat_id}">{cat_id}</span>'
href = f'<a href="https://orcestra-campaign.org/search.html?q={cat_id}">{span}</a>'
node = nodes.raw(
text=href,
format="html",
)

return node


class CategoryRole(SphinxRole):
def run(self):
node = create_badge(self.text)

return [node], []


class BadgesRole(SphinxRole):
def run(self):
fm = load_frontmatter(self.env.doc2path(self.env.docname))
categories = fm.get("categories", [])

nodes = [create_badge(cat_id) for cat_id in categories]

return nodes, []


class FrontmatterRole(SphinxRole):
def run(self):
"""Access variables defined in document front matter."""
fm = load_frontmatter(self.env.doc2path(self.env.docname))

return nodes.raw(text=fm[self.text]), []


def collect_fronmatter():
flights = pathlib.Path("orcestra_book/flight_reports/").glob("*[0-9]*[a-z].md")
func = partial(load_frontmatter, derive_flight=True)

return {fm["flight_id"]: fm for fm in map(func, sorted(flights))}


def write_flight_table(app):
frontmatters = collect_fronmatter()

with open("orcestra_book/_templates/operation.md", "r") as fp:
templ = fp.read()

with open("orcestra_book/operation.md", "w") as fp:
t = Template(templ)
fp.write(t.render(flights=frontmatters))


def setup(app):
app.connect("builder-inited", write_flight_table)

app.add_role("cat", CategoryRole())
app.add_role("badges", BadgesRole())
app.add_role("front", FrontmatterRole())

return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
8 changes: 8 additions & 0 deletions orcestra_book/_templates/operation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- This file was created automatically -->
# Operation

Flight-ID | Date | Takeoff | Landing | PI | Nickname | Categories
--- | --- | --- | --- | --- | --- | ---
{% for k, v in flights.items() -%}
[](flight_reports/{{ k }}) | {{ v["expr_date"] }} | {{ v["expr_takeoff"] }} | {{ v["expr_landing"] }} | {{ v["pi"] }} | {{ v["nickname"] }} | {{ v["expr_categories"]|join(' ') }}
{% endfor -%}
2 changes: 0 additions & 2 deletions orcestra_book/operation.md

This file was deleted.

0 comments on commit 2160a58

Please sign in to comment.