19 lines
638 B
Python
19 lines
638 B
Python
import re
|
|
from pathlib import Path
|
|
|
|
|
|
class CopybookPreprocessor:
|
|
def __init__(self, paths=None):
|
|
self.paths = paths or ["./copybooks"]
|
|
|
|
def expand(self, text: str) -> str:
|
|
def _rep(m):
|
|
n = m.group(1).strip()
|
|
for p in self.paths:
|
|
for e in ("", ".cpy", ".cbl"):
|
|
f = Path(p) / f"{n}{e}"
|
|
if f.exists():
|
|
return f" *> COPY {n}\n{f.read_text()}\n *> END COPY {n}"
|
|
return f" *> COPY {n} NOT FOUND"
|
|
return re.sub(r'^ COPY\s+(\w+(?:-\w+)?)\s*\.', _rep, text, flags=re.MULTILINE)
|