Merge pull request #248 from cclauss/patch-2

Streamline replacement logic
This commit is contained in:
Mathieu Virbel 2017-04-29 00:54:09 +02:00 committed by GitHub
commit 829126bf70

View file

@ -19,8 +19,8 @@ def resolve_cython():
def do(fn): def do(fn):
print('cythonize:', fn) print('cythonize:', fn)
assert(fn.endswith('.pyx'))
parts = fn.split('/') parts = fn.split('/')
assert(parts[-1].endswith('.pyx'))
if parts[0] == '.': if parts[0] == '.':
parts.pop(0) parts.pop(0)
modname = parts[-1][:-4] modname = parts[-1][:-4]
@ -37,24 +37,13 @@ def do(fn):
with open(fn_c) as fd: with open(fn_c) as fd:
data = fd.read() data = fd.read()
modname = modname.split('.')[-1] modname = modname.split('.')[-1]
pat1 = 'init{}(void)'.format(modname) pac_mod = '{}_{}'.format(package, modname)
sub1 = 'init{}_{}(void)'.format(package, modname) fmts = ('init{}(void)', 'PyInit_{}(void)', 'Pyx_NAMESTR("{}")', '"{}"')
pat2 = 'PyInit_{}(void)'.format(modname) for i, fmt in enumerate(fmts):
sub2 = 'PyInit{}_{}(void)'.format(package, modname) pat = fmt.format(modname)
pat3 = 'Pyx_NAMESTR("{}")'.format(modname) sub = fmt.format(pac_mod)
sub3 = 'Pyx_NAMESTR("{}_{}")'.format(package, modname) print('{}: {} -> {}'.format(i + 1, pat, sub))
pat4 = '"{}"'.format(modname) data = data.replace(pat, sub)
sub4 = '"{}_{}"'.format(package, modname)
print('1: {} -> {}'.format(pat1, sub1))
print('2: {} -> {}'.format(pat2, sub2))
print('3: {} -> {}'.format(pat3, sub3))
print('4: {} -> {}'.format(pat4, sub4))
data = data.replace(pat1, sub1)
data = data.replace(pat2, sub2)
data = data.replace(pat3, sub3)
data = data.replace(pat4, sub4)
print('rewrite', fn_c) print('rewrite', fn_c)
with open(fn_c, 'w') as fd: with open(fn_c, 'w') as fd:
fd.write(data) fd.write(data)