source: codes/icosagcm/devel/Python/jinja @ 615

Last change on this file since 615 was 615, checked in by dubos, 6 years ago

devel/unstructured : import Python layer from HEAT

  • Property svn:executable set to *
File size: 1.2 KB
Line 
1from jinja2 import Environment, FileSystemLoader
2from sys import argv
3from textwrap import dedent
4from re import search # regular expressions
5
6def 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       
13env=Environment(loader=FileSystemLoader('.'))
14tpl=env.get_template(argv[1])
15source = tpl.render()
16source = [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 '\'
23noop      = r'!\$OMP'
24increase  = r'\bTHEN\b|\bDO\b|\bSELECT CASE\b'
25inter     = r'\bELSE\b|\bCASE\(|\bCASE DEFAULT\b'
26decrease  = r'\bEND IF\b|\bENDIF\b|\bEND DO\b|\bENDDO\b|\bEND SELECT\b'
27
28indent=1
29
30for 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.