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)
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
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.
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.
33 from __future__
import unicode_literals
34 from __future__
import absolute_import
37 from copy
import deepcopy
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
)())
44 class OrderedDict(dict):
46 A dictionary that keeps its keys in the order in which they're inserted.
48 Copied from Django's SortedDict with some modifications.
51 def __new__(cls
, *args
, **kwargs
):
52 instance
= super(OrderedDict
, cls
).__new
__(cls
, *args
, **kwargs
)
53 instance
.keyOrder
= []
56 def __init__(self
, data
=None):
57 if data
is None or isinstance(data
, dict):
59 super(OrderedDict
, self
).__init
__(data
)
60 self
.keyOrder
= list(data
) if data
else []
62 super(OrderedDict
, self
).__init
__()
63 super_set
= super(OrderedDict
, self
).__setitem
__
64 for key
, value
in data
:
65 # Take the ordering from first key
67 self
.keyOrder
.append(key
)
68 # But override with last value in data (dict() does this)
71 def __deepcopy__(self
, memo
):
72 return self
.__class
__([(key
, deepcopy(value
, memo
))
73 for key
, value
in self
.items()])
76 # The Python's default copy implementation will alter the state
77 # of self. The reason for this seems complex but is likely related to
81 def __setitem__(self
, key
, value
):
83 self
.keyOrder
.append(key
)
84 super(OrderedDict
, self
).__setitem
__(key
, value
)
86 def __delitem__(self
, key
):
87 super(OrderedDict
, self
).__delitem
__(key
)
88 self
.keyOrder
.remove(key
)
91 return iter(self
.keyOrder
)
93 def __reversed__(self
):
94 return reversed(self
.keyOrder
)
96 def pop(self
, k
, *args
):
97 result
= super(OrderedDict
, self
).pop(k
, *args
)
99 self
.keyOrder
.remove(k
)
101 # Key wasn't in the dictionary in the first place. No problem.
106 result
= super(OrderedDict
, self
).popitem()
107 self
.keyOrder
.remove(result
[0])
110 def _iteritems(self
):
111 for key
in self
.keyOrder
:
115 for key
in self
.keyOrder
:
118 def _itervalues(self
):
119 for key
in self
.keyOrder
:
127 iteritems
= _iteritems
129 itervalues
= _itervalues
132 return [(k
, self
[k
]) for k
in self
.keyOrder
]
135 return self
.keyOrder
[:]
138 return [self
[k
] for k
in self
.keyOrder
]
140 def update(self
, dict_
):
141 for k
, v
in iteritems_compat(dict_
):
144 def setdefault(self
, key
, default
):
146 self
.keyOrder
.append(key
)
147 return super(OrderedDict
, self
).setdefault(key
, default
)
149 def value_for_index(self
, index
):
150 """Returns the value of the item at the given zero-based index."""
151 return self
[self
.keyOrder
[index
]]
153 def insert(self
, index
, key
, value
):
154 """Inserts the key, value pair before the item with the given index."""
155 if key
in self
.keyOrder
:
156 n
= self
.keyOrder
.index(key
)
160 self
.keyOrder
.insert(index
, key
)
161 super(OrderedDict
, self
).__setitem
__(key
, value
)
164 """Returns a copy of this object."""
165 # This way of initializing the copy means it works for subclasses, too.
166 return self
.__class
__(self
)
170 Replaces the normal dict.__repr__ with a version that returns the keys
171 in their Ordered order.
173 return '{%s}' % ', '.join(['%r: %r' % (k
, v
) for k
, v
in iteritems_compat(self
)])
176 super(OrderedDict
, self
).clear()
179 def index(self
, key
):
180 """ Return the index of a given key. """
182 return self
.keyOrder
.index(key
)
184 raise ValueError("Element '%s' was not found in OrderedDict" % key
)
186 def index_for_location(self
, location
):
187 """ Return index or None for a given location. """
188 if location
== '_begin':
190 elif location
== '_end':
192 elif location
.startswith('<') or location
.startswith('>'):
193 i
= self
.index(location
[1:])
194 if location
.startswith('>'):
201 raise ValueError('Not a valid location: "%s". Location key '
202 'must start with a ">" or "<".' % location
)
205 def add(self
, key
, value
, location
):
206 """ Insert by key location. """
207 i
= self
.index_for_location(location
)
209 self
.insert(i
, key
, value
)
211 self
.__setitem
__(key
, value
)
213 def link(self
, key
, location
):
214 """ Change location of an existing item. """
215 n
= self
.keyOrder
.index(key
)
218 i
= self
.index_for_location(location
)
220 self
.keyOrder
.insert(i
, key
)
222 self
.keyOrder
.append(key
)
223 except Exception as e
:
224 # restore to prevent data loss and reraise
225 self
.keyOrder
.insert(n
, key
)