diff options
Diffstat (limited to 'numpy/distutils/from_template.py')
| -rw-r--r-- | numpy/distutils/from_template.py | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/numpy/distutils/from_template.py b/numpy/distutils/from_template.py index 9052cf74e..d10b50218 100644 --- a/numpy/distutils/from_template.py +++ b/numpy/distutils/from_template.py @@ -47,15 +47,15 @@ process_file(filename) """ from __future__ import division, absolute_import, print_function -__all__ = ['process_str','process_file'] +__all__ = ['process_str', 'process_file'] import os import sys import re -routine_start_re = re.compile(r'(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\b',re.I) -routine_end_re = re.compile(r'\n\s*end\s*(subroutine|function)\b.*(\n|\Z)',re.I) -function_start_re = re.compile(r'\n (\$|\*)\s*function\b',re.I) +routine_start_re = re.compile(r'(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\b', re.I) +routine_end_re = re.compile(r'\n\s*end\s*(subroutine|function)\b.*(\n|\Z)', re.I) +function_start_re = re.compile(r'\n (\$|\*)\s*function\b', re.I) def parse_structure(astr): """ Return a list of tuples for each function or subroutine each @@ -66,22 +66,22 @@ def parse_structure(astr): spanlist = [] ind = 0 while True: - m = routine_start_re.search(astr,ind) + m = routine_start_re.search(astr, ind) if m is None: break start = m.start() - if function_start_re.match(astr,start,m.end()): + if function_start_re.match(astr, start, m.end()): while True: - i = astr.rfind('\n',ind,start) + i = astr.rfind('\n', ind, start) if i==-1: break start = i if astr[i:i+7]!='\n $': break start += 1 - m = routine_end_re.search(astr,m.end()) + m = routine_end_re.search(astr, m.end()) ind = end = m and m.end()-1 or len(astr) - spanlist.append((start,end)) + spanlist.append((start, end)) return spanlist template_re = re.compile(r"<\s*(\w[\w\d]*)\s*>") @@ -93,7 +93,7 @@ def find_repl_patterns(astr): names = {} for rep in reps: name = rep[0].strip() or unique_key(names) - repl = rep[1].replace('\,','@comma@') + repl = rep[1].replace('\,', '@comma@') thelist = conv(repl) names[name] = thelist return names @@ -124,14 +124,14 @@ def unique_key(adict): template_name_re = re.compile(r'\A\s*(\w[\w\d]*)\s*\Z') -def expand_sub(substr,names): - substr = substr.replace('\>','@rightarrow@') - substr = substr.replace('\<','@leftarrow@') +def expand_sub(substr, names): + substr = substr.replace('\>', '@rightarrow@') + substr = substr.replace('\<', '@leftarrow@') lnames = find_repl_patterns(substr) - substr = named_re.sub(r"<\1>",substr) # get rid of definition templates + substr = named_re.sub(r"<\1>", substr) # get rid of definition templates def listrepl(mobj): - thelist = conv(mobj.group(1).replace('\,','@comma@')) + thelist = conv(mobj.group(1).replace('\,', '@comma@')) if template_name_re.match(thelist): return "<%s>" % (thelist) name = None @@ -151,12 +151,12 @@ def expand_sub(substr,names): rules = {} for r in template_re.findall(substr): if r not in rules: - thelist = lnames.get(r,names.get(r,None)) + thelist = lnames.get(r, names.get(r, None)) if thelist is None: raise ValueError('No replicates found for <%s>' % (r)) if r not in names and not thelist.startswith('_'): names[r] = thelist - rule = [i.replace('@comma@',',') for i in thelist.split(',')] + rule = [i.replace('@comma@', ',') for i in thelist.split(',')] num = len(rule) if numsubs is None: @@ -168,20 +168,20 @@ def expand_sub(substr,names): else: print("Mismatch in number of replacements (base <%s=%s>)" " for <%s=%s>. Ignoring." % - (base_rule, ','.join(rules[base_rule]), r,thelist)) + (base_rule, ','.join(rules[base_rule]), r, thelist)) if not rules: return substr def namerepl(mobj): name = mobj.group(1) - return rules.get(name,(k+1)*[name])[k] + return rules.get(name, (k+1)*[name])[k] newstr = '' for k in range(numsubs): newstr += template_re.sub(namerepl, substr) + '\n\n' - newstr = newstr.replace('@rightarrow@','>') - newstr = newstr.replace('@leftarrow@','<') + newstr = newstr.replace('@rightarrow@', '>') + newstr = newstr.replace('@leftarrow@', '<') return newstr def process_str(allstr): @@ -196,13 +196,13 @@ def process_str(allstr): for sub in struct: writestr += newstr[oldend:sub[0]] names.update(find_repl_patterns(newstr[oldend:sub[0]])) - writestr += expand_sub(newstr[sub[0]:sub[1]],names) + writestr += expand_sub(newstr[sub[0]:sub[1]], names) oldend = sub[1] writestr += newstr[oldend:] return writestr -include_src_re = re.compile(r"(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+[.]src)['\"]",re.I) +include_src_re = re.compile(r"(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+[.]src)['\"]", re.I) def resolve_includes(source): d = os.path.dirname(source) @@ -213,7 +213,7 @@ def resolve_includes(source): if m: fn = m.group('name') if not os.path.isabs(fn): - fn = os.path.join(d,fn) + fn = os.path.join(d, fn) if os.path.isfile(fn): print('Including file', fn) lines.extend(resolve_includes(fn)) @@ -246,10 +246,10 @@ if __name__ == "__main__": fid = sys.stdin outfile = sys.stdout else: - fid = open(file,'r') + fid = open(file, 'r') (base, ext) = os.path.splitext(file) newname = base - outfile = open(newname,'w') + outfile = open(newname, 'w') allstr = fid.read() writestr = process_str(allstr) |
