| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import os |
| 18 |
import re |
| 19 |
from typing import List |
| 20 |
|
| 21 |
from cylc.flow.parsec.exceptions import ( |
| 22 |
FileParseError, IncludeFileNotFoundError) |
| 23 |
|
| 24 |
|
| 25 |
done: List[str] = [] |
| 26 |
flist: List[str] = [] |
| 27 |
|
| 28 |
include_re = re.compile(r'\s*%include\s+([\'"]?)(.*?)([\'"]?)\s*$') |
| 29 |
|
| 30 |
|
| 31 |
def inline(lines, dir_, filename, for_grep=False, viewcfg=None, level=None): |
| 32 |
"""Recursive inlining of parsec include-files""" |
| 33 |
if level is None: |
| 34 |
# avoid being affected by multiple *different* calls to this function |
| 35 |
flist[:] = [filename] |
| 36 |
else: |
| 37 |
flist.append(filename) |
| 38 |
single = False |
| 39 |
mark = False |
| 40 |
label = False |
| 41 |
if viewcfg: |
| 42 |
mark = viewcfg['mark'] |
| 43 |
single = viewcfg['single'] |
| 44 |
label = viewcfg['label'] |
| 45 |
else: |
| 46 |
viewcfg = {} |
| 47 |
|
| 48 |
outf = [] |
| 49 |
initial_line_index = 0 |
| 50 |
|
| 51 |
if level is None: |
| 52 |
level = '' |
| 53 |
elif mark: |
| 54 |
level += '!' |
| 55 |
|
| 56 |
msg = '' |
| 57 |
|
| 58 |
for line in lines[initial_line_index:]: |
| 59 |
m = include_re.match(line) |
| 60 |
if m: |
| 61 |
q1, match, q2 = m.groups() |
| 62 |
if q1 and (q1 != q2): |
| 63 |
raise FileParseError( |
| 64 |
"mismatched quotes", |
| 65 |
line=line, |
| 66 |
fpath=filename, |
| 67 |
) |
| 68 |
inc = os.path.join(dir_, match) |
| 69 |
if inc not in done: |
| 70 |
if single: |
| 71 |
done.append(inc) |
| 72 |
if not os.path.isfile(inc): |
| 73 |
flist.append(inc) |
| 74 |
raise IncludeFileNotFoundError(flist) |
| 75 |
if for_grep or single or label: |
| 76 |
outf.append( |
| 77 |
'#++++ START INLINED INCLUDE FILE ' + match + msg) |
| 78 |
with open(inc, 'r') as handle: |
| 79 |
finc = [line.rstrip('\n') for line in handle] |
| 80 |
# recursive inclusion |
| 81 |
outf.extend(inline( |
| 82 |
finc, dir_, inc, for_grep, viewcfg, level)) |
| 83 |
if for_grep or single or label: |
| 84 |
outf.append( |
| 85 |
'#++++ END INLINED INCLUDE FILE ' + match + msg) |
| 86 |
|
| 87 |
else: |
| 88 |
outf.append(level + line) |
| 89 |
else: |
| 90 |
# no match |
| 91 |
outf.append(level + line) |
| 92 |
return outf |