Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/rdflib/compat.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 """ | |
| 2 Utility functions and objects to ease Python 2/3 compatibility, | |
| 3 and different versions of support libraries. | |
| 4 """ | |
| 5 from __future__ import absolute_import | |
| 6 from __future__ import division | |
| 7 from __future__ import print_function | |
| 8 | |
| 9 import re | |
| 10 import codecs | |
| 11 import warnings | |
| 12 | |
| 13 import six | |
| 14 | |
| 15 | |
| 16 # clean ElementTree import | |
| 17 try: | |
| 18 from lxml import etree | |
| 19 except ImportError: | |
| 20 try: | |
| 21 # Python 2.5 | |
| 22 import xml.etree.cElementTree as etree | |
| 23 except ImportError: | |
| 24 try: | |
| 25 # Python 2.5 | |
| 26 import xml.etree.ElementTree as etree | |
| 27 except ImportError: | |
| 28 try: | |
| 29 # normal cElementTree install | |
| 30 import cElementTree as etree | |
| 31 except ImportError: | |
| 32 try: | |
| 33 # normal ElementTree install | |
| 34 import elementtree.ElementTree as etree | |
| 35 except ImportError: | |
| 36 raise Exception( | |
| 37 "Failed to import ElementTree from any known place") | |
| 38 | |
| 39 try: | |
| 40 etree_register_namespace = etree.register_namespace | |
| 41 except AttributeError: | |
| 42 | |
| 43 import xml.etree.ElementTree as etreenative | |
| 44 | |
| 45 def etree_register_namespace(prefix, uri): | |
| 46 etreenative._namespace_map[uri] = prefix | |
| 47 | |
| 48 | |
| 49 def cast_bytes(s, enc='utf-8'): | |
| 50 if isinstance(s, six.text_type): | |
| 51 return s.encode(enc) | |
| 52 return s | |
| 53 | |
| 54 | |
| 55 if six.PY3: | |
| 56 # Python 3: | |
| 57 # --------- | |
| 58 | |
| 59 def ascii(stream): | |
| 60 return codecs.getreader('ascii')(stream) | |
| 61 | |
| 62 def bopen(*args, **kwargs): | |
| 63 return open(*args, mode='rb', **kwargs) | |
| 64 | |
| 65 long_type = int | |
| 66 | |
| 67 def sign(n): | |
| 68 if n < 0: | |
| 69 return -1 | |
| 70 if n > 0: | |
| 71 return 1 | |
| 72 return 0 | |
| 73 | |
| 74 else: | |
| 75 # Python 2 | |
| 76 # -------- | |
| 77 | |
| 78 def ascii(stream): | |
| 79 return stream | |
| 80 | |
| 81 bopen = open | |
| 82 | |
| 83 long_type = long | |
| 84 | |
| 85 def sign(n): | |
| 86 return cmp(n, 0) | |
| 87 | |
| 88 r_unicodeEscape = re.compile(r'(\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})') | |
| 89 | |
| 90 | |
| 91 def _unicodeExpand(s): | |
| 92 return r_unicodeEscape.sub(lambda m: six.unichr(int(m.group(0)[2:], 16)), s) | |
| 93 | |
| 94 | |
| 95 narrow_build = False | |
| 96 try: | |
| 97 six.unichr(0x10FFFF) | |
| 98 except ValueError: | |
| 99 narrow_build = True | |
| 100 | |
| 101 if narrow_build: | |
| 102 def _unicodeExpand(s): | |
| 103 try: | |
| 104 return r_unicodeEscape.sub( | |
| 105 lambda m: six.unichr(int(m.group(0)[2:], 16)), s) | |
| 106 except ValueError: | |
| 107 warnings.warn( | |
| 108 'Encountered a unicode char > 0xFFFF in a narrow python build. ' | |
| 109 'Trying to degrade gracefully, but this can cause problems ' | |
| 110 'later when working with the string:\n%s' % s) | |
| 111 return r_unicodeEscape.sub( | |
| 112 lambda m: codecs.decode(m.group(0), 'unicode_escape'), s) | |
| 113 | |
| 114 | |
| 115 def decodeStringEscape(s): | |
| 116 """ | |
| 117 s is byte-string - replace \ escapes in string | |
| 118 """ | |
| 119 | |
| 120 if not six.PY3: | |
| 121 s = s.decode('string-escape') | |
| 122 else: | |
| 123 s = s.replace('\\t', '\t') | |
| 124 s = s.replace('\\n', '\n') | |
| 125 s = s.replace('\\r', '\r') | |
| 126 s = s.replace('\\b', '\b') | |
| 127 s = s.replace('\\f', '\f') | |
| 128 s = s.replace('\\"', '"') | |
| 129 s = s.replace("\\'", "'") | |
| 130 s = s.replace('\\\\', '\\') | |
| 131 | |
| 132 return s | |
| 133 # return _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping | |
| 134 | |
| 135 | |
| 136 def decodeUnicodeEscape(s): | |
| 137 """ | |
| 138 s is a unicode string | |
| 139 replace ``\\n`` and ``\\u00AC`` unicode escapes | |
| 140 """ | |
| 141 if not six.PY3: | |
| 142 s = s.encode('utf-8').decode('string-escape') | |
| 143 s = _unicodeExpand(s) | |
| 144 else: | |
| 145 s = s.replace('\\t', '\t') | |
| 146 s = s.replace('\\n', '\n') | |
| 147 s = s.replace('\\r', '\r') | |
| 148 s = s.replace('\\b', '\b') | |
| 149 s = s.replace('\\f', '\f') | |
| 150 s = s.replace('\\"', '"') | |
| 151 s = s.replace("\\'", "'") | |
| 152 s = s.replace('\\\\', '\\') | |
| 153 | |
| 154 s = _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping | |
| 155 | |
| 156 return s | |
| 157 | |
| 158 | |
| 159 # Migration to abc in Python 3.8 | |
| 160 try: | |
| 161 from collections.abc import Mapping, MutableMapping | |
| 162 except: | |
| 163 from collections import Mapping, MutableMapping |
