dompy-parser 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dompy/__init__.py +29 -0
- dompy/document.py +192 -0
- dompy/event.py +31 -0
- dompy/models.py +717 -0
- dompy/parser.py +176 -0
- dompy/selectors.py +100 -0
- dompy/serialization.py +79 -0
- dompy/uitils.py +7 -0
- dompy_parser-0.2.0.dist-info/METADATA +98 -0
- dompy_parser-0.2.0.dist-info/RECORD +13 -0
- dompy_parser-0.2.0.dist-info/WHEEL +5 -0
- dompy_parser-0.2.0.dist-info/licenses/LICENSE +674 -0
- dompy_parser-0.2.0.dist-info/top_level.txt +1 -0
dompy/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""HTML5 parsing with a Python DOM API."""
|
|
2
|
+
|
|
3
|
+
from .document import Document
|
|
4
|
+
from .models import (
|
|
5
|
+
Comment,
|
|
6
|
+
DocumentFragment,
|
|
7
|
+
DocumentType,
|
|
8
|
+
Element,
|
|
9
|
+
Node,
|
|
10
|
+
ProcessingInstruction,
|
|
11
|
+
Tags,
|
|
12
|
+
Text,
|
|
13
|
+
)
|
|
14
|
+
from .parser import DompyParser
|
|
15
|
+
from .selectors import SelectorError
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"Comment",
|
|
19
|
+
"Document",
|
|
20
|
+
"DocumentFragment",
|
|
21
|
+
"DocumentType",
|
|
22
|
+
"DompyParser",
|
|
23
|
+
"Element",
|
|
24
|
+
"Node",
|
|
25
|
+
"ProcessingInstruction",
|
|
26
|
+
"SelectorError",
|
|
27
|
+
"Tags",
|
|
28
|
+
"Text",
|
|
29
|
+
]
|
dompy/document.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Full HTML documents and input sources."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from os import PathLike
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from urllib.parse import urlsplit
|
|
9
|
+
from urllib.request import url2pathname, urlopen
|
|
10
|
+
|
|
11
|
+
from .models import (
|
|
12
|
+
HTML_NAMESPACE,
|
|
13
|
+
Comment,
|
|
14
|
+
DocumentFragment,
|
|
15
|
+
DocumentType,
|
|
16
|
+
Element,
|
|
17
|
+
ElementContainer,
|
|
18
|
+
Node,
|
|
19
|
+
Text,
|
|
20
|
+
)
|
|
21
|
+
from .parser import _convert, _parse
|
|
22
|
+
from .serialization import serialize
|
|
23
|
+
|
|
24
|
+
_TITLE_WHITESPACE = re.compile(r"[ \t\n\r\f]+")
|
|
25
|
+
_INVALID_TAG_NAME = re.compile(r"[ \t\n\r\f\x00<>/]")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Document(ElementContainer):
|
|
29
|
+
"""An inert HTML DOM built from a string, stream, file, or URL."""
|
|
30
|
+
|
|
31
|
+
nodeName = "#document"
|
|
32
|
+
nodeType = Node.DOCUMENT_NODE
|
|
33
|
+
|
|
34
|
+
def __init__(self, root: Element | None = None, *, scripting: bool = False) -> None:
|
|
35
|
+
super().__init__()
|
|
36
|
+
self._scripting = scripting
|
|
37
|
+
self._mode = "no-quirks"
|
|
38
|
+
self._template_document = None
|
|
39
|
+
if root is not None:
|
|
40
|
+
self.appendChild(root)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def documentElement(self) -> Element | None:
|
|
44
|
+
return self.firstElementChild
|
|
45
|
+
|
|
46
|
+
root = documentElement
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def compatMode(self) -> str:
|
|
50
|
+
return "BackCompat" if self._mode == "quirks" else "CSS1Compat"
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def doctype(self) -> DocumentType | None:
|
|
54
|
+
return next(
|
|
55
|
+
(child for child in self._children if isinstance(child, DocumentType)), None
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
def _html_child(self, *tags):
|
|
59
|
+
root = self.documentElement
|
|
60
|
+
if root is None or root.namespaceURI != HTML_NAMESPACE or root.tag != "html":
|
|
61
|
+
return None
|
|
62
|
+
return next(
|
|
63
|
+
(
|
|
64
|
+
node
|
|
65
|
+
for node in root.children
|
|
66
|
+
if node.namespaceURI == HTML_NAMESPACE and node.tag in tags
|
|
67
|
+
),
|
|
68
|
+
None,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def head(self) -> Element | None:
|
|
73
|
+
return self._html_child("head")
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def body(self) -> Element | None:
|
|
77
|
+
return self._html_child("body", "frameset")
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def title(self) -> str:
|
|
81
|
+
for element in self._elements():
|
|
82
|
+
if element.namespaceURI == HTML_NAMESPACE and element.tag == "title":
|
|
83
|
+
return _TITLE_WHITESPACE.sub(" ", element.textContent).strip(" ")
|
|
84
|
+
return ""
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def textContent(self) -> None:
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
@textContent.setter
|
|
91
|
+
def textContent(self, value: str | None) -> None:
|
|
92
|
+
pass
|
|
93
|
+
|
|
94
|
+
def getElementById(self, id: str) -> Element | None:
|
|
95
|
+
if not id:
|
|
96
|
+
return None
|
|
97
|
+
return next((node for node in self._elements() if node.id == id), None)
|
|
98
|
+
|
|
99
|
+
def createElement(self, tag: str) -> Element:
|
|
100
|
+
if not tag or _INVALID_TAG_NAME.search(tag):
|
|
101
|
+
raise ValueError("Invalid element name")
|
|
102
|
+
return self._create(Element(tag))
|
|
103
|
+
|
|
104
|
+
def createTextNode(self, data: str) -> Text:
|
|
105
|
+
return self._create(Text(data))
|
|
106
|
+
|
|
107
|
+
def createComment(self, data: str) -> Comment:
|
|
108
|
+
return self._create(Comment(data))
|
|
109
|
+
|
|
110
|
+
def createDocumentFragment(self) -> DocumentFragment:
|
|
111
|
+
return self._create(DocumentFragment())
|
|
112
|
+
|
|
113
|
+
def _create(self, node):
|
|
114
|
+
node._set_document(self)
|
|
115
|
+
return node
|
|
116
|
+
|
|
117
|
+
def _template_owner(self):
|
|
118
|
+
if self._template_document is None:
|
|
119
|
+
inert = Document()
|
|
120
|
+
inert._template_document = inert
|
|
121
|
+
self._template_document = inert
|
|
122
|
+
return self._template_document
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def _validate_children(children):
|
|
126
|
+
structure = [
|
|
127
|
+
node.nodeType
|
|
128
|
+
for node in children
|
|
129
|
+
if node.nodeType
|
|
130
|
+
not in (Node.COMMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE)
|
|
131
|
+
]
|
|
132
|
+
if structure not in (
|
|
133
|
+
[],
|
|
134
|
+
[Node.ELEMENT_NODE],
|
|
135
|
+
[Node.DOCUMENT_TYPE_NODE],
|
|
136
|
+
[Node.DOCUMENT_TYPE_NODE, Node.ELEMENT_NODE],
|
|
137
|
+
):
|
|
138
|
+
raise ValueError(
|
|
139
|
+
"A document may only contain one element and one preceding doctype"
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def _copy(self) -> Document:
|
|
143
|
+
clone = type(self)(scripting=self._scripting)
|
|
144
|
+
clone._mode = self._mode
|
|
145
|
+
return clone
|
|
146
|
+
|
|
147
|
+
def __str__(self) -> str:
|
|
148
|
+
return serialize(self)
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
def fromString(
|
|
152
|
+
cls, string, *, scripting: bool = False, encoding: str | None = None
|
|
153
|
+
) -> Document:
|
|
154
|
+
source, mode = _parse(string, scripting=scripting, encoding=encoding)
|
|
155
|
+
doc = cls(scripting=scripting)
|
|
156
|
+
doc._mode = mode
|
|
157
|
+
_convert(source, doc)
|
|
158
|
+
return doc
|
|
159
|
+
|
|
160
|
+
@classmethod
|
|
161
|
+
def fromFile(
|
|
162
|
+
cls, file, *, scripting: bool = False, encoding: str | None = None
|
|
163
|
+
) -> Document:
|
|
164
|
+
if isinstance(file, (str, PathLike)):
|
|
165
|
+
return cls.fromString(
|
|
166
|
+
Path(file).read_bytes(), scripting=scripting, encoding=encoding
|
|
167
|
+
)
|
|
168
|
+
return cls.fromString(file.read(), scripting=scripting, encoding=encoding)
|
|
169
|
+
|
|
170
|
+
@classmethod
|
|
171
|
+
def fromURL(
|
|
172
|
+
cls, url: str, *, timeout: float = 30, scripting: bool = False
|
|
173
|
+
) -> Document:
|
|
174
|
+
if urlsplit(url).scheme.lower() not in ("http", "https"):
|
|
175
|
+
raise ValueError("fromURL expects an HTTP or HTTPS URL")
|
|
176
|
+
with urlopen(url, timeout=timeout) as response:
|
|
177
|
+
return cls.fromString(
|
|
178
|
+
response.read(),
|
|
179
|
+
scripting=scripting,
|
|
180
|
+
encoding=response.headers.get_content_charset(),
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
@classmethod
|
|
184
|
+
def fromURI(
|
|
185
|
+
cls, uri: str, *, timeout: float = 30, scripting: bool = False
|
|
186
|
+
) -> Document:
|
|
187
|
+
location = urlsplit(uri)
|
|
188
|
+
if location.scheme in ("http", "https"):
|
|
189
|
+
return cls.fromURL(uri, timeout=timeout, scripting=scripting)
|
|
190
|
+
if location.scheme == "file" and location.netloc.lower() in ("", "localhost"):
|
|
191
|
+
return cls.fromFile(url2pathname(location.path), scripting=scripting)
|
|
192
|
+
raise ValueError("fromURI expects an HTTP(S) URL or a local file URI")
|
dompy/event.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
class Event:
|
|
3
|
+
""" Initialization of default Event Object. """
|
|
4
|
+
callbacks: dict = {}
|
|
5
|
+
|
|
6
|
+
# Method For Register An Event And Its Appropriate Callbacks
|
|
7
|
+
@staticmethod
|
|
8
|
+
def on(event: str, callback):
|
|
9
|
+
if Event.callbacks.get(event) is not None:
|
|
10
|
+
Event.callbacks.get(event).add(callback)
|
|
11
|
+
else:
|
|
12
|
+
Event.callbacks.update({
|
|
13
|
+
event: {callback}
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
# Method for unregistered or stop listening for event
|
|
17
|
+
@staticmethod
|
|
18
|
+
def off(event: str):
|
|
19
|
+
if Event.callbacks.get(event) is not None:
|
|
20
|
+
Event.callbacks.pop(event)
|
|
21
|
+
|
|
22
|
+
# Method For Executing And Passing Data To The Callbacked That Is Associated To An Certain Event
|
|
23
|
+
@staticmethod
|
|
24
|
+
def emmit(event: str, data=None):
|
|
25
|
+
callbacks = Event.callbacks.get(event)
|
|
26
|
+
if callbacks is not None:
|
|
27
|
+
for callback in callbacks:
|
|
28
|
+
if data is not None:
|
|
29
|
+
callback(data)
|
|
30
|
+
else:
|
|
31
|
+
callback()
|