File: parse.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (40 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import re


def get(filename):
    for path in ["..", "."]:
        filepath = os.path.join(path, filename)
        if os.path.exists(filepath):
            with open(filepath) as infile:
                code = infile.read()
                # comment = '#line 0 // Start of "%s"\n' % filename
                comment = '// --- start of "%s" ---\n' % filename
            return comment + code
    return '#error "%s" not found !\n' % filename

code = """
#include "colormap/colormaps.glsl"
"""

re_include = re.compile(r'\#include\s*"(?P<filename>[a-zA-Z0-9\-\.\/]+)"')

includes = []


def replace(match):
    filename = match.group("filename")
    if filename not in includes:
        includes.append(filename)
        text = get(filename)
        # lineno = code.count("\n",0,match.start())+1
        # text += '\n#line %d // End of "%s"' % (lineno, filename)
        text += '// --- end of "%s" ---\n' % filename
        return text
    return ''


if __name__ == "__main__":
    while re.search(re_include, code):
        code = re.sub(re_include, replace, code)
    print(code)