comparison env/lib/python3.7/site-packages/bleach/utils.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400 (2020-05-14)
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 from collections import OrderedDict
2
3 import six
4
5
6 def _attr_key(attr):
7 """Returns appropriate key for sorting attribute names
8
9 Attribute names are a tuple of ``(namespace, name)`` where namespace can be
10 ``None`` or a string. These can't be compared in Python 3, so we conver the
11 ``None`` to an empty string.
12
13 """
14 key = (attr[0][0] or ''), attr[0][1]
15 return key
16
17
18 def alphabetize_attributes(attrs):
19 """Takes a dict of attributes (or None) and returns them alphabetized"""
20 if not attrs:
21 return attrs
22
23 return OrderedDict(
24 [(k, v) for k, v in sorted(attrs.items(), key=_attr_key)]
25 )
26
27
28 def force_unicode(text):
29 """Takes a text (Python 2: str/unicode; Python 3: unicode) and converts to unicode
30
31 :arg str/unicode text: the text in question
32
33 :returns: text as unicode
34
35 :raises UnicodeDecodeError: if the text was a Python 2 str and isn't in
36 utf-8
37
38 """
39 # If it's already unicode, then return it
40 if isinstance(text, six.text_type):
41 return text
42
43 # If not, convert it
44 return six.text_type(text, 'utf-8', 'strict')