From 1e5917089064fcea522d8ec1e5d5a31c6a4e20d7 Mon Sep 17 00:00:00 2001 From: DavyCats Date: Fri, 31 Jan 2020 10:48:23 +0100 Subject: [PATCH] Use POSIX ERE for sub function (#321) implements openwdl/wdl#243 --- WDL/StdLib.py | 6 ++++-- requirements.txt | 1 + stubs/regex/__init__.py | 7 +++++++ tests/test_5stdlib.py | 6 +++++- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 stubs/regex/__init__.py diff --git a/WDL/StdLib.py b/WDL/StdLib.py index 0316016d..bfdee179 100644 --- a/WDL/StdLib.py +++ b/WDL/StdLib.py @@ -1,7 +1,7 @@ # pylint: disable=protected-access,exec-used import math import os -import re +import regex import json import tempfile from typing import List, Tuple, Callable, BinaryIO, Optional @@ -67,7 +67,9 @@ def static( @static([Type.String(), Type.String(), Type.String()], Type.String()) def sub(input: Value.String, pattern: Value.String, replace: Value.String) -> Value.String: - return Value.String(re.compile(pattern.value).sub(replace.value, input.value)) + return Value.String( + regex.compile(pattern.value, flags=regex.POSIX).sub(replace.value, input.value) + ) static([Type.String(), Type.String(optional=True)], Type.String())(basename) diff --git a/requirements.txt b/requirements.txt index 9c31c821..4523faab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ pygtail>=0.11.0 coloredlogs psutil importlib-metadata +regex diff --git a/stubs/regex/__init__.py b/stubs/regex/__init__.py new file mode 100644 index 00000000..4c0cbcc0 --- /dev/null +++ b/stubs/regex/__init__.py @@ -0,0 +1,7 @@ +from typing import Any + +POSIX: int + + +def compile(pattern, flags=0, **kwargs) -> Any: + ... diff --git a/tests/test_5stdlib.py b/tests/test_5stdlib.py index a19d04c5..fb6a4476 100644 --- a/tests/test_5stdlib.py +++ b/tests/test_5stdlib.py @@ -220,6 +220,8 @@ def test_sub(self): String chocolove = sub(chocolike, "like", "love") # I love chocolate when it's late String chocoearly = sub(chocolike, "late", "early") # I like chocoearly when it's early String chocolate = sub(chocolike, "late$", "early") # I like chocolate when it's early + String chocoearlylate = sub(chocolike, "[^ ]late", "early") # I like chocearly when it's late + String choco4 = sub(chocolike, " [[:alpha:]]{4} ", " 4444 ") # I 4444 chocolate 4444 it's late } } """) @@ -227,7 +229,9 @@ def test_sub(self): "chocolike": "I like chocolate when it's late", "chocolove": "I love chocolate when it's late", "chocoearly": "I like chocoearly when it's early", - "chocolate": "I like chocolate when it's early" + "chocolate": "I like chocolate when it's early", + "chocoearlylate": "I like chocearly when it's late", + "choco4": "I 4444 chocolate 4444 it's late" }) outputs = self._test_task(R""" task example {