]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/xml_dump/iteration/comment.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / xml_dump / iteration / comment.py
1 from ...types import serializable
2
3
4 class Comment(str, serializable.Type):
5     """
6     A revision comment.  This class behaves identically to
7     :class:`str` except that it takes and stores an additional parameter
8     recording whether the comment was deleted or not.
9
10     >>> from mw.xml_dump import Comment
11     >>>
12     >>> c = Comment("foo")
13     >>> c == "foo"
14     True
15     >>> c.deleted
16     False
17
18     **deleted**
19         Was the comment deleted? | `bool`
20
21     """
22
23     def __new__(cls, string_or_comment="", deleted=False):
24         if isinstance(string_or_comment, cls):
25             return string_or_comment
26         else:
27             inst = super().__new__(cls, string_or_comment)
28             inst.initialize(string_or_comment, deleted)
29             return inst
30
31     def initialize(self, string, deleted):
32         self.deleted = bool(deleted)
33
34     def __str__(self):
35         return str.__str__(self)
36
37     def __repr__(self):
38         return "{0}({1})".format(
39             self.__class__.__name__,
40             ", ".join([
41                 str.__repr__(self),
42                 "deleted={0}".format(repr(self.deleted))
43             ])
44         )
45
46     def serialize(self):
47         return {
48             "string_or_comment": str(self),
49             "deleted": self.deleted
50         }
51
52     @classmethod
53     def from_element(cls, e):
54         return cls(e.text, e.attr('deleted', False))

Community Data Science Collective || Want to submit a patch?