xml-konwerter 1.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 stanislawbartkowski
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,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: xml_konwerter
3
+ Version: 1.1.0
4
+ Summary: Lightweight XML template processor with variable substitution
5
+ Author-email: Stanisław Bartkowski <stanislawbartkowski@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 stanislawbartkowski
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/stanislawbartkowski/xml_konwerter
29
+ Keywords: xml,template,invoice,ksef
30
+ Classifier: Programming Language :: Python :: 3
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Operating System :: OS Independent
33
+ Requires-Python: >=3.10
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Dynamic: license-file
37
+
38
+ ## Opis
39
+
40
+ Prosty moduł do podstawiania zmiennych w pliku XML traktowanym jako wzorzec.
41
+
42
+ ## Instalacja
43
+ > pip install git+https://github.com/stanislawbartkowski/xml_konwerter.git
@@ -0,0 +1,6 @@
1
+ ## Opis
2
+
3
+ Prosty moduł do podstawiania zmiennych w pliku XML traktowanym jako wzorzec.
4
+
5
+ ## Instalacja
6
+ > pip install git+https://github.com/stanislawbartkowski/xml_konwerter.git
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.setuptools.packages.find]
6
+ where = ["."]
7
+
8
+ [project]
9
+ name = "xml_konwerter"
10
+ version = "1.1.0"
11
+ description = "Lightweight XML template processor with variable substitution"
12
+ readme = "README.md"
13
+ license = { file = "LICENSE" }
14
+ requires-python = ">=3.10"
15
+ authors = [
16
+ { name = "Stanisław Bartkowski", email = "stanislawbartkowski@gmail.com" }
17
+ ]
18
+ keywords = ["xml", "template", "invoice", "ksef"]
19
+ classifiers = [
20
+ "Programming Language :: Python :: 3",
21
+ "License :: OSI Approved :: MIT License",
22
+ "Operating System :: OS Independent",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/stanislawbartkowski/xml_konwerter"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,19 @@
1
+ import os
2
+ from tempfile import NamedTemporaryFile
3
+
4
+ import xml.etree.ElementTree as et
5
+
6
+ from xml_konwerter import konwertujdok
7
+
8
+
9
+ def _wez_test_plik(plik: str) -> str:
10
+ p = os.path.join(os.path.dirname(__file__), "testdata", plik)
11
+ return p
12
+
13
+
14
+ def konwertuj_dok(plik: str, d: dict) -> str:
15
+ plik_path = _wez_test_plik(plik)
16
+ with NamedTemporaryFile() as tfile:
17
+ konwertujdok(sou=plik_path, dest=tfile.name, d=d)
18
+ xml = tfile.read()
19
+ return xml.decode()
@@ -0,0 +1,22 @@
1
+ import unittest
2
+
3
+ from helper import konwertuj_dok
4
+
5
+
6
+ class TestKonwerter(unittest.TestCase):
7
+
8
+ PRZYKLAD = "test_faktura.xml"
9
+
10
+ def test_zamien(self):
11
+ d = {
12
+ "NIP": "NIP_SPRZEDAWCA_123",
13
+ "NIP_NABYWCA": "NABYWCA_999",
14
+ "DATA_WYSTAWIENIA": "2022-99-99",
15
+ "NUMER_FAKTURY": "NUMER-222/99/555"
16
+ }
17
+ xml = konwertuj_dok(self.PRZYKLAD, d)
18
+ print(xml)
19
+ self.assertIn("NIP_SPRZEDAWCA_123", xml)
20
+ self.assertIn("NABYWCA_999", xml)
21
+ self.assertIn("2022-99-99", xml)
22
+ self.assertIn("NUMER-222/99/555", xml)
@@ -0,0 +1,7 @@
1
+ from .konwxml import KONWXML
2
+ from .konwertujdok import konwertujdok
3
+
4
+ __all__ = [
5
+ "KONWXML",
6
+ "konwertujdok"
7
+ ]
@@ -0,0 +1,11 @@
1
+ import xml.etree.ElementTree as et
2
+
3
+ from xml_konwerter import KONWXML
4
+
5
+
6
+ def konwertujdok(sou: str, dest: str, d: dict):
7
+ tree = et.parse(sou)
8
+ root = tree.getroot()
9
+ K = KONWXML(root=root)
10
+ K.replace_all(d=d)
11
+ tree.write(dest)
@@ -0,0 +1,88 @@
1
+ from copy import deepcopy
2
+
3
+
4
+ class KONWXML:
5
+
6
+ def __init__(self, root):
7
+ self._root = root
8
+
9
+ def _replace_text(self, d:dict):
10
+ for elem in self._root.iter():
11
+ te = elem.text
12
+ if te is None:
13
+ continue
14
+ spos = 0
15
+ # znajdz zmienne w linii
16
+ zmset = []
17
+ while True:
18
+ beg = te.find("{{", spos)
19
+ end = te.find("}}", spos)
20
+ if beg >= 0 and end > 0:
21
+ varia = te[beg+2:end]
22
+ if varia in d:
23
+ zmset.append(varia)
24
+ else:
25
+ break
26
+ spos = end+2
27
+
28
+ # teraz zamien
29
+ for zm in zmset:
30
+ subst = "{{" + zm + "}}"
31
+ te = te.replace(subst, d[zm])
32
+
33
+ elem.text = te
34
+
35
+ def _replace_all(self, prefix: str, d: dict, alista: dict):
36
+ self._replace_text(d)
37
+
38
+ def replace_all(self, d: dict):
39
+ self._replace_all(prefix="", d=d, alista=d)
40
+
41
+ def _replace_linie(self, d, alista, plist, klista):
42
+ root = self._root
43
+ taglist = "{{LINIE" + plist + "}}"
44
+ lte = None
45
+ notable = 0
46
+ # tutaj wyszukuj odpowiedniej tabeli oznaczone {{LINIE...}}
47
+ # jest to koślawe, najpierw znajduje tag i zakłada,
48
+ # że nastepny table jest tym szukanym
49
+ for elem in root.iter():
50
+ te = elem.text
51
+ if elem.tag == "table":
52
+ notable += 1
53
+ if te is None:
54
+ continue
55
+ if te == taglist:
56
+ lte = elem
57
+ break
58
+ if lte is None:
59
+ return
60
+ # usuwa znacznik
61
+ elem = root.find(f'.//p[.="{taglist}"]')
62
+ parent = root.find(f'.//p[.="{taglist}"]/..')
63
+ parent.remove(elem)
64
+
65
+ # teraz szuka table o właściwym numerz
66
+ tablepar = root.findall(".//table")[notable]
67
+ # tutaj ./, tylko direct children
68
+ trlist = tablepar.findall("./tr")
69
+ # rozpoznanie, który element jest iterowalny
70
+ tr = trlist[0] if len(trlist) == 1 else trlist[1]
71
+ insert = -1 if len(trlist) <= 2 else 1
72
+ # usun iterowalny element (i potem będzie powielny w pętli)
73
+ lista = alista.get(klista)
74
+ if lista is None:
75
+ for t in trlist:
76
+ tablepar.remove(t)
77
+ return
78
+ tablepar.remove(tr)
79
+ it = iter(lista) if insert == -1 else reversed(lista)
80
+ for e in it:
81
+ trc = deepcopy(tr)
82
+ # _replace_text(trc, e)
83
+ # tutaj rekurencyjnie podmnieniaj zawartość tabeli
84
+ self._replace_all(trc, plist, d | e, e)
85
+ if insert == -1:
86
+ tablepar.append(trc)
87
+ else:
88
+ tablepar.insert(insert, trc)
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: xml_konwerter
3
+ Version: 1.1.0
4
+ Summary: Lightweight XML template processor with variable substitution
5
+ Author-email: Stanisław Bartkowski <stanislawbartkowski@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 stanislawbartkowski
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/stanislawbartkowski/xml_konwerter
29
+ Keywords: xml,template,invoice,ksef
30
+ Classifier: Programming Language :: Python :: 3
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Operating System :: OS Independent
33
+ Requires-Python: >=3.10
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Dynamic: license-file
37
+
38
+ ## Opis
39
+
40
+ Prosty moduł do podstawiania zmiennych w pliku XML traktowanym jako wzorzec.
41
+
42
+ ## Instalacja
43
+ > pip install git+https://github.com/stanislawbartkowski/xml_konwerter.git
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ tests/helper.py
5
+ tests/test.py
6
+ xml_konwerter/__init__.py
7
+ xml_konwerter/konwertujdok.py
8
+ xml_konwerter/konwxml.py
9
+ xml_konwerter.egg-info/PKG-INFO
10
+ xml_konwerter.egg-info/SOURCES.txt
11
+ xml_konwerter.egg-info/dependency_links.txt
12
+ xml_konwerter.egg-info/top_level.txt
@@ -0,0 +1,3 @@
1
+ dist
2
+ tests
3
+ xml_konwerter