Skip to content

Commit de47cec

Browse files
authored
Merge pull request #12 from clauspruefer/v1.0
V1.0
2 parents 93d442b + bcfaa55 commit de47cec

7 files changed

Lines changed: 54 additions & 33 deletions

File tree

.github/workflows/pylint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
pip install pylint
2424
- name: Analysing the code with pylint
2525
run: |
26-
pylint --disable=W0622,R1710,R0902,W0612,C0103,C0114,C0115,C0116,C0209,R0801,R0903,R0205,R1737,R0913,W1202,W0246,W0223,W0221,W0104,W0102,W0613,E0401,E0611,E1101 $(git ls-files 'src/microparser.py')
26+
pylint --disable=C0103,C0114,C0209,E0401,E1101,W0212,W0612,W1202,R0902 $(git ls-files 'src/')

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Version 1.0
4+
5+
- Add Virtual Environment install instructions
6+
- Add pylint linting / CI
7+
- Correct source according to pylint
8+
- Adapt test code
9+
310
## Version 1.0rc1
411

512
- Correct README.md (package name and version)

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ Or download the current Release Zip / Tarball @ [Release 1.0rc1](https://github.
2222
> Since Python 3.3 (PEP 405) Virtual Environments are proposed.
2323
2424
```bash
25-
# install into virtual environment
25+
# setup virtual-env
2626
python3 -m venv .xml-microparser
27+
28+
# activate virtual-env
2729
source .xml-microparser/bin/activate
30+
31+
# upgrade pip
2832
python3 -m pip install --upgrade pip
33+
34+
# install xmlmicroparser module
2935
pip3 install xmlmicroparser
36+
37+
# install dependencies
38+
pip3 install pytest pytest-pep8
3039
```
3140

3241
## 2.1. Dependencies

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "xmlmicroparser"
7-
version = "1.0rc1"
7+
version = "1.0"
88
authors = [
99
{ name="Claus Prüfer", email="pruefer@webcodex.de" },
1010
]

src/microparser.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,17 @@ def build_serializer(self):
106106
"""
107107
self._add_child_elements_recursive(self.get_root_element())
108108

109-
def get_element_by_id(self, id):
109+
def get_element_by_id(self, element_id):
110110
""" Get element by internal numerical id.
111111
112-
:param int id: internal numerical element id
112+
:param int element_id: internal numerical element id
113113
:return: found element
114114
:rtype: Element or None
115115
"""
116116
for element in self._elements:
117-
if element.get_id() == id:
117+
if element.get_id() == element_id:
118118
return element
119+
return None
119120

120121
def get_element_by_name(self, name):
121122
""" Get element by xml element id.
@@ -127,6 +128,7 @@ def get_element_by_name(self, name):
127128
for element in self._elements:
128129
if element.get_name() == name:
129130
return element
131+
return None
130132

131133
def get_elements(self):
132134
""" Return processed elements list.
@@ -144,15 +146,15 @@ def get_root_element(self):
144146
"""
145147
return self._elements[0]
146148

147-
def get_child_elements_by_id(self, id):
149+
def get_child_elements_by_id(self, element_id):
148150
""" Return elements children searched by element numerical id.
149151
150-
:param int id: element internal numerical id
152+
:param int element_id: element internal numerical id
151153
:yield: found item
152154
:rtype: elements (list of objects) or None
153155
"""
154156
for item in self._elements:
155-
if item.get_parent_id() == id:
157+
if item.get_parent_id() == element_id:
156158
yield item
157159

158160
def get_child_element(self, name):
@@ -179,8 +181,8 @@ def process_json(self):
179181
if element.get_child_element_count() == 0:
180182
process_ids.append(element.get_id())
181183

182-
for id in process_ids:
183-
root_element.get_element_by_element_id(id).json_transform()
184+
for element_id in process_ids:
185+
root_element.get_element_by_element_id(element_id).json_transform()
184186

185187
root_element.set_json_attributes()
186188

@@ -253,10 +255,10 @@ def _current_item_id_gen(self):
253255
254256
:rtype: Iterator[int]
255257
"""
256-
id = 0
258+
element_id = 0
257259
while True:
258-
id += 1
259-
yield id
260+
element_id += 1
261+
yield element_id
260262

261263
def _parse_line(self, line):
262264
""" Parse single xml data line.
@@ -275,7 +277,7 @@ def _parse_line(self, line):
275277

276278
args = {
277279
'name': element_id,
278-
'id': self._current_item_id.__next__(),
280+
'element_id': self._current_item_id.__next__(),
279281
'line_nr': line_nr,
280282
'parent_id': self._get_last_unclosed_element_id()
281283
}
@@ -387,6 +389,7 @@ def get_element_by_element_id(self, element_id):
387389
for element in self.iterate():
388390
if element.get_id() == element_id:
389391
return element
392+
return None
390393

391394
def get_element_by_element_name(self, element_name):
392395
""" Get element by element numerical id.
@@ -397,6 +400,7 @@ def get_element_by_element_name(self, element_name):
397400
for element in self.iterate():
398401
if element.get_name() == element_name:
399402
return element
403+
return None
400404

401405

402406
class Element(Serializer):
@@ -407,10 +411,10 @@ class Element(Serializer):
407411
part of this class.
408412
"""
409413

410-
def __init__(self, *, name, id, line_nr, parent_id):
414+
def __init__(self, *, name, element_id, line_nr, parent_id):
411415
"""
412416
:param str name: xml element name (id)
413-
:param int id: xml element internal processing id
417+
:param int element_id: xml element internal processing id
414418
:param int line_nr: line number of found xml opening tag in payload data
415419
:param int parent_id: parent element numerical id
416420
:ivar str _name: xml element id
@@ -423,14 +427,14 @@ def __init__(self, *, name, id, line_nr, parent_id):
423427
"""
424428

425429
assert isinstance(name, str), 'name must be string type'
426-
assert isinstance(id, int), 'id must be int type'
430+
assert isinstance(element_id, int), 'id must be int type'
427431
assert isinstance(line_nr, int), 'id must be int type'
428432

429433
if parent_id is not None:
430434
assert isinstance(parent_id, int), 'parent_id must be int type'
431435

432436
self._name = name
433-
self._id = id
437+
self._id = element_id
434438
self._parent_id = parent_id
435439
self._parent_element = None
436440
self._attributes = {}
@@ -547,6 +551,7 @@ def get_attribute_by_name(self, name):
547551
"""
548552
if name in self._attributes:
549553
return self._attributes[name]
554+
return None
550555

551556
def get_attributes(self):
552557
""" Get attributes.

test/integration/test_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def check_element(element, properties):
8-
assert element._id == properties['id']
8+
assert element._id == properties['element_id']
99
assert element._name == properties['name']
1010
assert element._attributes == properties['attributes']
1111
assert element._parent_id == properties['parent_id']
@@ -32,7 +32,7 @@ def test_basic_parsing(self, caplog):
3232
elements = p.get_elements()
3333

3434
props1 = {
35-
'id': 1,
35+
'element_id': 1,
3636
'name': 'outertag',
3737
'attributes': {'test1': 'string1', 'test2': 'string2'},
3838
'parent_id': None,
@@ -44,7 +44,7 @@ def test_basic_parsing(self, caplog):
4444
check_element(elements[0], props1)
4545

4646
props2 = {
47-
'id': 2,
47+
'element_id': 2,
4848
'name': 'innertag1',
4949
'attributes': {},
5050
'parent_id': 1,
@@ -56,7 +56,7 @@ def test_basic_parsing(self, caplog):
5656
check_element(elements[1], props2)
5757

5858
props3 = {
59-
'id': 3,
59+
'element_id': 3,
6060
'name': 'innertag2',
6161
'attributes': {},
6262
'parent_id': 2,

test/unit/test_element.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ def test_attribute_add_valid_types(self, caplog):
1212

1313
e = microparser.Element(
1414
name = 'element1',
15-
id = 1,
15+
element_id = 1,
1616
line_nr = 1,
1717
parent_id = None
1818
)
1919

2020
e = microparser.Element(
2121
name = 'element2',
22-
id = 100,
22+
element_id = 100,
2323
line_nr = 10,
2424
parent_id = 10
2525
)
@@ -31,39 +31,39 @@ def test_attribute_add_invalid_types(self, caplog):
3131
with pytest.raises(AssertionError):
3232
e = microparser.Element(
3333
name = 1,
34-
id = 1,
34+
element_id = 1,
3535
line_nr = 1,
3636
parent_id = None
3737
)
3838

3939
with pytest.raises(AssertionError):
4040
e = microparser.Element(
4141
name = 'element',
42-
id = 'name',
42+
element_id = 'name',
4343
line_nr = 1,
4444
parent_id = None
4545
)
4646

4747
with pytest.raises(AssertionError):
4848
e = microparser.Element(
4949
name = 'element',
50-
id = 1,
50+
element_id = 1,
5151
line_nr = {},
5252
parent_id = None
5353
)
5454

5555
with pytest.raises(AssertionError):
5656
e = microparser.Element(
5757
name = None,
58-
id = 1,
58+
element_id = 1,
5959
line_nr = 1,
6060
parent_id = 10
6161
)
6262

6363
with pytest.raises(AssertionError):
6464
e = microparser.Element(
6565
name = 'element',
66-
id = 1,
66+
element_id = 1,
6767
line_nr = 1,
6868
parent_id = 'test'
6969
)
@@ -77,7 +77,7 @@ def test_get_element_by_id(self, caplog):
7777
p._elements.append(
7878
microparser.Element(
7979
name = 'element1',
80-
id = 101,
80+
element_id = 101,
8181
line_nr = 100,
8282
parent_id = None
8383
)
@@ -86,7 +86,7 @@ def test_get_element_by_id(self, caplog):
8686
p._elements.append(
8787
microparser.Element(
8888
name = 'element2',
89-
id = 210,
89+
element_id = 210,
9090
line_nr = 101,
9191
parent_id = 101
9292
)
@@ -95,7 +95,7 @@ def test_get_element_by_id(self, caplog):
9595
p._elements.append(
9696
microparser.Element(
9797
name = 'element3',
98-
id = 28,
98+
element_id = 28,
9999
line_nr = 200,
100100
parent_id = 101
101101
)

0 commit comments

Comments
 (0)