| OLD | NEW |
| 1 # markdown is released under the BSD license | |
| 2 # Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) | |
| 3 # Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) | |
| 4 # Copyright 2004 Manfred Stienstra (the original version) | |
| 5 # | |
| 6 # All rights reserved. | |
| 7 # | |
| 8 # Redistribution and use in source and binary forms, with or without | |
| 9 # modification, are permitted provided that the following conditions are met: | |
| 10 # | |
| 11 # * Redistributions of source code must retain the above copyright | |
| 12 # notice, this list of conditions and the following disclaimer. | |
| 13 # * Redistributions in binary form must reproduce the above copyright | |
| 14 # notice, this list of conditions and the following disclaimer in the | |
| 15 # documentation and/or other materials provided with the distribution. | |
| 16 # * Neither the name of the <organization> nor the | |
| 17 # names of its contributors may be used to endorse or promote products | |
| 18 # derived from this software without specific prior written permission. | |
| 19 # | |
| 20 # THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY | |
| 21 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 23 # DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT | |
| 24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 # POSSIBILITY OF SUCH DAMAGE. | |
| 31 | |
| 32 | |
| 33 from __future__ import unicode_literals | 1 from __future__ import unicode_literals |
| 34 from __future__ import absolute_import | 2 from __future__ import absolute_import |
| 35 from . import util | 3 from . import util |
| 36 | |
| 37 from copy import deepcopy | 4 from copy import deepcopy |
| 38 | 5 |
| 39 def iteritems_compat(d): | |
| 40 """Return an iterator over the (key, value) pairs of a dictionary. | |
| 41 Copied from `six` module.""" | |
| 42 return iter(getattr(d, _iteritems)()) | |
| 43 | 6 |
| 44 class OrderedDict(dict): | 7 class OrderedDict(dict): |
| 45 """ | 8 """ |
| 46 A dictionary that keeps its keys in the order in which they're inserted. | 9 A dictionary that keeps its keys in the order in which they're inserted. |
| 47 | 10 |
| 48 Copied from Django's SortedDict with some modifications. | 11 Copied from Django's SortedDict with some modifications. |
| 49 | 12 |
| 50 """ | 13 """ |
| 51 def __new__(cls, *args, **kwargs): | 14 def __new__(cls, *args, **kwargs): |
| 52 instance = super(OrderedDict, cls).__new__(cls, *args, **kwargs) | 15 instance = super(OrderedDict, cls).__new__(cls, *args, **kwargs) |
| 53 instance.keyOrder = [] | 16 instance.keyOrder = [] |
| 54 return instance | 17 return instance |
| 55 | 18 |
| 56 def __init__(self, data=None): | 19 def __init__(self, data=None): |
| 57 if data is None or isinstance(data, dict): | 20 if data is None or isinstance(data, dict): |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 yield key, self[key] | 75 yield key, self[key] |
| 113 | 76 |
| 114 def _iterkeys(self): | 77 def _iterkeys(self): |
| 115 for key in self.keyOrder: | 78 for key in self.keyOrder: |
| 116 yield key | 79 yield key |
| 117 | 80 |
| 118 def _itervalues(self): | 81 def _itervalues(self): |
| 119 for key in self.keyOrder: | 82 for key in self.keyOrder: |
| 120 yield self[key] | 83 yield self[key] |
| 121 | 84 |
| 122 if util.PY3: | 85 if util.PY3: # pragma: no cover |
| 123 items = _iteritems | 86 items = _iteritems |
| 124 keys = _iterkeys | 87 keys = _iterkeys |
| 125 values = _itervalues | 88 values = _itervalues |
| 126 else: | 89 else: # pragma: no cover |
| 127 iteritems = _iteritems | 90 iteritems = _iteritems |
| 128 iterkeys = _iterkeys | 91 iterkeys = _iterkeys |
| 129 itervalues = _itervalues | 92 itervalues = _itervalues |
| 130 | 93 |
| 131 def items(self): | 94 def items(self): |
| 132 return [(k, self[k]) for k in self.keyOrder] | 95 return [(k, self[k]) for k in self.keyOrder] |
| 133 | 96 |
| 134 def keys(self): | 97 def keys(self): |
| 135 return self.keyOrder[:] | 98 return self.keyOrder[:] |
| 136 | 99 |
| 137 def values(self): | 100 def values(self): |
| 138 return [self[k] for k in self.keyOrder] | 101 return [self[k] for k in self.keyOrder] |
| 139 | 102 |
| 140 def update(self, dict_): | 103 def update(self, dict_): |
| 141 for k, v in iteritems_compat(dict_): | 104 for k in dict_: |
| 142 self[k] = v | 105 self[k] = dict_[k] |
| 143 | 106 |
| 144 def setdefault(self, key, default): | 107 def setdefault(self, key, default): |
| 145 if key not in self: | 108 if key not in self: |
| 146 self.keyOrder.append(key) | 109 self.keyOrder.append(key) |
| 147 return super(OrderedDict, self).setdefault(key, default) | 110 return super(OrderedDict, self).setdefault(key, default) |
| 148 | 111 |
| 149 def value_for_index(self, index): | 112 def value_for_index(self, index): |
| 150 """Returns the value of the item at the given zero-based index.""" | 113 """Returns the value of the item at the given zero-based index.""" |
| 151 return self[self.keyOrder[index]] | 114 return self[self.keyOrder[index]] |
| 152 | 115 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 163 def copy(self): | 126 def copy(self): |
| 164 """Returns a copy of this object.""" | 127 """Returns a copy of this object.""" |
| 165 # This way of initializing the copy means it works for subclasses, too. | 128 # This way of initializing the copy means it works for subclasses, too. |
| 166 return self.__class__(self) | 129 return self.__class__(self) |
| 167 | 130 |
| 168 def __repr__(self): | 131 def __repr__(self): |
| 169 """ | 132 """ |
| 170 Replaces the normal dict.__repr__ with a version that returns the keys | 133 Replaces the normal dict.__repr__ with a version that returns the keys |
| 171 in their Ordered order. | 134 in their Ordered order. |
| 172 """ | 135 """ |
| 173 return '{%s}' % ', '.join(['%r: %r' % (k, v) for k, v in iteritems_compa
t(self)]) | 136 return '{%s}' % ', '.join( |
| 137 ['%r: %r' % (k, v) for k, v in self._iteritems()] |
| 138 ) |
| 174 | 139 |
| 175 def clear(self): | 140 def clear(self): |
| 176 super(OrderedDict, self).clear() | 141 super(OrderedDict, self).clear() |
| 177 self.keyOrder = [] | 142 self.keyOrder = [] |
| 178 | 143 |
| 179 def index(self, key): | 144 def index(self, key): |
| 180 """ Return the index of a given key. """ | 145 """ Return the index of a given key. """ |
| 181 try: | 146 try: |
| 182 return self.keyOrder.index(key) | 147 return self.keyOrder.index(key) |
| 183 except ValueError: | 148 except ValueError: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 try: | 182 try: |
| 218 i = self.index_for_location(location) | 183 i = self.index_for_location(location) |
| 219 if i is not None: | 184 if i is not None: |
| 220 self.keyOrder.insert(i, key) | 185 self.keyOrder.insert(i, key) |
| 221 else: | 186 else: |
| 222 self.keyOrder.append(key) | 187 self.keyOrder.append(key) |
| 223 except Exception as e: | 188 except Exception as e: |
| 224 # restore to prevent data loss and reraise | 189 # restore to prevent data loss and reraise |
| 225 self.keyOrder.insert(n, key) | 190 self.keyOrder.insert(n, key) |
| 226 raise e | 191 raise e |
| OLD | NEW |