comparison env/lib/python3.7/site-packages/bleach/utils.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
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')