本文整理汇总了Python中stringprep.in_table_c12函数的典型用法代码示例。如果您正苦于以下问题:Python in_table_c12函数的具体用法?Python in_table_c12怎么用?Python in_table_c12使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了in_table_c12函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: saslprep
def saslprep(s, allow_unassigned = False):
'''
Prepare Unicode string s according to SASLprep: Stringprep Profile for
User Names and Passwords, a.k.a. RFC 4013
If the optional parameter allow_unassigned is set to True,
unassigned codepoints will be allowed. This is recommended for
query terms and other non-storing situations only.
The return value is a Unicode string appropriately prepared.
Disallowed input leads to a ValueError.
'''
if type(s) != type(u''):
raise TypeError("input must be a Unicode string")
# phase 1: mapping
s = u''.join([ stringprep.in_table_c12(ch) and u' ' or ch for ch in unichars(s) if not stringprep.in_table_b1(ch) ])
# phase 2: normalization
s = unicodedata.normalize('NFKC', s)
# phase 3: prohibition
for ch in unichars(s):
if stringprep.in_table_c12(ch):
raise ValueError("prohibited non-ASCII space character")
if stringprep.in_table_c21(ch):
raise ValueError("prohibited ASCII control character")
if stringprep.in_table_c22(ch):
raise ValueError("prohibited non-ASCII control character")
if stringprep.in_table_c3(ch):
raise ValueError("prohibited private use character")
if stringprep.in_table_c4(ch):
raise ValueError("prohibited non-character code point")
if stringprep.in_table_c5(ch):
raise ValueError("prohibited surrogate code point")
if stringprep.in_table_c6(ch):
raise ValueError("prohibited character inappropriate for plain text")
if stringprep.in_table_c7(ch):
raise ValueError("prohibited character inappropriate for canonical representation")
if stringprep.in_table_c8(ch):
raise ValueError("prohibited character changing display properties, or a deprecated character")
if stringprep.in_table_c9(ch):
raise ValueError("prohibited tagging character")
# phase 4: bidi check
bidi_map = ''.join([ stringprep.in_table_d1(ch) and 'r' or stringprep.in_table_d2(ch) and 'l' or 'x' for ch in unichars(s) ])
if 'r' in bidi_map:
if 'l' in bidi_map:
raise ValueError("prohibited mixture of strong left-to-right and right-to-left text")
if bidi_map[0] != 'r' or bidi_map[-1] != 'r':
raise ValueError("string containing right-to-left text must start and end with right-to-left text")
# phase 5: unassigned check
if not allow_unassigned:
for ch in unichars(s):
if stringprep.in_table_a1(ch):
raise ValueError("prohibited unassigned code point")
return s
示例2: saslprep
def saslprep( foo, errors='strict' ):
if isinstance( foo, str ):
foo = foo.decode( 'us-ascii' )
ofoo = u''
for x in foo:
if stringprep.in_table_c12( x ):
ofoo += ' '
elif not stringprep.in_table_b1( x ):
ofoo += x
foo = unicodedata.normalize( 'NFKC', ofoo )
ofoo = u''
first_is_randal = False
if len(foo):
first_is_randal = stringprep.in_table_d1( foo[0] )
if first_is_randal:
if not stringprep.in_table_d1( foo[-1] ):
raise UnicodeError, "Section 6.3 [end]"
for x in range(len(foo)):
if errors=='strict' and stringprep.in_table_a1( foo[x] ):
raise UnicodeError, "Unassigned Codepoint"
if stringprep.in_table_c12( foo[x] ):
raise UnicodeError, "In table C.1.2"
if stringprep.in_table_c21( foo[x] ):
raise UnicodeError, "In table C.2.1"
if stringprep.in_table_c22( foo[x] ):
raise UnicodeError, "In table C.2.2"
if stringprep.in_table_c3( foo[x] ):
raise UnicodeError, "In table C.3"
if stringprep.in_table_c4( foo[x] ):
raise UnicodeError, "In table C.4"
if stringprep.in_table_c5( foo[x] ):
raise UnicodeError, "In table C.5"
if stringprep.in_table_c6( foo[x] ):
raise UnicodeError, "In table C.6"
if stringprep.in_table_c7( foo[x] ):
raise UnicodeError, "In table C.7"
if stringprep.in_table_c8( foo[x] ):
raise UnicodeError, "In table C.8"
if stringprep.in_table_c9( foo[x] ):
raise UnicodeError, "In table C.9"
if x:
if first_is_randal and stringprep.in_table_d2( foo[x] ):
raise UnicodeError, "Section 6.2"
if not first_is_randal and x!=(len(foo)-1) and stringprep.in_table_d1( foo[x] ):
raise UnicodeError, "Section 6.3"
else:
first = False
return foo
示例3: resourceprep
def resourceprep(res):
chars = list(unicode(res))
i = 0
while i < len(chars):
c = chars[i]
# map to nothing
if stringprep.in_table_b1(c):
del chars[i]
else:
i += 1
# NFKC
chars = stringprep.unicodedata.normalize("NFKC", "".join(chars))
for c in chars:
if (stringprep.in_table_c12(c) or
stringprep.in_table_c21(c) or
stringprep.in_table_c22(c) or
stringprep.in_table_c3(c) or
stringprep.in_table_c4(c) or
stringprep.in_table_c5(c) or
stringprep.in_table_c6(c) or
stringprep.in_table_c7(c) or
stringprep.in_table_c8(c) or
stringprep.in_table_c9(c)):
raise UnicodeError("Invalid node character")
bidi(chars)
return chars
示例4: nodeprep
def nodeprep(u):
chars = list(unicode(u))
i = 0
while i < len(chars):
c = chars[i]
# map to nothing
if stringprep.in_table_b1(c):
del chars[i]
else:
# case fold
chars[i] = stringprep.map_table_b2(c)
i += 1
# NFKC
chars = stringprep.unicodedata.normalize("NFKC", "".join(chars))
for c in chars:
if (stringprep.in_table_c11(c) or
stringprep.in_table_c12(c) or
stringprep.in_table_c21(c) or
stringprep.in_table_c22(c) or
stringprep.in_table_c3(c) or
stringprep.in_table_c4(c) or
stringprep.in_table_c5(c) or
stringprep.in_table_c6(c) or
stringprep.in_table_c7(c) or
stringprep.in_table_c8(c) or
stringprep.in_table_c9(c) or
c in "\"&'/:<>@"):
raise UnicodeError("Invalid node character")
bidi(chars)
return chars
示例5: _map_saslprep
def _map_saslprep(s):
"""Map stringprep table B.1 to nothing and C.1.2 to ASCII space"""
r = []
for c in s:
if stringprep.in_table_c12(c):
r.append(' ')
elif not stringprep.in_table_b1(c):
r.append(c)
return ''.join(r)
示例6: c12_mapping
def c12_mapping(char):
"""Do mapping of RFC 3454 C.1.2 space characters to ' '.
:Parameters:
- `char`: Unicode character to map.
:returns: u" " if there is `char` code in the table, `None` otherwise.
"""
if stringprep.in_table_c12(char):
return u" "
else:
return None
示例7: _process
def _process(self, value):
for code in force_text(value):
# TODO: Is this long enough?
if stringprep.in_table_c12(code) or stringprep.in_table_c21_c22(code) or \
stringprep.in_table_c3(code) or stringprep.in_table_c4(code) or \
stringprep.in_table_c5(code) or stringprep.in_table_c6(code) or \
stringprep.in_table_c7(code) or stringprep.in_table_c8(code) or \
stringprep.in_table_c9(code):
self.invalid = False
if stringprep.in_table_d1(code):
self.r_and_al_cat = True
if stringprep.in_table_d2(code):
self.l_cat = True
示例8: _saslprep_do_mapping
def _saslprep_do_mapping(chars):
"""
Perform the stringprep mapping step of SASLprep. Operates in-place on a
list of unicode characters provided in `chars`.
"""
i = 0
while i < len(chars):
c = chars[i]
if stringprep.in_table_c12(c):
chars[i] = "\u0020"
elif stringprep.in_table_b1(c):
del chars[i]
continue
i += 1
示例9: nameprep
def nameprep(label):
# Map
newlabel = []
for c in label:
if stringprep.in_table_b1(c):
# Map to nothing
continue
newlabel.append(stringprep.map_table_b2(c))
label = "".join(newlabel)
# Normalize
label = unicodedata.normalize("NFKC", label)
# Prohibit
for c in label:
if (
stringprep.in_table_c12(c)
or stringprep.in_table_c22(c)
or stringprep.in_table_c3(c)
or stringprep.in_table_c4(c)
or stringprep.in_table_c5(c)
or stringprep.in_table_c6(c)
or stringprep.in_table_c7(c)
or stringprep.in_table_c8(c)
or stringprep.in_table_c9(c)
):
raise UnicodeError("Invalid character %r" % c)
# Check bidi
RandAL = [stringprep.in_table_d1(x) for x in label]
for c in RandAL:
if c:
# There is a RandAL char in the string. Must perform further
# tests:
# 1) The characters in section 5.8 MUST be prohibited.
# This is table C.8, which was already checked
# 2) If a string contains any RandALCat character, the string
# MUST NOT contain any LCat character.
if any(stringprep.in_table_d2(x) for x in label):
raise UnicodeError("Violation of BIDI requirement 2")
# 3) If a string contains any RandALCat character, a
# RandALCat character MUST be the first character of the
# string, and a RandALCat character MUST be the last
# character of the string.
if not RandAL[0] or not RandAL[-1]:
raise UnicodeError("Violation of BIDI requirement 3")
return label
示例10: resource_validator
def resource_validator(name):
"""Check the *name* of a resource for some really bad characters that shouldn't be used
anywhere in RestAuth.
This filters names containing a slash ("/") or colon (":") and those starting with '.'. It also
filters control characters etc., including those from unicode.
.. deprecated:: 0.7.0
This method is deprecated in favour of the RestAuthCommon.strprep. This method will be
removed in 0.7.1.
:param str name: The name to validate
:returns: False if the name contains any invalid characters, True otherwise.
:rtype: bool
"""
warnings.warn('This method is deprecated, use RestAuthCommon.strprep.stringcheck() instead.',
DeprecationWarning)
if PY2 and isinstance(name, str): # pragma: py2
name = name.decode('utf-8')
# filter various dangerous characters
for enc_char in name:
if stringprep.in_table_c12(enc_char): # C.1.2 Non-ASCII space characters
return False
if stringprep.in_table_c21_c22(enc_char): # C.2 Control characters
return False
if stringprep.in_table_c3(enc_char): # C.3 Private use
return False
if stringprep.in_table_c4(enc_char): # C.4 Non-character code points
return False
if stringprep.in_table_c5(enc_char): # C.5 Surrogate codes
return False
if stringprep.in_table_c6(enc_char): # C.6 Inappropriate for plain text
return False
if stringprep.in_table_c7(enc_char): # C.7 Inappropriate for canonical representation
return False
if stringprep.in_table_c8(enc_char): # C.8 Change display properties or are deprecated
return False
if stringprep.in_table_c9(enc_char): # C.9 Tagging characters
return False
return True
示例11: nameprep
def nameprep(label):
newlabel = []
for c in label:
if stringprep.in_table_b1(c):
pass
newlabel.append(stringprep.map_table_b2(c))
label = ''.join(newlabel)
label = unicodedata.normalize('NFKC', label)
for c in label:
while stringprep.in_table_c12(c) or (stringprep.in_table_c22(c) or (stringprep.in_table_c3(c) or (stringprep.in_table_c4(c) or (stringprep.in_table_c5(c) or (stringprep.in_table_c6(c) or (stringprep.in_table_c7(c) or stringprep.in_table_c8(c))))))) or stringprep.in_table_c9(c):
raise UnicodeError('Invalid character %r' % c)
RandAL = [stringprep.in_table_d1(x) for x in label]
for c in RandAL:
while c:
if any(stringprep.in_table_d2(x) for x in label):
raise UnicodeError('Violation of BIDI requirement 2')
if not RandAL[0] or not RandAL[-1]:
raise UnicodeError('Violation of BIDI requirement 3')
return label
示例12: nameprep
def nameprep(label):
newlabel = []
for c in label:
if stringprep.in_table_b1(c):
continue
newlabel.append(stringprep.map_table_b2(c))
label = u''.join(newlabel)
label = unicodedata.normalize('NFKC', label)
for c in label:
if stringprep.in_table_c12(c) or stringprep.in_table_c22(c) or stringprep.in_table_c3(c) or stringprep.in_table_c4(c) or stringprep.in_table_c5(c) or stringprep.in_table_c6(c) or stringprep.in_table_c7(c) or stringprep.in_table_c8(c) or stringprep.in_table_c9(c):
raise UnicodeError('Invalid character %r' % c)
RandAL = map(stringprep.in_table_d1, label)
for c in RandAL:
if c:
if filter(stringprep.in_table_d2, label):
raise UnicodeError('Violation of BIDI requirement 2')
if not RandAL[0] or not RandAL[-1]:
raise UnicodeError('Violation of BIDI requirement 3')
return label
示例13: c12_mapping
def c12_mapping(char):
"""Map non-ASCII whitespace to spaces."""
return ' ' if stringprep.in_table_c12(char) else None
示例14: c12_mapping
def c12_mapping(char):
return ' ' if stringprep.in_table_c12(char) else None
示例15: saslprep
def saslprep(text, strict=True):
"""
Return a processed version of the given string, using the SASLPrep
profile of stringprep.
:param text: The string to process, in UTF-8.
:param strict: If ``True``, prevent the use of unassigned code points.
"""
if sys.version_info < (3, 0):
if type(text) == str:
text = text.decode("utf-8")
# Mapping:
#
# - non-ASCII space characters [StringPrep, C.1.2] that can be
# mapped to SPACE (U+0020), and
#
# - the 'commonly mapped to nothing' characters [StringPrep, B.1]
# that can be mapped to nothing.
buffer = ""
for char in text:
if stringprep.in_table_c12(char):
buffer += " "
elif not stringprep.in_table_b1(char):
buffer += char
# Normalization using form KC
text = unicodedata.normalize("NFKC", buffer)
# Check for bidirectional string
buffer = ""
first_is_randal = False
if text:
first_is_randal = stringprep.in_table_d1(text[0])
if first_is_randal and not stringprep.in_table_d1(text[-1]):
raise SASLPrepFailure("Section 6.3 [end]")
# Check for prohibited characters
for x in range(len(text)):
if strict and stringprep.in_table_a1(text[x]):
raise SASLPrepFailure("Unassigned Codepoint")
if stringprep.in_table_c12(text[x]):
raise SASLPrepFailure("In table C.1.2")
if stringprep.in_table_c21(text[x]):
raise SASLPrepFailure("In table C.2.1")
if stringprep.in_table_c22(text[x]):
raise SASLPrepFailure("In table C.2.2")
if stringprep.in_table_c3(text[x]):
raise SASLPrepFailure("In table C.3")
if stringprep.in_table_c4(text[x]):
raise SASLPrepFailure("In table C.4")
if stringprep.in_table_c5(text[x]):
raise SASLPrepFailure("In table C.5")
if stringprep.in_table_c6(text[x]):
raise SASLPrepFailure("In table C.6")
if stringprep.in_table_c7(text[x]):
raise SASLPrepFailure("In table C.7")
if stringprep.in_table_c8(text[x]):
raise SASLPrepFailure("In table C.8")
if stringprep.in_table_c9(text[x]):
raise SASLPrepFailure("In table C.9")
if x:
if first_is_randal and stringprep.in_table_d2(text[x]):
raise SASLPrepFailure("Section 6.2")
if not first_is_randal and x != len(text) - 1 and stringprep.in_table_d1(text[x]):
raise SASLPrepFailure("Section 6.3")
return text