Last change
on this file since 615 was
615,
checked in by dubos, 7 years ago
|
devel/unstructured : import Python layer from HEAT
|
-
Property svn:executable set to
*
|
File size:
1.2 KB
|
Rev | Line | |
---|
[615] | 1 | from jinja2 import Environment, FileSystemLoader |
---|
| 2 | from sys import argv |
---|
| 3 | from textwrap import dedent |
---|
| 4 | from re import search # regular expressions |
---|
| 5 | |
---|
| 6 | def pretty(line,indent): |
---|
| 7 | line = dedent(line) |
---|
| 8 | if search('^#', line): # check if line is a preprocessor directive |
---|
| 9 | print line |
---|
| 10 | else: |
---|
| 11 | print (3*indent)*' ' + line |
---|
| 12 | |
---|
| 13 | env=Environment(loader=FileSystemLoader('.')) |
---|
| 14 | tpl=env.get_template(argv[1]) |
---|
| 15 | source = tpl.render() |
---|
| 16 | source = [line for line in source.split('\n') if line.strip() != ''] |
---|
| 17 | |
---|
| 18 | # regular expressions matching Fortran keywords |
---|
| 19 | # \b detects word boundaries |
---|
| 20 | # $ and ( must be escaped : \$, \( |
---|
| 21 | # THIS|THAT means THIS or THAT |
---|
| 22 | # raw Python strings r'blabla' avoid doubling '\' |
---|
| 23 | noop = r'!\$OMP' |
---|
| 24 | increase = r'\bTHEN\b|\bDO\b|\bSELECT CASE\b' |
---|
| 25 | inter = r'\bELSE\b|\bCASE\(|\bCASE DEFAULT\b' |
---|
| 26 | decrease = r'\bEND IF\b|\bENDIF\b|\bEND DO\b|\bENDDO\b|\bEND SELECT\b' |
---|
| 27 | |
---|
| 28 | indent=1 |
---|
| 29 | |
---|
| 30 | for line in source : |
---|
| 31 | if search(noop, line) : |
---|
| 32 | pretty(line,indent) |
---|
| 33 | elif search(decrease, line) : |
---|
| 34 | indent = indent-1 |
---|
| 35 | pretty(line,indent) |
---|
| 36 | elif search(increase,line): |
---|
| 37 | pretty(line,indent) |
---|
| 38 | indent = indent+1 |
---|
| 39 | elif search(inter,line): |
---|
| 40 | pretty(line,indent-1) |
---|
| 41 | else: |
---|
| 42 | pretty(line,indent) |
---|
Note: See
TracBrowser
for help on using the repository browser.