xml-encoding-detector 0.1.0__tar.gz → 0.1.1__tar.gz
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.
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/PKG-INFO +1 -1
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/pyproject.toml +5 -2
- xml_encoding_detector-0.1.1/src/xml_encoding_detector/__init__.py +15 -0
- xml_encoding_detector-0.1.1/src/xml_encoding_detector/cli.py +39 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector/detector.py +57 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector.egg-info/PKG-INFO +1 -1
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector.egg-info/SOURCES.txt +3 -0
- xml_encoding_detector-0.1.1/src/xml_encoding_detector.egg-info/entry_points.txt +2 -0
- xml_encoding_detector-0.1.1/tests/test_cli.py +8 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/tests/test_detector.py +63 -0
- xml_encoding_detector-0.1.0/src/xml_encoding_detector/__init__.py +0 -9
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/LICENSE +0 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/README.md +0 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/setup.cfg +0 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector.egg-info/dependency_links.txt +0 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector.egg-info/requires.txt +0 -0
- {xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "xml-encoding-detector"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.1"
|
|
8
8
|
description = "Lightweight XML encoding detector for Python."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -90,4 +90,7 @@ select = [
|
|
|
90
90
|
"UP"
|
|
91
91
|
]
|
|
92
92
|
|
|
93
|
-
ignore = []
|
|
93
|
+
ignore = []
|
|
94
|
+
[project.scripts]
|
|
95
|
+
|
|
96
|
+
xml-encoding-detector = "xml_encoding_detector.cli:main"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .detector import (
|
|
2
|
+
get_xml_encoding,
|
|
3
|
+
DetectionResult,
|
|
4
|
+
ValidationResult,
|
|
5
|
+
detect_encoding,
|
|
6
|
+
is_xml_well_formed,
|
|
7
|
+
validate_xml,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"get_xml_encoding",
|
|
12
|
+
"DetectionResult",
|
|
13
|
+
"detect_encoding",
|
|
14
|
+
"is_xml_well_formed",
|
|
15
|
+
]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from xml_encoding_detector import (
|
|
5
|
+
validate_xml,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main() -> int:
|
|
10
|
+
|
|
11
|
+
if len(sys.argv) < 2 or sys.argv[1] in (
|
|
12
|
+
"-h",
|
|
13
|
+
"--help",
|
|
14
|
+
):
|
|
15
|
+
print("Usage: xml-encoding-detector <file>")
|
|
16
|
+
return 1
|
|
17
|
+
|
|
18
|
+
path = sys.argv[1]
|
|
19
|
+
|
|
20
|
+
if not Path(path).exists():
|
|
21
|
+
print(f"File not found: {path}")
|
|
22
|
+
|
|
23
|
+
return 1
|
|
24
|
+
|
|
25
|
+
with open(
|
|
26
|
+
path,
|
|
27
|
+
"rb",
|
|
28
|
+
) as file:
|
|
29
|
+
data = file.read()
|
|
30
|
+
|
|
31
|
+
result = validate_xml(data)
|
|
32
|
+
|
|
33
|
+
print(f"XML valid : {result.valid}")
|
|
34
|
+
|
|
35
|
+
print(f"Encoding : {result.encoding}")
|
|
36
|
+
|
|
37
|
+
print(f"Error : {result.error}")
|
|
38
|
+
|
|
39
|
+
return 0
|
{xml_encoding_detector-0.1.0 → xml_encoding_detector-0.1.1}/src/xml_encoding_detector/detector.py
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
+
from xml.etree import ElementTree
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
@dataclass(slots=True)
|
|
@@ -8,6 +9,13 @@ class DetectionResult:
|
|
|
8
9
|
source: str
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
@dataclass(slots=True)
|
|
13
|
+
class ValidationResult:
|
|
14
|
+
valid: bool
|
|
15
|
+
error: str | None
|
|
16
|
+
encoding: str | None
|
|
17
|
+
|
|
18
|
+
|
|
11
19
|
UTF8_BOM = b"\xef\xbb\xbf"
|
|
12
20
|
|
|
13
21
|
UTF16_LE_BOM = b"\xff\xfe"
|
|
@@ -143,3 +151,52 @@ def detect_encoding(
|
|
|
143
151
|
confidence=0.5,
|
|
144
152
|
source="default",
|
|
145
153
|
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def is_xml_well_formed(
|
|
157
|
+
data: bytes,
|
|
158
|
+
) -> bool:
|
|
159
|
+
|
|
160
|
+
result = detect_encoding(data)
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
text = data.decode(result.encoding)
|
|
164
|
+
|
|
165
|
+
ElementTree.fromstring(text)
|
|
166
|
+
|
|
167
|
+
return True
|
|
168
|
+
|
|
169
|
+
except Exception:
|
|
170
|
+
return False
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def validate_xml(
|
|
174
|
+
data: bytes,
|
|
175
|
+
) -> ValidationResult:
|
|
176
|
+
|
|
177
|
+
result = detect_encoding(data)
|
|
178
|
+
|
|
179
|
+
try:
|
|
180
|
+
text = data.decode(result.encoding)
|
|
181
|
+
|
|
182
|
+
ElementTree.fromstring(text)
|
|
183
|
+
|
|
184
|
+
return ValidationResult(
|
|
185
|
+
valid=True,
|
|
186
|
+
error=None,
|
|
187
|
+
encoding=result.encoding,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
except Exception as exc:
|
|
191
|
+
return ValidationResult(
|
|
192
|
+
valid=False,
|
|
193
|
+
error=str(exc),
|
|
194
|
+
encoding=result.encoding,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def get_xml_encoding(
|
|
199
|
+
data: bytes,
|
|
200
|
+
) -> str:
|
|
201
|
+
|
|
202
|
+
return detect_encoding(data).encoding
|
|
@@ -2,10 +2,13 @@ LICENSE
|
|
|
2
2
|
README.md
|
|
3
3
|
pyproject.toml
|
|
4
4
|
src/xml_encoding_detector/__init__.py
|
|
5
|
+
src/xml_encoding_detector/cli.py
|
|
5
6
|
src/xml_encoding_detector/detector.py
|
|
6
7
|
src/xml_encoding_detector.egg-info/PKG-INFO
|
|
7
8
|
src/xml_encoding_detector.egg-info/SOURCES.txt
|
|
8
9
|
src/xml_encoding_detector.egg-info/dependency_links.txt
|
|
10
|
+
src/xml_encoding_detector.egg-info/entry_points.txt
|
|
9
11
|
src/xml_encoding_detector.egg-info/requires.txt
|
|
10
12
|
src/xml_encoding_detector.egg-info/top_level.txt
|
|
13
|
+
tests/test_cli.py
|
|
11
14
|
tests/test_detector.py
|
|
@@ -169,3 +169,66 @@ def test_normalize_big5():
|
|
|
169
169
|
)
|
|
170
170
|
|
|
171
171
|
assert normalize_encoding("BIG5") == "big5"
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_well_formed_xml():
|
|
175
|
+
|
|
176
|
+
from xml_encoding_detector import (
|
|
177
|
+
is_xml_well_formed,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
data = b"<root></root>"
|
|
181
|
+
|
|
182
|
+
assert is_xml_well_formed(data) is True
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def test_invalid_xml():
|
|
186
|
+
|
|
187
|
+
from xml_encoding_detector import (
|
|
188
|
+
is_xml_well_formed,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
data = b"<root>"
|
|
192
|
+
|
|
193
|
+
assert is_xml_well_formed(data) is False
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def test_validate_xml_success():
|
|
197
|
+
|
|
198
|
+
from xml_encoding_detector import (
|
|
199
|
+
validate_xml,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
result = validate_xml(b"<root></root>")
|
|
203
|
+
|
|
204
|
+
assert result.valid is True
|
|
205
|
+
|
|
206
|
+
assert result.error is None
|
|
207
|
+
|
|
208
|
+
assert result.encoding == "utf-8"
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def test_validate_xml_failure():
|
|
212
|
+
|
|
213
|
+
from xml_encoding_detector import (
|
|
214
|
+
validate_xml,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
result = validate_xml(b"<root>")
|
|
218
|
+
|
|
219
|
+
assert result.valid is False
|
|
220
|
+
|
|
221
|
+
assert result.error is not None
|
|
222
|
+
|
|
223
|
+
assert result.encoding == "utf-8"
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def test_get_xml_encoding():
|
|
227
|
+
|
|
228
|
+
from xml_encoding_detector import (
|
|
229
|
+
get_xml_encoding,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
result = get_xml_encoding(b"<root></root>")
|
|
233
|
+
|
|
234
|
+
assert result == "utf-8"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|