changeset d85963a57040 in trytond:default details: https://hg.tryton.org/trytond?cmd=changeset;node=d85963a57040 description: Add strip wildcards helpers
issue7347 review64541002 diffstat: CHANGELOG | 1 + trytond/tests/test_tools.py | 56 ++++++++++++++++++++++++++++++++++++++++++++- trytond/tools/misc.py | 23 ++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletions(-) diffs (111 lines): diff -r 0e0591b85c0e -r d85963a57040 CHANGELOG --- a/CHANGELOG Thu Apr 11 13:50:56 2019 +0200 +++ b/CHANGELOG Thu Apr 11 15:17:30 2019 +0200 @@ -1,3 +1,4 @@ +* Add strip wildcards helpers * Add list-form view * Do not set id for on_change calls if cached * Add cache on RPC diff -r 0e0591b85c0e -r d85963a57040 trytond/tests/test_tools.py --- a/trytond/tests/test_tools.py Thu Apr 11 13:50:56 2019 +0200 +++ b/trytond/tests/test_tools.py Thu Apr 11 15:17:30 2019 +0200 @@ -8,7 +8,8 @@ import sql.operators from trytond.tools import ( - reduce_ids, reduce_domain, decimal_, is_instance_method, file_open) + reduce_ids, reduce_domain, decimal_, is_instance_method, file_open, + strip_wildcard, lstrip_wildcard, rstrip_wildcard) from trytond.tools.string_ import StringPartitioned @@ -132,6 +133,59 @@ with self.assertRaisesRegex(IOError, "Permission denied:"): file_open('../trytond_suffix', subdir=None) + def test_strip_wildcard(self): + 'Test strip wildcard' + for clause, result in [ + ('%a%', 'a'), + ('%%%%a%%%', 'a'), + ('\\%a%', '\\%a'), + ('\\%a\\%', '\\%a\\%'), + ('a', 'a'), + ('', ''), + (None, None), + ]: + self.assertEqual( + strip_wildcard(clause), result, msg=clause) + + def test_strip_wildcard_different_wildcard(self): + 'Test strip wildcard with different wildcard' + self.assertEqual(strip_wildcard('___a___', '_'), 'a') + + def test_lstrip_wildcard(self): + 'Test lstrip wildcard' + for clause, result in [ + ('%a', 'a'), + ('%a%', 'a%'), + ('%%%%a%', 'a%'), + ('\\%a%', '\\%a%'), + ('a', 'a'), + ('', ''), + (None, None), + ]: + self.assertEqual( + lstrip_wildcard(clause), result, msg=clause) + + def test_lstrip_wildcard_different_wildcard(self): + 'Test lstrip wildcard with different wildcard' + self.assertEqual(lstrip_wildcard('___a', '_'), 'a') + + def test_rstrip_wildcard(self): + 'Test rstrip wildcard' + for clause, result in [ + ('a%', 'a'), + ('%a%', '%a'), + ('%a%%%%%', '%a'), + ('%a\\%', '%a\\%'), + ('a', 'a'), + ('', ''), + (None, None), + ]: + self.assertEqual( + rstrip_wildcard(clause), result, msg=clause) + + def test_rstrip_wildcard_diferent_wildcard(self): + self.assertEqual(rstrip_wildcard('a___', '_'), 'a') + class StringPartitionedTestCase(unittest.TestCase): "Test StringPartitioned" diff -r 0e0591b85c0e -r d85963a57040 trytond/tools/misc.py --- a/trytond/tools/misc.py Thu Apr 11 13:50:56 2019 +0200 +++ b/trytond/tools/misc.py Thu Apr 11 15:17:30 2019 +0200 @@ -248,3 +248,26 @@ except AttributeError: found = importlib.import_module(used) return found + + +def strip_wildcard(string, wildcard='%', escape='\\'): + "Strip starting and ending wildcard from string" + string = lstrip_wildcard(string, wildcard) + return rstrip_wildcard(string, wildcard, escape) + + +def lstrip_wildcard(string, wildcard='%'): + "Strip starting wildcard from string" + if not string: + return string + return string.lstrip(wildcard) + + +def rstrip_wildcard(string, wildcard='%', escape='\\'): + "Strip ending wildcard from string" + if not string: + return string + new_string = string.rstrip(wildcard) + if new_string[-1] == escape: + return string + return new_string