comparison env/lib/python3.9/site-packages/bleach/utils.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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([(k, v) for k, v in sorted(attrs.items(), key=_attr_key)])
24
25
26 def force_unicode(text):
27 """Takes a text (Python 2: str/unicode; Python 3: unicode) and converts to unicode
28
29 :arg str/unicode text: the text in question
30
31 :returns: text as unicode
32
33 :raises UnicodeDecodeError: if the text was a Python 2 str and isn't in
34 utf-8
35
36 """
37 # If it's already unicode, then return it
38 if isinstance(text, six.text_type):
39 return text
40
41 # If not, convert it
42 return six.text_type(text, "utf-8", "strict")