3 Represents a chunk of text and the revisions of a page that it survived.
5 __slots__ = ('text', 'revisions')
7 def __init__(self, text, revisions=None):
10 The text of the token.
13 self.revisions = revisions if revisions is not None else []
15 The meta data for the revisions that the token has appeared within.
18 def persist(self, revision):
19 self.revisions.append(revision)
22 return "{0}({1})".format(
23 self.__class__.__name__,
25 "text={0}".format(repr(self.text)),
26 "revisions={0}".format(repr(self.revisions))
33 Represents a :class:`list` of :class:`~mw.lib.persistence.Token` with some
34 useful helper functions.
38 >>> from mw.lib.persistence import Token, Tokens
41 >>> tokens.append(Token("foo"))
42 >>> tokens.extend([Token(" "), Token("bar")])
45 Token(text='foo', revisions=[])
47 >>> "".join(tokens.texts())
51 def __init__(self, *args, **kwargs):
52 super().__init__(*args, **kwargs)
54 def persist(self, revision):
56 token.persist(revision)
62 def compare(self, new, diff):
65 return self.apply_diff(diff(old, new), self, new)
68 def apply_diff(cls, ops, old, new):
72 tokens_removed = cls()
74 for code, a_start, a_end, b_start, b_end in ops:
76 for token_text in new[b_start:b_end]:
77 token = Token(token_text)
79 tokens_added.append(token)
81 elif code == "replace":
82 for token_text in new[b_start:b_end]:
83 token = Token(token_text)
85 tokens_added.append(token)
87 tokens_removed.extend(t for t in old[a_start:a_end])
90 tokens.extend(old[a_start:a_end])
91 elif code == "delete":
92 tokens_removed.extend(old[a_start:a_end])
96 "encounted an unrecognized operation code: " + repr(code)
98 return (tokens, tokens_added, tokens_removed)