comparison env/lib/python3.7/site-packages/bleach/utils.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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')