| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Create Syntax keys from SPEC |
| 3 |
""" |
| 4 |
|
| 5 |
import re |
| 6 |
|
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
from cylc.flow.cfgspec.workflow import SPEC |
| 10 |
|
| 11 |
|
| 12 |
LANG_FILE_PATH = Path(__file__).parent.parent.parent / 'cylc/flow/etc/syntax' |
| 13 |
PRIVATE_NAMES = ['__MANY__', 'cylc.flow'] |
| 14 |
|
| 15 |
|
| 16 |
def get_keywords_from_workflow_cfg(): |
| 17 |
"""Extract a list of keywords from workflow SPEC. |
| 18 |
""" |
| 19 |
keywords = [] |
| 20 |
for item in SPEC.walk(): |
| 21 |
keywords += [item[1].name] |
| 22 |
|
| 23 |
keywords = sorted(set(keywords) - set(PRIVATE_NAMES)) |
| 24 |
return list(reversed(keywords)) |
| 25 |
|
| 26 |
|
| 27 |
def update_cylc_lang(keywords, file_, template): |
| 28 |
"""Modify cylc.lang File. |
| 29 |
|
| 30 |
Creates a replacement keyword section for a language file and inserts |
| 31 |
it into the file. |
| 32 |
|
| 33 |
Args: |
| 34 |
keywords (list): |
| 35 |
A list of keywords. |
| 36 |
file_ (str): |
| 37 |
Name of file to modify. |
| 38 |
template (str): |
| 39 |
String template for each keyword line in the config file. |
| 40 |
Should contain {word}. |
| 41 |
""" |
| 42 |
filepath = LANG_FILE_PATH / file_ |
| 43 |
# Create a new keywords section. |
| 44 |
repl_text = '<!--TAG_FOR_AUTO_UPDATE-->\n' |
| 45 |
for word in keywords: |
| 46 |
repl_text += template.format(word=word) |
| 47 |
repl_text += ' <!--END_TAG_FOR_AUTO_UPDATE-->' |
| 48 |
|
| 49 |
# Replace the keywords section the language file. |
| 50 |
text = filepath.read_text() |
| 51 |
regex = ( |
| 52 |
r"(.*)(<!--TAG_FOR_AUTO_UPDATE-->.*<!--END_TAG_FOR_AUTO_UPDATE-->)(.*)" |
| 53 |
) |
| 54 |
text = re.match(regex, text, re.DOTALL | re.MULTILINE) |
| 55 |
text = text.groups()[0] + repl_text + text.groups()[2] |
| 56 |
filepath.write_text(text) |
| 57 |
|
| 58 |
|
| 59 |
def main(): |
| 60 |
keywords = get_keywords_from_workflow_cfg() |
| 61 |
update_cylc_lang( |
| 62 |
keywords, |
| 63 |
'cylc.lang', |
| 64 |
' <keyword>{word}</keyword>\n' |
| 65 |
) |
| 66 |
update_cylc_lang( |
| 67 |
keywords, |
| 68 |
'cylc.xml', |
| 69 |
" <RegExpr attribute='Keyword' String=' {word} '/>\n" |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
if __name__ == '__main__': |
| 74 |
main() |