Skip to content

Commit

Permalink
Merge pull request #5 from projectpythia-mystmd/agoose77/feat-improve…
Browse files Browse the repository at this point in the history
…-error-handling

ENH: improve error handling
  • Loading branch information
jnywong authored Jul 18, 2024
2 parents c141fea + 976f650 commit d32a3e9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 58 deletions.
43 changes: 28 additions & 15 deletions pythia-gallery.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { spawn } from 'node:child_process';

import { spawn } from "node:child_process";

/**
* Call out to an external process that conforms to a JSON-based
* Call out to an external process that conforms to a JSON-based
* stdin-stdout transform specification.
*
* @param opts transform options, containing the binary path and arguments
Expand All @@ -17,17 +16,31 @@ function externalTransform(opts) {
subprocess.stdin.end();

// Read out the response in chunks
const buffers = [];
subprocess.stdout.on('data', (chunk) => {
buffers.push(chunk);
const stdoutBuffers = [];
subprocess.stdout.on("data", (chunk) => {
stdoutBuffers.push(chunk);
});
const stderrBuffers = [];
subprocess.stderr.on("data", (chunk) => {
stderrBuffers.push(chunk);
});

// Await exit
await new Promise((resolve) => {
subprocess.on('close', resolve);
const exitCode = await new Promise((resolve) => {
subprocess.on("close", (code) => resolve(code));
});
if (exitCode) {
const stderr = Buffer.concat(stderrBuffers).toString();
throw new Error(
`Non-zero error code while running pythia-gallery'\n\n${stderr}`,
);
} else if (stderrBuffers.length) {
const stderr = Buffer.concat(stderrBuffers).toString();
// Log the error
console.debug(`\n\n${stderr}\n\n`);
}
// Concatenate the responses
const stdout = Buffer.concat(buffers).toString();
const stdout = Buffer.concat(stdoutBuffers).toString();

// Modify the tree in-place
const result = JSON.parse(stdout);
Expand All @@ -36,23 +49,23 @@ function externalTransform(opts) {
}

const cookbooksTransform = () =>
externalTransform({ executable: 'python3', args: ['pythia-gallery.py'] });
externalTransform({ executable: "python3", args: ["pythia-gallery.py"] });

const pythiaGalleryDirective = {
name: 'pythia-cookbooks',
doc: 'An example directive for embedding a Pythia cookbook gallery.',
name: "pythia-cookbooks",
doc: "An example directive for embedding a Pythia cookbook gallery.",
run() {
const img = { type: 'pythia-cookbooks', children: [] };
const img = { type: "pythia-cookbooks", children: [] };
return [img];
},
};
const pythiaGalleryTransform = {
plugin: cookbooksTransform,
stage: 'document',
stage: "document",
};

const plugin = {
name: 'Pythia Gallery',
name: "Pythia Gallery",
directives: [pythiaGalleryDirective],
transforms: [pythiaGalleryTransform],
};
Expand Down
92 changes: 49 additions & 43 deletions pythia-gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib.request
import yaml
import concurrent.futures
import traceback

from unist import *

Expand All @@ -23,60 +24,65 @@


def fetch_yaml(url: str):
print(f"Fetching {url}", file=sys.stderr, flush=True)
with urllib.request.urlopen(url) as response:
body = response.read().decode()
return yaml.load(body, yaml.SafeLoader)


def render_cookbook(name: str):
raw_base_url = f"https://raw.githubusercontent.com/ProjectPythia-MystMD/{name}/main"
base_url = f"https://github.com/ProjectPythia-MystMD/{name}"
config_url = f"{raw_base_url}/_config.yml"
book_url = f"https://projectpythia-mystmd.github.io/{name}"

# Load JB data
data = fetch_yaml(config_url)
title = data["title"]

# Fetch gallery metadata
gallery_url = f"{raw_base_url}/_gallery_info.yml"
gallery_data = fetch_yaml(gallery_url)
image_name = gallery_data["thumbnail"]
image_url = f"{raw_base_url}/{image_name}"

# Build tags
tags = gallery_data["tags"]

return {
"type": "card",
"url": book_url,
"children": [
{"type": "cardTitle", "children": [text(title)]},
div(
[
image(image_url),
div(
[
span(
[text(item)],
style=styles.get(name, DEFAULT_STYLE),
)
for name, items in tags.items()
if items is not None
for item in items
]
),
],
),
],
}

try:
print(f"Rendering {name}", file=sys.stderr, flush=True)
raw_base_url = f"https://raw.githubusercontent.com/ProjectPythia-MystMD/{name}/main"
config_url = f"{raw_base_url}/myst.yml"
book_url = f"https://projectpythia-mystmd.github.io/{name}"

# Load JB data
config = fetch_yaml(config_url)
title = config["project"]["title"]

# Fetch gallery metadata
gallery_url = f"{raw_base_url}/_gallery_info.yml"
gallery_data = fetch_yaml(gallery_url)
image_name = gallery_data["thumbnail"]
image_url = f"{raw_base_url}/{image_name}"

# Build tags
tags = gallery_data["tags"]

return {
"type": "card",
"url": book_url,
"children": [
{"type": "cardTitle", "children": [text(title)]},
div(
[
image(image_url),
div(
[
span(
[text(item)],
style=styles.get(name, DEFAULT_STYLE),
)
for name, items in tags.items()
if items is not None
for item in items
]
),
],
),
],
}
except Exception as err:
print(f"\n\nError rendering {name}", file=sys.stderr)
traceback.print_exception(err, file=sys.stderr)
return None

def render_cookbooks(pool):
with open("cookbook_gallery.txt") as f:
body = f.read()

return [*pool.map(render_cookbook, body.splitlines())]
return [c for c in pool.map(render_cookbook, body.splitlines()) if c is not None]


if __name__ == "__main__":
Expand Down

0 comments on commit d32a3e9

Please sign in to comment.