xml-encoding-detector 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 namnv0410
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.4
2
+ Name: xml-encoding-detector
3
+ Version: 0.1.0
4
+ Summary: Lightweight XML encoding detector for Python.
5
+ Author: namnv0410
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/namnv0410-ship-it/xml-encoding-detector
8
+ Project-URL: Repository, https://github.com/namnv0410-ship-it/xml-encoding-detector
9
+ Project-URL: Issues, https://github.com/namnv0410-ship-it/xml-encoding-detector/issues
10
+ Keywords: xml,encoding,unicode,utf8,utf16
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Requires-Python: >=3.11
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: lxml>=5.0
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest>=8.0; extra == "test"
22
+ Requires-Dist: pytest-cov>=5.0; extra == "test"
23
+ Provides-Extra: dev
24
+ Requires-Dist: ruff>=0.7; extra == "dev"
25
+ Dynamic: license-file
26
+
27
+ xml-encoding-detector
28
+
29
+ Lightweight XML encoding detector for Python.
30
+
31
+
32
+
33
+ Features
34
+
35
+ UTF-8 BOM detection
36
+
37
+ UTF-16 LE/BE BOM detection
38
+
39
+ UTF-32 LE/BE BOM detection
40
+
41
+ XML declaration parsing
42
+
43
+ Encoding alias normalization
44
+
45
+ Installation
46
+
47
+ pip install xml-encoding-detector
48
+
49
+ Usage
50
+
51
+ from xml\_encoding\_detector import (
52
+
53
+   detect\_encoding,
54
+
55
+ )
56
+
57
+
58
+
59
+ xml = (
60
+
61
+ &#x20; b'<?xml version="1.0" '
62
+
63
+ &#x20; b'encoding="Shift\_JIS"?>'
64
+
65
+ )
66
+
67
+
68
+
69
+ result = detect\_encoding(xml)
70
+
71
+
72
+
73
+ print(result.encoding)
74
+
75
+ print(result.confidence)
76
+
77
+ print(result.source)
78
+
79
+ Example Output
80
+
81
+ shift\_jis
82
+
83
+ 0.9
84
+
85
+ xml-declaration
86
+
@@ -0,0 +1,60 @@
1
+ xml-encoding-detector
2
+
3
+ Lightweight XML encoding detector for Python.
4
+
5
+
6
+
7
+ Features
8
+
9
+ UTF-8 BOM detection
10
+
11
+ UTF-16 LE/BE BOM detection
12
+
13
+ UTF-32 LE/BE BOM detection
14
+
15
+ XML declaration parsing
16
+
17
+ Encoding alias normalization
18
+
19
+ Installation
20
+
21
+ pip install xml-encoding-detector
22
+
23
+ Usage
24
+
25
+ from xml\_encoding\_detector import (
26
+
27
+ &#x20; detect\_encoding,
28
+
29
+ )
30
+
31
+
32
+
33
+ xml = (
34
+
35
+ &#x20; b'<?xml version="1.0" '
36
+
37
+ &#x20; b'encoding="Shift\_JIS"?>'
38
+
39
+ )
40
+
41
+
42
+
43
+ result = detect\_encoding(xml)
44
+
45
+
46
+
47
+ print(result.encoding)
48
+
49
+ print(result.confidence)
50
+
51
+ print(result.source)
52
+
53
+ Example Output
54
+
55
+ shift\_jis
56
+
57
+ 0.9
58
+
59
+ xml-declaration
60
+
@@ -0,0 +1,93 @@
1
+ [build-system]
2
+ requires = ["setuptools>=69"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "xml-encoding-detector"
7
+ version = "0.1.0"
8
+ description = "Lightweight XML encoding detector for Python."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ keywords = [
12
+ "xml",
13
+ "encoding",
14
+ "unicode",
15
+ "utf8",
16
+ "utf16",
17
+ ]
18
+
19
+ classifiers = [
20
+ "Development Status :: 3 - Alpha",
21
+ "License :: OSI Approved :: MIT License",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ ]
26
+ license = { text = "MIT" }
27
+
28
+ authors = [
29
+ { name = "namnv0410" }
30
+ ]
31
+
32
+ dependencies = [
33
+ "lxml>=5.0"
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+
38
+ test = [
39
+ "pytest>=8.0",
40
+ "pytest-cov>=5.0"
41
+ ]
42
+
43
+ dev = [
44
+ "ruff>=0.7"
45
+ ]
46
+
47
+ [project.urls]
48
+
49
+ Homepage = "https://github.com/namnv0410-ship-it/xml-encoding-detector"
50
+ Repository = "https://github.com/namnv0410-ship-it/xml-encoding-detector"
51
+ Issues = "https://github.com/namnv0410-ship-it/xml-encoding-detector/issues"
52
+
53
+ [tool.setuptools]
54
+ package-dir = { "" = "src" }
55
+
56
+ [tool.setuptools.packages.find]
57
+ where = ["src"]
58
+
59
+ [tool.pytest.ini_options]
60
+
61
+ testpaths = [
62
+ "tests"
63
+ ]
64
+
65
+ addopts = [
66
+ "-ra",
67
+ "-v",
68
+ "--cov=xml_encoding_detector",
69
+ "--cov-report=term-missing"
70
+ ]
71
+
72
+ [tool.ruff]
73
+
74
+ line-length = 88
75
+
76
+ target-version = "py312"
77
+
78
+ src = [
79
+ "src",
80
+ "tests"
81
+ ]
82
+
83
+ [tool.ruff.lint]
84
+
85
+ select = [
86
+ "E",
87
+ "F",
88
+ "I",
89
+ "B",
90
+ "UP"
91
+ ]
92
+
93
+ ignore = []
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,9 @@
1
+ from .detector import (
2
+ DetectionResult,
3
+ detect_encoding,
4
+ )
5
+
6
+ __all__ = [
7
+ "DetectionResult",
8
+ "detect_encoding",
9
+ ]
@@ -0,0 +1,145 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass(slots=True)
5
+ class DetectionResult:
6
+ encoding: str
7
+ confidence: float
8
+ source: str
9
+
10
+
11
+ UTF8_BOM = b"\xef\xbb\xbf"
12
+
13
+ UTF16_LE_BOM = b"\xff\xfe"
14
+ UTF16_BE_BOM = b"\xfe\xff"
15
+
16
+ UTF32_LE_BOM = b"\xff\xfe\x00\x00"
17
+ UTF32_BE_BOM = b"\x00\x00\xfe\xff"
18
+
19
+ ENCODING_ALIASES = {
20
+ "utf8": "utf-8",
21
+ "utf16": "utf-16",
22
+ "utf16le": "utf-16-le",
23
+ "utf16be": "utf-16-be",
24
+ "utf32": "utf-32",
25
+ "utf32le": "utf-32-le",
26
+ "utf32be": "utf-32-be",
27
+ "shift-jis": "shift_jis",
28
+ "windows-1258": "cp1258",
29
+ "gb2312": "gb2312",
30
+ "gbk": "gbk",
31
+ "gb18030": "gb18030",
32
+ "big5": "big5",
33
+ }
34
+
35
+
36
+ def normalize_encoding(
37
+ encoding: str,
38
+ ) -> str:
39
+
40
+ value = encoding.strip().lower()
41
+
42
+ return ENCODING_ALIASES.get(
43
+ value,
44
+ value,
45
+ )
46
+
47
+
48
+ def detect_xml_declaration_encoding(
49
+ data: bytes,
50
+ ) -> str | None:
51
+
52
+ head = data[:512]
53
+
54
+ try:
55
+ text = head.decode(
56
+ "ascii",
57
+ errors="ignore",
58
+ )
59
+ except Exception:
60
+ return None
61
+
62
+ marker = "encoding="
63
+
64
+ index = text.lower().find(marker)
65
+
66
+ if index == -1:
67
+ return None
68
+
69
+ value = text[index + len(marker) :].lstrip()
70
+
71
+ if not value:
72
+ return None
73
+
74
+ quote = value[0]
75
+
76
+ if quote not in (
77
+ '"',
78
+ "'",
79
+ ):
80
+ return None
81
+
82
+ end = value.find(
83
+ quote,
84
+ 1,
85
+ )
86
+
87
+ if end == -1:
88
+ return None
89
+
90
+ return normalize_encoding(value[1:end])
91
+
92
+
93
+ def detect_encoding(
94
+ data: bytes,
95
+ ) -> DetectionResult:
96
+
97
+ if data.startswith(UTF8_BOM):
98
+ return DetectionResult(
99
+ encoding="utf-8",
100
+ confidence=1.0,
101
+ source="bom",
102
+ )
103
+
104
+ if data.startswith(UTF32_LE_BOM):
105
+ return DetectionResult(
106
+ encoding="utf-32-le",
107
+ confidence=1.0,
108
+ source="bom",
109
+ )
110
+
111
+ if data.startswith(UTF32_BE_BOM):
112
+ return DetectionResult(
113
+ encoding="utf-32-be",
114
+ confidence=1.0,
115
+ source="bom",
116
+ )
117
+
118
+ if data.startswith(UTF16_LE_BOM):
119
+ return DetectionResult(
120
+ encoding="utf-16-le",
121
+ confidence=1.0,
122
+ source="bom",
123
+ )
124
+
125
+ if data.startswith(UTF16_BE_BOM):
126
+ return DetectionResult(
127
+ encoding="utf-16-be",
128
+ confidence=1.0,
129
+ source="bom",
130
+ )
131
+
132
+ declaration_encoding = detect_xml_declaration_encoding(data)
133
+
134
+ if declaration_encoding:
135
+ return DetectionResult(
136
+ encoding=declaration_encoding,
137
+ confidence=0.9,
138
+ source="xml-declaration",
139
+ )
140
+
141
+ return DetectionResult(
142
+ encoding="utf-8",
143
+ confidence=0.5,
144
+ source="default",
145
+ )
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.4
2
+ Name: xml-encoding-detector
3
+ Version: 0.1.0
4
+ Summary: Lightweight XML encoding detector for Python.
5
+ Author: namnv0410
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/namnv0410-ship-it/xml-encoding-detector
8
+ Project-URL: Repository, https://github.com/namnv0410-ship-it/xml-encoding-detector
9
+ Project-URL: Issues, https://github.com/namnv0410-ship-it/xml-encoding-detector/issues
10
+ Keywords: xml,encoding,unicode,utf8,utf16
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Requires-Python: >=3.11
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: lxml>=5.0
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest>=8.0; extra == "test"
22
+ Requires-Dist: pytest-cov>=5.0; extra == "test"
23
+ Provides-Extra: dev
24
+ Requires-Dist: ruff>=0.7; extra == "dev"
25
+ Dynamic: license-file
26
+
27
+ xml-encoding-detector
28
+
29
+ Lightweight XML encoding detector for Python.
30
+
31
+
32
+
33
+ Features
34
+
35
+ UTF-8 BOM detection
36
+
37
+ UTF-16 LE/BE BOM detection
38
+
39
+ UTF-32 LE/BE BOM detection
40
+
41
+ XML declaration parsing
42
+
43
+ Encoding alias normalization
44
+
45
+ Installation
46
+
47
+ pip install xml-encoding-detector
48
+
49
+ Usage
50
+
51
+ from xml\_encoding\_detector import (
52
+
53
+ &#x20; detect\_encoding,
54
+
55
+ )
56
+
57
+
58
+
59
+ xml = (
60
+
61
+ &#x20; b'<?xml version="1.0" '
62
+
63
+ &#x20; b'encoding="Shift\_JIS"?>'
64
+
65
+ )
66
+
67
+
68
+
69
+ result = detect\_encoding(xml)
70
+
71
+
72
+
73
+ print(result.encoding)
74
+
75
+ print(result.confidence)
76
+
77
+ print(result.source)
78
+
79
+ Example Output
80
+
81
+ shift\_jis
82
+
83
+ 0.9
84
+
85
+ xml-declaration
86
+
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/xml_encoding_detector/__init__.py
5
+ src/xml_encoding_detector/detector.py
6
+ src/xml_encoding_detector.egg-info/PKG-INFO
7
+ src/xml_encoding_detector.egg-info/SOURCES.txt
8
+ src/xml_encoding_detector.egg-info/dependency_links.txt
9
+ src/xml_encoding_detector.egg-info/requires.txt
10
+ src/xml_encoding_detector.egg-info/top_level.txt
11
+ tests/test_detector.py
@@ -0,0 +1,8 @@
1
+ lxml>=5.0
2
+
3
+ [dev]
4
+ ruff>=0.7
5
+
6
+ [test]
7
+ pytest>=8.0
8
+ pytest-cov>=5.0
@@ -0,0 +1,171 @@
1
+ from xml_encoding_detector import (
2
+ detect_encoding,
3
+ )
4
+
5
+
6
+ def test_default_encoding():
7
+
8
+ result = detect_encoding(b"")
9
+
10
+ assert result.encoding == "utf-8"
11
+ assert result.source == "default"
12
+ assert result.confidence == 0.5
13
+
14
+
15
+ def test_utf8_bom():
16
+
17
+ data = b"\xef\xbb\xbf<?xml version='1.0'?>"
18
+
19
+ result = detect_encoding(data)
20
+
21
+ assert result.encoding == "utf-8"
22
+ assert result.source == "bom"
23
+ assert result.confidence == 1.0
24
+
25
+
26
+ def test_utf16_le_bom():
27
+
28
+ data = b"\xff\xfe<\x00?\x00"
29
+
30
+ result = detect_encoding(data)
31
+
32
+ assert result.encoding == "utf-16-le"
33
+
34
+ assert result.source == "bom"
35
+
36
+ assert result.confidence == 1.0
37
+
38
+
39
+ def test_utf16_be_bom():
40
+
41
+ data = b"\xfe\xff\x00<\x00?"
42
+
43
+ result = detect_encoding(data)
44
+
45
+ assert result.encoding == "utf-16-be"
46
+
47
+ assert result.source == "bom"
48
+
49
+ assert result.confidence == 1.0
50
+
51
+
52
+ def test_utf32_le_bom():
53
+
54
+ data = b"\xff\xfe\x00\x00<\x00\x00\x00"
55
+
56
+ result = detect_encoding(data)
57
+
58
+ assert result.encoding == "utf-32-le"
59
+
60
+ assert result.source == "bom"
61
+
62
+
63
+ def test_utf32_be_bom():
64
+
65
+ data = b"\x00\x00\xfe\xff\x00\x00\x00<"
66
+
67
+ result = detect_encoding(data)
68
+
69
+ assert result.encoding == "utf-32-be"
70
+
71
+ assert result.source == "bom"
72
+
73
+
74
+ def test_xml_declaration_utf8():
75
+
76
+ data = b'<?xml version="1.0" encoding="UTF-8"?>'
77
+
78
+ result = detect_encoding(data)
79
+
80
+ assert result.encoding == "utf-8"
81
+
82
+ assert result.source == "xml-declaration"
83
+
84
+
85
+ def test_xml_declaration_shift_jis():
86
+
87
+ data = b'<?xml version="1.0" encoding="Shift_JIS"?>'
88
+
89
+ result = detect_encoding(data)
90
+
91
+ assert result.encoding == "shift_jis"
92
+
93
+ assert result.source == "xml-declaration"
94
+
95
+
96
+ def test_xml_declaration_utf8_alias():
97
+
98
+ data = b'<?xml version="1.0" encoding="UTF8"?>'
99
+
100
+ result = detect_encoding(data)
101
+
102
+ assert result.encoding == "utf-8"
103
+
104
+ assert result.source == "xml-declaration"
105
+
106
+
107
+ def test_shift_jis_alias():
108
+
109
+ data = b'<?xml version="1.0" encoding="Shift-JIS"?>'
110
+
111
+ result = detect_encoding(data)
112
+
113
+ assert result.encoding == "shift_jis"
114
+
115
+ assert result.source == "xml-declaration"
116
+
117
+
118
+ def test_normalize_utf16_alias():
119
+
120
+ from xml_encoding_detector.detector import (
121
+ normalize_encoding,
122
+ )
123
+
124
+ assert normalize_encoding("UTF16LE") == "utf-16-le"
125
+
126
+
127
+ def test_normalize_utf32_alias():
128
+
129
+ from xml_encoding_detector.detector import (
130
+ normalize_encoding,
131
+ )
132
+
133
+ assert normalize_encoding("UTF32BE") == "utf-32-be"
134
+
135
+
136
+ def test_normalize_unknown_encoding():
137
+
138
+ from xml_encoding_detector.detector import (
139
+ normalize_encoding,
140
+ )
141
+
142
+ assert normalize_encoding("GB18030") == "gb18030"
143
+
144
+
145
+ def test_xml_without_encoding():
146
+
147
+ data = b'<?xml version="1.0"?>'
148
+
149
+ result = detect_encoding(data)
150
+
151
+ assert result.encoding == "utf-8"
152
+
153
+ assert result.source == "default"
154
+
155
+
156
+ def test_normalize_windows_1258():
157
+
158
+ from xml_encoding_detector.detector import (
159
+ normalize_encoding,
160
+ )
161
+
162
+ assert normalize_encoding("windows-1258") == "cp1258"
163
+
164
+
165
+ def test_normalize_big5():
166
+
167
+ from xml_encoding_detector.detector import (
168
+ normalize_encoding,
169
+ )
170
+
171
+ assert normalize_encoding("BIG5") == "big5"