diff --git a/src/linkahead/utils/escape.py b/src/linkahead/utils/escape.py index d6b206fc458b7ba002603fafe51c3a7835202920..cae9eb9f9baade6a4490a82f61631705fdf485fd 100644 --- a/src/linkahead/utils/escape.py +++ b/src/linkahead/utils/escape.py @@ -19,13 +19,15 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. # +import warnings -def escape_quoted_text(text: str) -> str: - """The characters ``\\`` and ``*`` need to be escaped if used in quoted expressions in the query - language. - This function return the given string where the characters ``\\`` and ``*`` are escaped by - a ``\\``. +def escape_squoted_text(text: str) -> str: + """The characters ``\\``, ``*`` and ``'`` need to be escaped if used in single quoted + expressions in the query language. + + This function returns the given string where the characters ``\\``, ``'`` and ``*`` are + escaped by a ``\\``. Parameters ---------- @@ -37,4 +39,32 @@ def escape_quoted_text(text: str) -> str: str The escaped text """ - return text.replace('\\', r'\\').replace('*', r'\*') + return text.replace('\\', r'\\').replace("'", "\\'").replace('*', r'\*') + + +def escape_dquoted_text(text: str) -> str: + """The characters ``\\``, ``*`` and ``"`` need to be escaped if used in double quoted + expressions in the query language. + + This function returns the given string where the characters ``\\``, ``"`` and ``*`` are + escaped by a ``\\``. + + Parameters + ---------- + text : str + The text to be escaped + + Returns + ------- + str + The escaped text + """ + return text.replace('\\', r'\\').replace('"', '\\"').replace('*', r'\*') + + +def escape_quoted_text(text: str) -> str: + """ + Please use escape_squoted_text or escape_dquoted_text + """ + warnings.warn("Please use escape_squoted_text or escape_dquoted_text", DeprecationWarning) + return escape_squoted_text(text) diff --git a/unittests/test_utils.py b/unittests/test_utils.py index 4fb223f7c71cd2ed2871016e776a5a25e5bc9001..fe4c9d031c22f608f63d181f2bbd53174366d7a2 100644 --- a/unittests/test_utils.py +++ b/unittests/test_utils.py @@ -25,7 +25,8 @@ from __future__ import unicode_literals from linkahead.common.utils import xml2str -from linkahead.utils.escape import escape_quoted_text +from linkahead.utils.escape import (escape_dquoted_text, escape_quoted_text, + escape_squoted_text) from lxml.etree import Element @@ -41,3 +42,5 @@ def test_escape_quoted_text(): assert escape_quoted_text("bl\\a") == "bl\\\\a" assert escape_quoted_text("bl*a") == "bl\\*a" assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla" + assert escape_squoted_text("bl'a") == "bl\\'a" + assert escape_dquoted_text('bl"a') == 'bl\\"a'