Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/bleach/utils.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:18:57 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d30785e31577 |
|---|---|
| 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') |
