blob: 6d4a0cf4cfc3b89064b0549b28247c3e20456ede (
plain) (
blame)
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
|
import re
from pathlib import Path
def preprocess_includes(filepath: Path):
"""
Reads the file at 'filepath', processing any "include" directives,
and returns a single string containing the preprocessed file content.
This does not report circular includes, so that would become a infinite loop
"""
INCLUDE_RE = re.compile(r'^\s*include\s+([^\n]+)\s*$', re.IGNORECASE)
def read_file(file_path):
with open(file_path, 'r') as file:
return file.readlines()
lines = read_file(filepath)
i = 0
while i < len(lines):
match = INCLUDE_RE.match(lines[i])
if match:
included_file_path = match.group(1)
included_lines = read_file(included_file_path)
lines[i:i+1] = included_lines
else:
i += 1
return ''.join(lines)
|