4343
4444DEFAULT_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
5049class 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
551562def 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
0 commit comments