Skip to content

Commit 8f3a04c

Browse files
authored
Merge pull request #54 from asdf-format/issue-22
Add initial stub for Sphinx documentation, to be improved later
2 parents 7543248 + ba19876 commit 8f3a04c

28 files changed

+1275
-36
lines changed

.github/workflows/build.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,44 @@ jobs:
8080

8181
- name: Dist check
8282
run: make distcheck ${{ github.event.inputs.make_flags }}
83+
84+
# Test the documentation build if the build passes
85+
docs:
86+
name: 'Build documentation'
87+
runs-on: ubuntu-latest
88+
needs: build-and-test
89+
steps:
90+
- name: Checkout code
91+
uses: actions/checkout@v3
92+
with:
93+
submodules: true
94+
95+
- name: Install documentation dependencies
96+
run: |
97+
sudo apt-get update
98+
sudo apt-get install -y \
99+
autoconf \
100+
automake \
101+
libtool \
102+
build-essential \
103+
libclang-dev \
104+
libfyaml-dev \
105+
llvm \
106+
python3-sphinx \
107+
python3-clang
108+
# Don't use docs/requirements.txt here since we are trying with the
109+
# system packages for sphinx and clang
110+
pip install furo hawkmoth
111+
112+
- name: Set LD_LIBRARY_PATH
113+
run: echo "LD_LIBRARY_PATH=$(llvm-config --libdir):$LD_LIBRARY_PATH" >> $GITHUB_ENV
114+
115+
- name: Bootstrap autotools
116+
run: ./autogen.sh
117+
118+
- name: Configure
119+
run: |
120+
./configure --enable-docs
121+
122+
- name: Build Sphinx documentation
123+
run: make docs SPHINX_FLAGS=-W

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config.h.in~
99
config.log
1010
config.status
1111
configure
12+
configure~
1213
stamp-h1
1314
Makefile.in
1415
Makefile
@@ -36,9 +37,13 @@ third_party/*.la
3637
tests/*.log
3738
tests/*.trs
3839
tests/*.unit
40+
tests/doc_examples
3941
tests/test_cpp_headers.cpp
4042
tests/tmp
4143

44+
# doc build outputs
45+
docs/_build
46+
4247
# coverage outputs
4348
*-coverage.info
4449
*.gcda
@@ -49,3 +54,6 @@ tests/tmp
4954

5055
# distcheck outputs
5156
libasdf-*/
57+
58+
# misc
59+
.gdb_history

Makefile.am

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ tidy:
7777
clang-tidy $(src_files) -- \
7878
$(DEFS) $(DEFAULT_INCLUDES) $(AM_CPPLFAGS) $(CPPFLAGS) $(asdf_CFLAGS)
7979

80+
if BUILD_DOCS
81+
docs:
82+
$(SPHINXBUILD) $(SPHINX_FLAGS) -b html $(srcdir)/docs $(srcdir)/docs/_build/html
83+
endif
84+
.PHONY: docs
85+
86+
87+
# Check building docs from the distribution if possible
88+
distcheck-hook:
89+
if BUILD_DOCS
90+
$(MAKE) docs SPHINX_FLAGS=-W
91+
endif
92+
8093

8194
CODE_COVERAGE_IGNORE_PATTERN = \
8295
$(abs_top_srcdir)/tests/* \
@@ -85,12 +98,16 @@ CODE_COVERAGE_IGNORE_PATTERN = \
8598
# For rules generated by AX_AM_MACROS_STATIC; e.g. code coverage rules
8699
include $(top_srcdir)/aminclude_static.am
87100

88-
SUBDIRS = include tests third_party
101+
SUBDIRS = include tests third_party docs
89102

90103
# Include sample files from asdf-standard in the distribution (used in tests)
91104
# Eventually test against older reference files as well
92105
reference_files = $(top_srcdir)/asdf-standard/reference_files/1.6.0
93106
EXTRA_DIST = \
107+
CHANGES.rst \
108+
CODE_OF_CONDUCT.md \
109+
LICENSE \
110+
README.rst \
94111
$(reference_files)/anchor.asdf \
95112
$(reference_files)/ascii.asdf \
96113
$(reference_files)/basic.asdf \

README.rst

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,170 @@
11
libasdf
22
#######
33

4-
C library for reading (and eventually writing) `ASDF
5-
<https://www.asdf-format.org/en/latest/>`__ files
4+
.. _begin-badges:
5+
6+
.. image:: https://github.com/asdf-format/libasdf/workflows/Build/badge.svg
7+
:target: https://github.com/asdf-format/libasdf/actions
8+
:alt: CI Status
9+
10+
.. _end-badges:
11+
12+
A C library for reading (and eventually writing) `ASDF
13+
<https://www.asdf-format.org/en/latest/>`__ files.
614

715

816
Introduction
917
============
1018

11-
Let's have something to say here first.
19+
libasdf is largely a wrapper around `libfyaml <https://pantoniou.github.io/libfyaml/>`__
20+
but with an understanding of the structure of ASDF files, with the capability to read and
21+
extract binary block data, as well as typed getters for metadata in the ASDF tree.
22+
23+
It also features an extension mechanism (still nascent) for reading ASDF schemas, including
24+
the core schemas such as ``core/ndarray-<x.y.z>`` into C-native datastructures.
25+
26+
Getting Started
27+
---------------
28+
29+
To open an ASDF file with libasdf the simplest way is to use the ``asdf_open`` function.
30+
This returns an ``asdf_file_t *`` which is your main interface to the ASDF file.
31+
When done with the file make sure to call ``asdf_close`` to free resources:
32+
33+
.. code:: c
34+
:name: test-open-close-file
35+
36+
#include <stdio.h>
37+
#include <asdf.h>
38+
39+
int main(int argc, char **argv) {
40+
if (argc < 2) {
41+
fprintf(stderr, "Usage: %s filename\n", argv[0]);
42+
return 1;
43+
}
44+
const char *filename = argv[1];
45+
asdf_file_t *file = asdf_open(filename, "r");
46+
47+
if (file == NULL) {
48+
fprintf(stderr, "error opening the ASDF file\n");
49+
return 1;
50+
}
51+
52+
asdf_close(file);
53+
return 0;
54+
}
55+
56+
The following more complete example demonstrates how to read different metadata out of
57+
the ASDF tree, as well as extract block data. Inline comments provide further explanation:
58+
59+
.. code:: c
60+
:name: test-read-metadata-ndarray
61+
62+
#include <stdio.h>
63+
#include <stdlib.h>
64+
#include <asdf.h>
65+
66+
int main(int argc, char **argv) {
67+
if (argc < 2) {
68+
fprintf(stderr, "Usage: %s filename\n", argv[0]);
69+
return 1;
70+
}
71+
const char *filename = argv[1];
72+
73+
// The mode string "r" is required and is the only currently-supported mode
74+
asdf_file_t *file = asdf_open(filename, "r");
75+
76+
if (file == NULL) {
77+
fprintf(stderr, "error opening the ASDF file\n");
78+
return 1;
79+
}
80+
81+
// The simplest way to read metadata from the file is with the
82+
// `asdf_get_<type>*` family of functions
83+
// They all return a value by pointer argument and return an
84+
// `asdf_value_error_t`
85+
// For example you can read a string from the metadata like:
86+
87+
const char *software = NULL;
88+
// Returns a 0-terminated string into *software.
89+
asdf_value_err_t err = asdf_get_string0(file, "asdf_library/author", &software);
90+
91+
if (err == ASDF_VALUE_OK) {
92+
printf("software: %s\n", software);
93+
}
94+
95+
// Other errors could be e.g. ASDF_VALUE_ERR_NOT_FOUND if the key doesn't
96+
// exist, or ASDF_VALUE_ERR_TYPE_MISMATCH if it's not a string.
97+
98+
// There are also extensions registered for some (not all yet) of the
99+
// core schemas. Objects defined by extension schemas (identified by
100+
// their YAML tags) also have corresponding asdf_get_<type> functions:
101+
asdf_meta_t *meta = NULL;
102+
103+
// This reads the top-level core/asdf-1.0.0 schema
104+
err = asdf_get_meta(file, "/", &meta);
105+
if (err == ASDF_VALUE_OK) {
106+
if (meta->history.entries[0]) {
107+
// This is a NULL-terminated array of asdf_history_entry_t*
108+
printf("first history entry: %s\n", meta->history.entries[0]->description);
109+
}
110+
}
111+
112+
// Functions like `asdf_get_meta` that return into a double-pointer to a
113+
// struct allocate memory for that structure automatically.
114+
// The all have a corresponding `asdf_<type>_destroy` function.
115+
// The plan is to track these on the file object (issue #34) to make
116+
// memory management easier and cleaner, but for now you have to free
117+
// them manually when you're done with them. This is good practice in any
118+
// case.
119+
asdf_meta_destroy(meta);
120+
121+
// ndarrays work no differently; this reads an ndarray named "cube".
122+
asdf_ndarray_t *ndarray = NULL;
123+
err = asdf_get_ndarray(file, "cube", &ndarray);
124+
if (err != ASDF_VALUE_OK) {
125+
fprintf(stderr, "error reading ndarray metadata: %d\n", err);
126+
return 1;
127+
}
128+
129+
printf("number of data dimensions: %d\n", ndarray->ndim);
130+
131+
// Get just a raw pointer to the ndarray data block (if uncompressed).
132+
// Optionally returns the size in bytes as well
133+
size_t size = 0;
134+
void *data = asdf_ndarray_data_raw(ndarray, &size);
135+
136+
if (data == NULL) {
137+
fprintf(stderr, "error reading ndarray data\n");
138+
return 1;
139+
}
140+
141+
// Slightly more useful is the asdf_ndarray_read_tile_ functions.
142+
// They can copy the data, including converting endianness into a tile
143+
// buffer. If an existing buffer is not passed it will allocate one of
144+
// the correct size to hold the data. The user is responsible for
145+
// freeing the buffer.
146+
147+
// Read a 10x10x10 cube
148+
const uint64_t origin[3] = {0, 0, 0};
149+
const uint64_t shape[3] = {10, 10, 10};
150+
void *tile = NULL;
151+
asdf_ndarray_err_t array_err = asdf_ndarray_read_tile_ndim(
152+
ndarray,
153+
origin,
154+
shape,
155+
&tile
156+
);
157+
158+
if (array_err != ASDF_NDARRAY_OK) {
159+
fprintf(stderr, "error reading ndarray: %d\n", array_err);
160+
return 1;
161+
}
162+
163+
free(tile);
164+
asdf_ndarray_destroy(ndarray);
165+
asdf_close(file);
166+
return 0;
167+
}
12168
13169
14170
Development

configure.ac

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,18 @@ AS_IF([test "x$asdf_build_tool" = "xyes"], [
177177
])
178178
])
179179

180+
# Enable/check doc build requirements
181+
ASDF_CHECK_SPHINX
182+
183+
# Check for Python--used to generate some of the tests if available
184+
AC_PATH_PROG([PYTHON3], [python3], [no])
185+
AM_CONDITIONAL([HAVE_PYTHON], [test "$PYTHON3" != "no"])
186+
AC_SUBST([PYTHON3])
187+
180188
AC_SUBST([ASDF_CFLAGS])
181189
AC_SUBST([ASDF_LDFLAGS])
182190
AC_CONFIG_HEADERS([config.h]) # use config.h instead of passing -D in the command line
183-
AC_CONFIG_FILES([Makefile include/Makefile tests/Makefile third_party/Makefile])
191+
AC_CONFIG_FILES([Makefile include/Makefile tests/Makefile third_party/Makefile docs/Makefile])
184192

185193
AC_OUTPUT
186194

docs/Makefile.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
EXTRA_DIST = \
2+
conf.py \
3+
index.rst \
4+
requirements.txt \
5+
_static/css/globalnav.css \
6+
_static/images/favicon.ico \
7+
_static/images/logo-dark-mode.png \
8+
_static/images/logo-light-mode.png

docs/_static/css/globalnav.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* Top Banner Navigation
2+
-------------------------------------------------- */
3+
.announcement-content a {
4+
padding-right: 1em;
5+
}
6+
7+
.announcement-content a:hover {
8+
color: fuchsia;
9+
}

docs/_static/images/favicon.ico

42.3 KB
Binary file not shown.
45.5 KB
Loading
42.3 KB
Loading

0 commit comments

Comments
 (0)