Skip to content

ENH: Python as a templating language(PyAS) #17952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/DISTUTILS.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ to produce ``.h`` files).

.. _conv_template.py: https://github.com/numpy/numpy/blob/master/numpy/distutils/conv_template.py

.. automodule:: numpy.distutils.pyas_template
:members:

Useful functions in ``numpy.distutils.misc_util``
-------------------------------------------------

Expand Down
6 changes: 4 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,16 @@ def linkcode_resolve(domain, info):

from pygments.lexers import CLexer
from pygments.lexer import inherit, bygroups
from pygments.token import Comment
from pygments.token import Comment, Keyword

class NumPyLexer(CLexer):
name = 'NUMPYLEXER'

tokens = {
'statements': [
(r'@[a-zA-Z_]*@', Comment.Preproc, 'macro'),
(r'\$[A-Z][A-Za-z]*', Keyword.Constant),
(r'{{.*}}', Keyword.Constant),
inherit,
],
]
}
34 changes: 25 additions & 9 deletions numpy/distutils/command/build_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from numpy.distutils.from_template import process_file as process_f_file
from numpy.distutils.conv_template import process_file as process_c_file
from numpy.distutils.pyas_template import process_file as process_pyas_file

def subst_vars(target, source, d):
"""Substitute any occurrence of @foo@ by d['foo'] from source file into
Expand Down Expand Up @@ -405,31 +406,46 @@ def filter_files(self, sources, exts = []):
return new_sources, files

def template_sources(self, sources, extension):
def process_conv(source, target_file, base):
if _f_pyf_ext_match(base):
log.info("from_template:> " + target_file)
outstr = process_f_file(source)
else:
log.info("conv_template:> " + target_file)
outstr = process_c_file(source)
with open(target_file, 'w') as fid:
fid.write(outstr)

def process_pyas(source, target_file, base):
log.info("pyas_template:> " + target_file)
process_pyas_file(source, target_file)

tpl_handler = {
'.src' : process_conv,
'.pyas' : process_pyas
}
new_sources = []
if is_sequence(extension):
depends = extension[1].get('depends')
include_dirs = extension[1].get('include_dirs')
else:
depends = extension.depends
include_dirs = extension.include_dirs

for source in sources:
(base, ext) = os.path.splitext(source)
if ext == '.src': # Template file
tph = tpl_handler.get(ext)
if tph: # Template file
if self.inplace:
target_dir = os.path.dirname(base)
else:
target_dir = appendpath(self.build_src, os.path.dirname(base))
self.mkpath(target_dir)

target_file = os.path.join(target_dir, os.path.basename(base))
if (self.force or newer_group([source] + depends, target_file)):
if _f_pyf_ext_match(base):
log.info("from_template:> %s" % (target_file))
outstr = process_f_file(source)
else:
log.info("conv_template:> %s" % (target_file))
outstr = process_c_file(source)
with open(target_file, 'w') as fid:
fid.write(outstr)
tph(source, target_file, base)

if _header_ext_match(target_file):
d = os.path.dirname(target_file)
if d not in include_dirs:
Expand Down
Loading