Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Accommodate multi-heading DAG docs (#1866)

sarayourfrienda2fb2763d463
diff --git a/catalog/dags/providers/provider_api_scripts/wikimedia_commons.py b/catalog/dags/providers/provider_api_scripts/wikimedia_commons.py
index 0a95d85e3..81c1f92f4 100644
--- a/catalog/dags/providers/provider_api_scripts/wikimedia_commons.py
+++ b/catalog/dags/providers/provider_api_scripts/wikimedia_commons.py
@@ -6,7 +6,7 @@
**Output:** TSV file containing the image, the respective
meta-data.
-### Notes
+# Notes
Rate limit of no more than 200 requests/second, and we are required to set a unique
User-Agent field ([docs](https://www.mediawiki.org/wiki/Wikimedia_REST_API#Terms_and_conditions)).
@@ -88,7 +88,7 @@ Below are some specific references to various properties, with examples for case
they might exceed the limit. Technically, it's feasible for almost any property to
exceed the limit, but these are the ones that we've seen in practice.
-#### `imageinfo`
+## `imageinfo`
[Docs](https://commons.wikimedia.org/w/api.php?action=help&modules=query%2Bimageinfo)
[Example where metadata has hundreds of data points](https://commons.wikimedia.org/wiki/File:The_Railway_Chronicle_1844.pdf#metadata)
@@ -97,7 +97,7 @@ exceed the limit, but these are the ones that we've seen in practice.
For these requests, we can remove the `metadata` property from the `iiprops` parameter
to avoid this issue on subsequent iterations.
-#### `globalusage`
+## `globalusage`
[Docs](https://commons.wikimedia.org/w/api.php?action=help&modules=query%2Bglobalusage)
[Example where an image is used on almost every wiki](https://commons.wikimedia.org/w/index.php?curid=4298234).
diff --git a/catalog/utilities/dag_doc_gen/dag_doc_generation.py b/catalog/utilities/dag_doc_gen/dag_doc_generation.py
index 2590ab9ec..606d434f2 100644
--- a/catalog/utilities/dag_doc_gen/dag_doc_generation.py
+++ b/catalog/utilities/dag_doc_gen/dag_doc_generation.py
@@ -14,6 +14,7 @@ The individual DAG documentation section pulls the DAG's `doc_md` blurb and rend
it within the document.
"""
import logging
+import re
from collections import defaultdict
from pathlib import Path
from typing import NamedTuple
@@ -28,6 +29,7 @@ log = logging.getLogger(__name__)
logging.getLogger("common.storage.media").setLevel(logging.WARNING)
# Constants
+HEADING_PROG = re.compile(r"^#+", re.MULTILINE)
DAG_MD_PATH = Path(__file__).parent / "DAGs.md"
DAG_FOLDER = Path(__file__).parents[2] / "dags"
PREAMBLE = """\
@@ -90,6 +92,25 @@ def get_provider_workflows() -> dict[str, ProviderWorkflow]:
return {workflow.dag_id: workflow for workflow in PROVIDER_WORKFLOWS}
+def fix_headings(doc: str) -> str:
+ """
+ Increase all heading levels by 2.
+
+ This is necessary to accommodate the embedded setting of the DAG docs
+ in the final Markdown output.
+ """
+ # Reverse matches so we can work backwards and
+ # not need to worry about our changes to ``doc``
+ # changing the span for the next hit
+ for match in reversed(list(HEADING_PROG.finditer(doc))):
+ start, end = match.span()
+ original_heading = match.string[start:end]
+ new_heading = f"##{original_heading}"
+ doc = f"{doc[:start]}{new_heading}{doc[end:]}"
+
+ return doc
+
+
def get_dags_info(dags: DagMapping) -> list[DagInfo]:
"""
Convert the provided DAG ID -> DAG mapping into a list of DagInfo instances.
@@ -99,10 +120,10 @@ def get_dags_info(dags: DagMapping) -> list[DagInfo]:
provider_workflows = get_provider_workflows()
for dag_id, dag in dags.items():
doc = dag.doc_md
- # Convert the initial H1 header level to H3 if it exists, for better formatting
- # within the document
- if doc and doc.strip().startswith("# "):
- doc = "### " + doc.strip()[2:]
+
+ if doc:
+ doc = fix_headings(doc)
+
dated = dag.catchup
# Infer dag type from the first available tag
type_ = dag.tags[0] if dag.tags else "other"