pyRegRep 5__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.
- pyregrep-5/LICENSE +21 -0
- pyregrep-5/PKG-INFO +11 -0
- pyregrep-5/pyRegRep.egg-info/PKG-INFO +11 -0
- pyregrep-5/pyRegRep.egg-info/SOURCES.txt +9 -0
- pyregrep-5/pyRegRep.egg-info/dependency_links.txt +1 -0
- pyregrep-5/pyRegRep.egg-info/top_level.txt +1 -0
- pyregrep-5/pyRegRep4/RIMParsing.py +135 -0
- pyregrep-5/pyRegRep4/__init__.py +0 -0
- pyregrep-5/setup.cfg +4 -0
- pyregrep-5/setup.py +12 -0
- pyregrep-5/tests/test_parsing_1.py +32 -0
pyregrep-5/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shapovalov Andrey
|
|
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.
|
pyregrep-5/PKG-INFO
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyRegRep4
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from lxml import etree
|
|
4
|
+
|
|
5
|
+
_logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Parsing:
|
|
9
|
+
def __init__(self, doc: bytes):
|
|
10
|
+
self.xml = doc.decode("utf-8")
|
|
11
|
+
parser = etree.XMLParser(remove_comments=True)
|
|
12
|
+
self.doc = etree.fromstring(doc, parser)
|
|
13
|
+
self._ns = {}
|
|
14
|
+
for perfix, uri in self.doc.nsmap.items():
|
|
15
|
+
if perfix is None:
|
|
16
|
+
perfix = "default"
|
|
17
|
+
self._ns.update({perfix: uri})
|
|
18
|
+
self.query = self.doc.find(".//query:Query", namespaces=self._ns)
|
|
19
|
+
self.exception = self.doc.find(".//rs:Exception", namespaces=self._ns)
|
|
20
|
+
self.objects = self.doc.findall(".//rim:RegistryObject", namespaces=self._ns)
|
|
21
|
+
self.slots = self.__list_slots()
|
|
22
|
+
|
|
23
|
+
def __tname(self, ns: str, tag: str) -> str:
|
|
24
|
+
if not ns:
|
|
25
|
+
return tag
|
|
26
|
+
return f"{{{self._ns[ns]}}}{tag}"
|
|
27
|
+
|
|
28
|
+
def __slot(self, name: str) -> etree._Element | None:
|
|
29
|
+
slot = self.doc.find(f".//rim:Slot[@name='{name}']", namespaces=self._ns)
|
|
30
|
+
if slot is None:
|
|
31
|
+
return None
|
|
32
|
+
return slot
|
|
33
|
+
|
|
34
|
+
def __value(self, slot: etree._Element):
|
|
35
|
+
slot = slot[0]
|
|
36
|
+
type_ = slot.get(self.__tname("xsi", "type"), '').split(":")[1].strip() #
|
|
37
|
+
|
|
38
|
+
if "AnyValueType" in type_:
|
|
39
|
+
return type_, slot[0]
|
|
40
|
+
|
|
41
|
+
elif "InternationalStringValueType" in type_:
|
|
42
|
+
values = []
|
|
43
|
+
for i, item in enumerate(list(slot[0])):
|
|
44
|
+
value = {
|
|
45
|
+
"lang": item.get(self.__tname("xsi", "lang")),
|
|
46
|
+
"value": item.get("value"),
|
|
47
|
+
}
|
|
48
|
+
values.append(value)
|
|
49
|
+
return type_, values
|
|
50
|
+
|
|
51
|
+
elif "CollectionValueType" in type_:
|
|
52
|
+
values = []
|
|
53
|
+
for i, item in enumerate(list(slot)):
|
|
54
|
+
wrapper = etree.Element("wrapper")
|
|
55
|
+
wrapper.append(item)
|
|
56
|
+
values.append(self.__value(wrapper))
|
|
57
|
+
return type_, values
|
|
58
|
+
else:
|
|
59
|
+
return type_, slot[0].text
|
|
60
|
+
|
|
61
|
+
def __list_slots(self) -> dict[str, tuple[str, str]]:
|
|
62
|
+
edm: dict = {"doc": {}, "query": {}, "exception": {}, "object": {}}
|
|
63
|
+
for slot in list(self.doc):
|
|
64
|
+
if slot.tag == self.__tname("rim", "Slot"):
|
|
65
|
+
name = slot.get("name")
|
|
66
|
+
edm["doc"].update({name: (self.__value(slot))})
|
|
67
|
+
continue
|
|
68
|
+
elif slot.tag == self.__tname("query", "Query"):
|
|
69
|
+
for query in list(slot):
|
|
70
|
+
name = query.get("name")
|
|
71
|
+
edm["query"].update({name: (self.__value(query))})
|
|
72
|
+
continue
|
|
73
|
+
elif slot.tag == self.__tname("rs", "Exception"):
|
|
74
|
+
for exception in list(slot):
|
|
75
|
+
name = exception.get("name")
|
|
76
|
+
edm["exception"].update({name: (self.__value(exception))})
|
|
77
|
+
continue
|
|
78
|
+
elif slot.tag == self.__tname("rim", "RegistryObjectList"):
|
|
79
|
+
for registryObject in list(slot):
|
|
80
|
+
ev = {}
|
|
81
|
+
for evidense in list(registryObject):
|
|
82
|
+
if evidense.tag == self.__tname("rim", "Slot"):
|
|
83
|
+
ev.update({evidense.get("name"): self.__value(evidense)})
|
|
84
|
+
if evidense.tag == self.__tname("rim", "RepositoryItemRef"):
|
|
85
|
+
ev.update(
|
|
86
|
+
{
|
|
87
|
+
"RepositoryItemRef": {
|
|
88
|
+
"xlink": evidense.get(
|
|
89
|
+
self.__tname("xlink", "href")
|
|
90
|
+
),
|
|
91
|
+
"title": evidense.get(
|
|
92
|
+
self.__tname("xlink", "title")
|
|
93
|
+
),
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
edm["object"].update(ev)
|
|
98
|
+
continue
|
|
99
|
+
else:
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
return edm
|
|
103
|
+
|
|
104
|
+
def serialize(self):
|
|
105
|
+
def transform_data(data):
|
|
106
|
+
if isinstance(data, list):
|
|
107
|
+
return [transform_data(item) for item in data]
|
|
108
|
+
|
|
109
|
+
elif isinstance(data, dict):
|
|
110
|
+
return {k: transform_data(v) for k, v in data.items()}
|
|
111
|
+
|
|
112
|
+
elif isinstance(data, tuple) and len(data) == 2:
|
|
113
|
+
data_type, value = data
|
|
114
|
+
|
|
115
|
+
if data_type == 'BooleanValueType':
|
|
116
|
+
return value.lower() == 'true'
|
|
117
|
+
|
|
118
|
+
elif data_type == 'StringValueType':
|
|
119
|
+
return value.strip()
|
|
120
|
+
|
|
121
|
+
elif data_type == 'CollectionValueType':
|
|
122
|
+
return transform_data(value)
|
|
123
|
+
|
|
124
|
+
elif data_type == 'AnyValueType':
|
|
125
|
+
return value
|
|
126
|
+
|
|
127
|
+
elif data_type == 'InternationalStringValueType':
|
|
128
|
+
return transform_data(value)
|
|
129
|
+
|
|
130
|
+
# Якщо тип не підпав під умови, повертаємо значення як є
|
|
131
|
+
return transform_data(value) if isinstance(value, (list, dict)) else value
|
|
132
|
+
|
|
133
|
+
return data
|
|
134
|
+
|
|
135
|
+
return transform_data(self.slots)
|
|
File without changes
|
pyregrep-5/setup.cfg
ADDED
pyregrep-5/setup.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="pyRegRep",
|
|
5
|
+
version="5",
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
license="MIT",
|
|
8
|
+
author="Andrii Shapovalov",
|
|
9
|
+
author_email="mt.andrey@gmail.com",
|
|
10
|
+
include_package_data=True,
|
|
11
|
+
install_requires=[],
|
|
12
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from pyRegRep4.RIMParsing import Parsing
|
|
2
|
+
import logging
|
|
3
|
+
|
|
4
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
5
|
+
_logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
files = [
|
|
9
|
+
"./EDM_Ferst_Request.xml",
|
|
10
|
+
"./EDM_Ferst_Response.xml",
|
|
11
|
+
"./EDM_Second_Request.xml",
|
|
12
|
+
"./EDM_Second_Response.xml",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
xmls: list[dict[str, bytes]] = []
|
|
16
|
+
|
|
17
|
+
for file in files:
|
|
18
|
+
with open(file, "rb") as f:
|
|
19
|
+
d = {file[2:]: f.read()}
|
|
20
|
+
xmls.append(d)
|
|
21
|
+
|
|
22
|
+
for xml in xmls:
|
|
23
|
+
for key, value in xml.items():
|
|
24
|
+
print("")
|
|
25
|
+
print("=" * 100)
|
|
26
|
+
print(key)
|
|
27
|
+
print("-" * 100)
|
|
28
|
+
edm = Parsing(value)
|
|
29
|
+
print(edm.slots)
|
|
30
|
+
print(edm.serialize())
|
|
31
|
+
for k, v in edm.serialize().items():
|
|
32
|
+
print(k, v)
|