aboutsummaryrefslogtreecommitdiff
path: root/src/journal_lib/parse/preprocessing.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal_lib/parse/preprocessing.py')
-rw-r--r--src/journal_lib/parse/preprocessing.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/journal_lib/parse/preprocessing.py b/src/journal_lib/parse/preprocessing.py
new file mode 100644
index 0000000..6d4a0cf
--- /dev/null
+++ b/src/journal_lib/parse/preprocessing.py
@@ -0,0 +1,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)