ENH: handle tempita templated Cython source files, *.pyx.in

This commit is contained in:
Evgeni Burovski
2016-02-02 16:35:20 +00:00
parent aae7f2b615
commit 3cd4d03cb2
+24 -1
View File
@@ -30,7 +30,8 @@ def cython(pyx_files, working_path=''):
# If cython is not found, we do nothing -- the build will make use of
# the distributed .c files
print("Cython not found; falling back to pre-built %s" \
% " ".join([f.replace('.pyx', '.c') for f in pyx_files]))
% " ".join([f.replace('.pyx.in', 'c').replace('.pyx', '.c')
for f in pyx_files]))
else:
for pyxfile in [os.path.join(working_path, f) for f in pyx_files]:
@@ -38,6 +39,10 @@ def cython(pyx_files, working_path=''):
if not _changed(pyxfile):
continue
if pyxfile.endswith('.pyx.in'):
process_tempita_pyx(pyxfile)
pyxfile = pyxfile.replace('.pyx.in', '.pyx')
cythonize(pyxfile)
def _md5sum(f):
@@ -69,3 +74,21 @@ def _changed(filename):
cf.write(md5_new.encode('utf-8'))
return md5_cached != md5_new.encode('utf-8')
def process_tempita_pyx(fromfile):
try:
try:
from Cython import Tempita as tempita
except ImportError:
import tempita
except ImportError:
raise Exception('Building requires Tempita: '
'pip install --user Tempita')
from_filename = tempita.Template.from_filename
template = from_filename(fromfile) #, encoding=sys.getdefaultencoding())
pyxcontent = template.substitute()
assert fromfile.endswith('.pyx.in')
pyxfile = fromfile[:-len('.pyx.in')] + '.pyx'
with open(pyxfile, "w") as f:
f.write(pyxcontent)