changeset 360951b6b5cd in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=360951b6b5cd
description:
        Add escape_wildcard in tools

        issue9463
        review306021002
diffstat:

 CHANGELOG                   |  1 +
 trytond/tests/test_tools.py |  8 +++++++-
 trytond/tools/__init__.py   |  5 +++--
 trytond/tools/misc.py       |  6 ++++++
 4 files changed, 17 insertions(+), 3 deletions(-)

diffs (70 lines):

diff -r 8d3d023fc121 -r 360951b6b5cd CHANGELOG
--- a/CHANGELOG Tue Jul 21 14:33:49 2020 +0200
+++ b/CHANGELOG Tue Jul 21 14:37:01 2020 +0200
@@ -1,3 +1,4 @@
+* Add escape_wildcard in tools
 * Add option to ensure emails are sent
 * Allow keyword action for all models
 * Add sortable_values in tools
diff -r 8d3d023fc121 -r 360951b6b5cd trytond/tests/test_tools.py
--- a/trytond/tests/test_tools.py       Tue Jul 21 14:33:49 2020 +0200
+++ b/trytond/tests/test_tools.py       Tue Jul 21 14:37:01 2020 +0200
@@ -12,7 +12,8 @@
 
 from trytond.tools import (
     reduce_ids, reduce_domain, decimal_, is_instance_method, file_open,
-    strip_wildcard, lstrip_wildcard, rstrip_wildcard, slugify, sortable_values)
+    strip_wildcard, lstrip_wildcard, rstrip_wildcard, slugify, sortable_values,
+    escape_wildcard)
 from trytond.tools.string_ import StringPartitioned, LazyString
 from trytond.tools.domain_inversion import (
     domain_inversion, parse, simplify, merge, concat, unique_value,
@@ -195,6 +196,11 @@
     def test_rstrip_wildcard_diferent_wildcard(self):
         self.assertEqual(rstrip_wildcard('a___', '_'), 'a')
 
+    def test_escape_wildcard(self):
+        self.assertEqual(
+            escape_wildcard('foo%bar_baz\\'),
+            'foo\\%bar\\_baz\\\\')
+
     def test_slugify(self):
         "Test slugify"
         self.assertEqual(slugify('unicode ♥ is ☢'), 'unicode-is')
diff -r 8d3d023fc121 -r 360951b6b5cd trytond/tools/__init__.py
--- a/trytond/tools/__init__.py Tue Jul 21 14:33:49 2020 +0200
+++ b/trytond/tools/__init__.py Tue Jul 21 14:37:01 2020 +0200
@@ -8,14 +8,15 @@
 from .misc import (
     file_open, get_smtp_server, reduce_ids, reduce_domain,
     grouped_slice, is_instance_method, resolve, strip_wildcard,
-    lstrip_wildcard, rstrip_wildcard, slugify, sortable_values)
+    lstrip_wildcard, rstrip_wildcard, slugify, sortable_values,
+    escape_wildcard)
 from .decimal_ import decistmt
 
 __all__ = ['file_open', 'get_smtp_server', 'reduce_ids',
     'reduce_domain', 'grouped_slice', 'is_instance_method', 'resolve',
     'strip_wildcard', 'lstrip_wildcard', 'rstrip_wildcard', 'slugify',
     'decistmt', 'ClassProperty', 'cursor_dict', 'cached_property',
-    'sortable_values']
+    'sortable_values', 'escape_wildcard']
 
 
 class ClassProperty(property):
diff -r 8d3d023fc121 -r 360951b6b5cd trytond/tools/misc.py
--- a/trytond/tools/misc.py     Tue Jul 21 14:33:49 2020 +0200
+++ b/trytond/tools/misc.py     Tue Jul 21 14:37:01 2020 +0200
@@ -214,6 +214,12 @@
     return new_string
 
 
+def escape_wildcard(string, wildcards='%_', escape='\\'):
+    for wildcard in escape + wildcards:
+        string = string.replace(wildcard, escape + wildcard)
+    return string
+
+
 _slugify_strip_re = re.compile(r'[^\w\s-]')
 _slugify_hyphenate_re = re.compile(r'[-\s]+')
 

Reply via email to