Skip to content

Commit

Permalink
update CI, tidy macros
Browse files Browse the repository at this point in the history
  • Loading branch information
virgesmith committed Feb 25, 2024
1 parent b1f6bc7 commit 6464710
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: "pip: Python ${{ matrix.python-version }} / ${{ matrix.os }}"
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
matrix:
python-version: [ "3.11" ]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: "pip: Python ${{ matrix.python-version }} coverage"
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/draft-pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
name: Paper Draft
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build draft PDF
uses: openjournals/openjournals-draft-action@master
with:
journal: joss
paper-path: paper/paper.md
- name: Upload
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: paper
path: paper/paper.pdf
4 changes: 2 additions & 2 deletions .github/workflows/mpi-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
matrix:
python-version: ["3.11"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: "pip: Python ${{ matrix.python-version }}"
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pypi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
if: "!contains(github.event.head_commit.message, 'Bump version')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: current_version
Expand Down
156 changes: 86 additions & 70 deletions docs/macros.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,106 @@
# macros for mkdocs-macros-plugin
import os
import requests
import importlib
import os
from datetime import datetime

import requests

_inline_code_styles = {
".py": "python",
".sh": "bash",
".h": "cpp",
".cpp": "cpp",
".c": "c",
".rs": "rs",
".js": "js",
".md": None
".py": "python",
".sh": "bash",
".h": "cpp",
".cpp": "cpp",
".c": "c",
".rs": "rs",
".js": "js",
".md": None,
}

# this is the overall record id, not a specific version
_NEWORDER_ZENODO_ID = 4031821


def write_requirements() -> None:
try:
with open("docs/requirements.txt", "w") as fd:
fd.write(f"""# DO NOT EDIT
try:
with open("docs/requirements.txt", "w") as fd:
fd.write(
f"""\
# DO NOT EDIT
# auto-generated @ {datetime.now()} by docs/macros.py::write_requirements()
# required by readthedocs.io
""")
fd.writelines(f"{dep}=={importlib.metadata.version(dep)}\n" for dep in [
"mkdocs",
"mkdocs-macros-plugin",
"mkdocs-material",
"mkdocs-material-extensions",
"mkdocs-video",
"requests"
])
# ignore any error, this should only run in a dev env anyway
except:
pass
"""
)
fd.writelines(
f"{dep}=={importlib.metadata.version(dep)}\n"
for dep in [
"mkdocs",
"mkdocs-macros-plugin",
"mkdocs-material",
"mkdocs-material-extensions",
"mkdocs-video",
"requests",
]
)
# ignore any error, this should only run in a dev env anyway
except:
pass


def define_env(env):

@env.macro
def insert_zenodo_field(*keys: str):
""" This is the *released* version not the dev one """
try:
response = requests.get('https://zenodo.org/api/records', params={'q': _NEWORDER_ZENODO_ID, 'access_token': os.getenv("ZENODO_PAT")})
response.raise_for_status()
result = response.json()["hits"]["hits"][0]
for k in keys:
result = result[k]
return result

except Exception as e:
return f"{e.__class__.__name__}:{e} while retrieving {keys}"


@env.macro
def include_snippet(filename, tag=None, show_filename=True):
""" looks for code in <filename> between lines containing "!<tag>!" """
full_filename = os.path.join(env.project_dir, filename)

_, file_type = os.path.splitext(filename)
# default to literal "text" for inline code style
code_style = _inline_code_styles.get(file_type, "text")

with open(full_filename, 'r') as f:
lines = f.readlines()

if tag:
tag = f"!{tag}!"
span = []
for i, l in enumerate(lines):
if tag in l:
span.append(i)
if len(span) != 2:
return f"```ERROR {filename} ({code_style}) too few/many tags ({len(span)}) for '{tag}'```"
lines = lines[span[0] + 1: span[1]]

if show_filename:
title = f'title="{filename}"'
else:
title = ""
if code_style is not None:
return f"```{code_style} {title}\n{''.join(lines)}```"
else:
return "".join(lines)
@env.macro
def insert_zenodo_field(*keys: str):
"""This is the *released* version not the dev one"""
try:
response = requests.get(
"https://zenodo.org/api/records",
params={
"q": _NEWORDER_ZENODO_ID,
"access_token": os.getenv("ZENODO_PAT"),
},
)
response.raise_for_status()
# with open("zenodo-result.json", "w") as fd:
# print(response.text)
# fd.write(response.text)
result = response.json()["hits"]["hits"][0]
for k in keys:
result = result[k]
return result

except Exception as e:
return f"{e.__class__.__name__}:{e} while retrieving {keys}"

@env.macro
def include_snippet(filename, tag=None, show_filename=True):
"""looks for code in <filename> between lines containing "!<tag>!" """
full_filename = os.path.join(env.project_dir, filename)

_, file_type = os.path.splitext(filename)
# default to literal "text" for inline code style
code_style = _inline_code_styles.get(file_type, "text")

with open(full_filename, "r") as f:
lines = f.readlines()

if tag:
tag = f"!{tag}!"
span = []
for i, l in enumerate(lines):
if tag in l:
span.append(i)
if len(span) != 2:
return f"```ERROR {filename} ({code_style}) too few/many tags ({len(span)}) for '{tag}'```"
lines = lines[span[0] + 1 : span[1]]

if show_filename:
title = f'title="{filename}"'
else:
title = ""
if code_style is not None:
return f"```{code_style} {title}\n{''.join(lines)}```"
else:
return "".join(lines)


# write_requirements()

0 comments on commit 6464710

Please sign in to comment.