Skip to content

Commit aea2a42

Browse files
committed
Restored support for constants from the ERFA module. This did require partially restoring what was core.py.templ, but now it's a separate constants.py.templ just for the constants. The only reason to do this is so that their docstrings can (in principle) be picked up by Sphinx. However, I think it *may* be possible a the C level to give these constants docstrings as well, in which case this can be done in the Cython module (or C module if we switch to just using C directly). But I need to check about that.
1 parent 53aa86d commit aea2a42

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ MANIFEST
1515
astropy/version.py
1616
astropy/cython_version.py
1717
astropy/wcs/include/wcsconfig.h
18+
astropy/erfa/constants.py
1819
astropy/erfa/core.pyx
1920
astropy/erfa/erfa.json
2021

astropy/erfa/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
try:
33
# The ERFA wrappers are not guaranteed available at setup time
4+
from .constants import *
45
from .core import *
56
except ImportError:
67
if not _ASTROPY_SETUP_:

astropy/erfa/constants.py.templ

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
3+
# "constants.py" is auto-generated by erfa_generator.py from the template
4+
# "constants.py.templ". Do *not* edit constants.py directly, instead edit
5+
# "constants.py.templ" and run erfa_generator.py from the source directory
6+
# to update it (or re-run `setup.py build`).
7+
8+
"""This module contains constants defined in the ERFA library."""
9+
10+
{% for constant in constants %}
11+
{{ constant.name}} = {{constant.value }}
12+
"""{{ constant.doc|join(' ') }}"""
13+
{% endfor %}

astropy/erfa/erfa_generator.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
DEFAULT_ERFA_LOC = os.path.join(os.path.dirname(__file__), os.pardir,
4545
os.pardir, 'cextern', 'erfa')
46-
DEFAULT_TEMPLATE_PATH = os.path.join(os.path.dirname(__file__),
47-
'core.pyx.templ')
46+
DEFAULT_TEMPLATE_LOC = os.path.join(os.path.dirname(__file__))
4847

4948

5049
class FunctionDoc(object):
@@ -484,16 +483,13 @@ def extract_erfa_constants(src):
484483
return constants
485484

486485

487-
def generate_erfa_pyx(template, funcs, stream=None):
486+
def generate_erfa_pyx(env, funcs, stream=None):
488487
"""
489488
Generate the core.pyx file from the given template and list of functions.
490489
"""
491490

492491
log.debug("Rendering template")
493492

494-
#Prepare the jinja2 templating environment
495-
env = Environment(loader=FileSystemLoader(os.path.dirname(template)))
496-
497493
def prefix(items, pre):
498494
templ = '{0}{{0}}'.format(pre)
499495
return map(templ.format, items)
@@ -510,7 +506,7 @@ def surround(items, pre, post):
510506
env.filters['postfix'] = postfix
511507
env.filters['surround'] = surround
512508

513-
erfa_pyx_in = env.get_template(os.path.basename(template))
509+
erfa_pyx_in = env.get_template('core.pyx.templ')
514510

515511
if stream is None:
516512
return erfa_pyx_in.render(funcs=funcs)
@@ -527,8 +523,17 @@ def generate_erfa_json(funcs, stream=None):
527523
json.dump(erfa_json, stream, indent=2)
528524

529525

530-
def write_erfa_sources(src=DEFAULT_ERFA_LOC, out='.',
531-
template=DEFAULT_TEMPLATE_PATH, verbose=False):
526+
def generate_erfa_constants(env, constants, stream=None):
527+
templ = env.get_template('constants.py.templ')
528+
529+
if stream is None:
530+
return templ.render(constants=constants)
531+
else:
532+
templ.stream(constants=constants).dump(stream)
533+
534+
535+
def write_erfa_sources(src=DEFAULT_ERFA_LOC, templates=DEFAULT_TEMPLATE_LOC,
536+
out='.', verbose=False):
532537
"""
533538
Generate and write the core.pyx and erfa.json sources.
534539
"""
@@ -539,13 +544,19 @@ def write_erfa_sources(src=DEFAULT_ERFA_LOC, out='.',
539544
log.setLevel(logging.INFO)
540545

541546
funcs = extract_erfa_functions(src)
547+
constants = extract_erfa_constants(src)
542548

543-
erfa_pyx_filename = os.path.join(out,
544-
os.path.basename(template).rsplit('.', 1)[0])
549+
erfa_pyx_filename = os.path.join(out, 'core.pyx')
545550
erfa_json_filename = os.path.join(out, 'erfa.json')
551+
constants_py_filename = os.path.join(out, 'constants.py')
552+
553+
#Prepare the jinja2 templating environment
554+
env = Environment(loader=FileSystemLoader(templates))
546555

547-
generate_erfa_pyx(template, funcs, stream=open(erfa_pyx_filename, 'w'))
556+
generate_erfa_pyx(env, funcs, stream=open(erfa_pyx_filename, 'w'))
548557
generate_erfa_json(funcs, stream=open(erfa_json_filename, 'w'))
558+
generate_erfa_constants(env, constants,
559+
stream=open(constants_py_filename, 'w'))
549560

550561

551562
def main(argv):
@@ -556,18 +567,18 @@ def main(argv):
556567
'(which must be in the same directory as '
557568
'erfa.h). Defaults to the builtin astropy '
558569
'erfa: "{0}"'.format(DEFAULT_ERFA_LOC))
570+
ap.add_argument('-t', '--templates', default=DEFAULT_TEMPLATE_LOC,
571+
help='Path to look for Jinja2 templates for core.pyx '
572+
'and constants.py')
559573
ap.add_argument('-o', '--output', default='.',
560574
help='Directory to which generated sources should be '
561575
'output (default: .)')
562-
ap.add_argument('-t', '--template',
563-
default=DEFAULT_TEMPLATE_PATH,
564-
help='the path to the core.pyx template')
565576
ap.add_argument('-q', '--quiet', action='store_false', dest='verbose',
566577
help='Suppress output normally printed to stdout.')
567578

568579
args = ap.parse_args(argv)
569580

570-
write_erfa_sources(args.src, args.output, args.template,
581+
write_erfa_sources(args.src, args.templates, args.output,
571582
verbose=args.verbose)
572583

573584

astropy/erfa/setup_package.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
SRC_FILES = glob.glob(os.path.join(ERFA_SRC, '*'))
1818
SRC_FILES += [os.path.join(ERFAPKGDIR, filename)
19-
for filename in ['core.pyx.templ', 'erfa_generator.py']]
19+
for filename in ['core.pyx.templ', 'constants.py.templ',
20+
'erfa_generator.py']]
2021

2122
GEN_FILES = [os.path.join(ERFAPKGDIR, 'core.pyx'),
23+
os.path.join(ERFAPKGDIR, 'constants.py'),
2224
os.path.join(ERFAPKGDIR, 'erfa.json')]
2325

2426

0 commit comments

Comments
 (0)