Move character escaping function to ipautil

Functions `escape_seq` and `unescape_seq` have a generic use-case so it makes
sense to move them from `kerberos` to ipautil module so that other modules can
reuse them more readily.

https://fedorahosted.org/freeipa/ticket/5809

Reviewed-By: Tomas Krizek <tkrizek@redhat.com>
This commit is contained in:
Martin Babinsky 2016-09-23 15:53:41 +02:00
parent d982710bec
commit a6833222ff
2 changed files with 29 additions and 27 deletions

View File

@ -1487,3 +1487,30 @@ def is_fips_enabled():
# Consider that the host is not fips-enabled if the file does not exist
pass
return False
def unescape_seq(seq, *args):
"""
unescape (remove '\\') all occurences of sequence in input strings.
:param seq: sequence to unescape
:param args: input string to process
:returns: tuple of strings with unescaped sequences
"""
unescape_re = re.compile(r'\\{}'.format(seq))
return tuple(re.sub(unescape_re, seq, a) for a in args)
def escape_seq(seq, *args):
"""
escape (prepend '\\') all occurences of sequence in input strings
:param seq: sequence to escape
:param args: input string to process
:returns: tuple of strings with escaped sequences
"""
return tuple(a.replace(seq, u'\\{}'.format(seq)) for a in args)

View File

@ -8,6 +8,8 @@ classes/utils for Kerberos principal name validation/manipulation
import re
import six
from ipapython.ipautil import escape_seq, unescape_seq
if six.PY3:
unicode = str
@ -58,33 +60,6 @@ def split_principal_name(principal_name):
return tuple(COMPONENT_SPLIT_RE.split(principal_name))
def unescape_seq(seq, *args):
"""
unescape (remove '\\') all occurences of sequence in input strings.
:param seq: sequence to unescape
:param args: input string to process
:returns: tuple of strings with unescaped sequences
"""
unescape_re = re.compile(r'\\{}'.format(seq))
return tuple(re.sub(unescape_re, seq, a) for a in args)
def escape_seq(seq, *args):
"""
escape (prepend '\\') all occurences of sequence in input strings
:param seq: sequence to escape
:param args: input string to process
:returns: tuple of strings with escaped sequences
"""
return tuple(a.replace(seq, u'\\{}'.format(seq)) for a in args)
@six.python_2_unicode_compatible
class Principal(object):
"""