Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid %% escape in xtrigger func args (7.8.x). #3257

Merged
merged 5 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Selected user-facing changes:

### Fixes

[#3257](https://github.com/cylc/cylc-flow/pull/3257) - leave '%'-escaped string
templates alone in xtrigger arguments.

[#3204](https://github.com/cylc/cylc-flow/pull/3204) - fix restart on
`cylc run --icp=now SUITE` and related, which was broken at 7.8.3.

Expand Down
4 changes: 4 additions & 0 deletions doc/src/external-triggers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ and less commonly needed:
- ``%(suite_run_dir)s`` - suite run directory
- ``%(suite_share_dir)s`` - suite share directory

If you need to pass a string template into an xtrigger function as a string
literal - i.e. to be used as a template inside the function - escape it with
``%`` to avoid detection by the Cylc xtrigger parser: ``%%(cat)s``.

Function return values should be as follows:

- if the trigger condition is *not satisfied*:
Expand Down
39 changes: 39 additions & 0 deletions lib/cylc/tests/test_xtrigger_mgr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python2

# THIS FILE IS PART OF THE CYLC SUITE ENGINE.
# Copyright (C) 2008-2019 NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest

from cylc.xtrigger_mgr import RE_STR_TMPL


class TestXtriggerManager(unittest.TestCase):

def test_extract_templates(self):
"""Test escaped templates in xtrigger arg string.

They should be left alone and passed into the function as string
literals, not identified as template args.
"""
self.assertEqual(
RE_STR_TMPL.findall('%(cat)s, %(dog)s, %%(fish)s'),
['cat', 'dog']
)


if __name__ == '__main__':
unittest.main()
5 changes: 3 additions & 2 deletions lib/cylc/xtrigger_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
TMPL_TASK_CYCLE_POINT, TMPL_TASK_IDENT, TMPL_TASK_NAME, TMPL_SUITE_RUN_DIR,
TMPL_SUITE_SHARE_DIR, TMPL_USER_NAME, TMPL_SUITE_NAME, TMPL_DEBUG_MODE]

# Extract all 'foo' from string templates '%(foo)s'.
RE_STR_TMPL = re.compile(r'%\(([\w]+)\)s')
# Extract 'foo' from string templates '%(foo)s', avoiding '%%' escaping
# ('%%(foo)s` is not a string template).
RE_STR_TMPL = re.compile(r'(?<!%)%\(([\w]+)\)s')
Copy link
Member Author

@hjoliver hjoliver Jul 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero-width negative look behind assertion, innit 😁



class XtriggerManager(object):
Expand Down