1 from ...types import serializable
2 from ...util import none_or
3 from ..errors import MalformedXML
4 from .redirect import Redirect
5 from .revision import Revision
8 class Page(serializable.Type):
10 Page meta data and a :class:`~mw.xml_dump.Revision` iterator. Instances of
11 this class can be called as iterators directly. E.g.
13 .. code-block:: python
15 page = mw.xml_dump.Page( ... )
18 print("{0} {1}".format(revision.id, page_id))
29 def __init__(self, id, title, namespace, redirect, restrictions, revisions=None):
30 self.id = none_or(id, int)
35 self.title = none_or(title, str)
37 Page title (namespace excluded) : `str`
40 self.namespace = none_or(namespace, int)
45 self.redirect = none_or(redirect, Redirect)
47 Page is currently redirect? : :class:`~mw.xml_dump.Redirect` | `None`
50 self.restrictions = serializable.List.deserialize(restrictions)
52 A list of page editing restrictions (empty unless restrictions are specified) : list( `str` )
55 # Should be a lazy generator
56 self.__revisions = revisions or []
59 return self.__revisions
62 return next(self.__revisions)
65 def load_revisions(cls, first_revision, element):
66 yield Revision.from_element(first_revision)
68 for sub_element in element:
72 yield Revision.from_element(sub_element)
74 raise MalformedXML("Expected to see 'revision'. " +
75 "Instead saw '{0}'".format(tag))
78 def from_element(cls, element):
87 # Consume each of the elements until we see <id> which should come last.
88 for sub_element in element:
91 title = sub_element.text
93 namespace = sub_element.text
95 id = int(sub_element.text)
96 elif tag == "redirect":
97 redirect = Redirect.from_element(sub_element)
98 elif tag == "restrictions":
99 restrictions.append(sub_element.text)
100 elif tag == "DiscussionThreading":
104 elif tag == "revision":
105 first_revision = sub_element
107 # Assuming that the first revision seen marks the end of page
108 # metadata. I'm not too keen on this assumption, so I'm leaving
109 # this long comment to warn whoever ends up maintaining this.
111 raise MalformedXML("Unexpected tag found when processing " +
112 "a <page>: '{0}'".format(tag))
114 # Assuming that I got here by seeing a <revision> tag. See verbose
116 revisions = cls.load_revisions(first_revision, element)
118 return cls(id, title, namespace, redirect, restrictions, revisions)