Skip to content

Commit c76f954

Browse files
jeanseb6windrjarry
authored andcommitted
context: simplify parse flag names
The flag arguments names are very long and this makes hard to read code. Remove the parser_ and validation_ prefixes to the argument names. There are almost no name collisions except for no_state. Since it makes no sense to pass this flag only to validation or parser, it translates to both validation and parser.
1 parent 52200dc commit c76f954

File tree

2 files changed

+55
-76
lines changed

2 files changed

+55
-76
lines changed

libyang/context.py

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -311,27 +311,26 @@ def parse_data(
311311
in_type: IOType,
312312
in_data: Union[str, bytes, IO],
313313
parent: DNode = None,
314-
parser_lyb_mod_update: bool = False,
315-
parser_no_state: bool = False,
316-
parser_parse_only: bool = False,
317-
parser_opaq: bool = False,
318-
parser_ordered: bool = False,
319-
parser_strict: bool = False,
320-
validation_no_state: bool = False,
321-
validation_validate_present: bool = False,
314+
lyb_mod_update: bool = False,
315+
no_state: bool = False,
316+
parse_only: bool = False,
317+
opaq: bool = False,
318+
ordered: bool = False,
319+
strict: bool = False,
320+
validate_present: bool = False,
322321
) -> Optional[DNode]:
323322
if self.cdata is None:
324323
raise RuntimeError("context already destroyed")
325324
parser_flgs = parser_flags(
326-
lyb_mod_update=parser_lyb_mod_update,
327-
no_state=parser_no_state,
328-
parse_only=parser_parse_only,
329-
opaq=parser_opaq,
330-
ordered=parser_ordered,
331-
strict=parser_strict,
325+
lyb_mod_update=lyb_mod_update,
326+
no_state=no_state,
327+
parse_only=parse_only,
328+
opaq=opaq,
329+
ordered=ordered,
330+
strict=strict,
332331
)
333332
validation_flgs = validation_flags(
334-
no_state=validation_no_state, validate_present=validation_validate_present
333+
no_state=no_state, validate_present=validate_present
335334
)
336335
fmt = data_format(fmt)
337336
encode = True
@@ -376,57 +375,53 @@ def parse_data_mem(
376375
data: Union[str, bytes],
377376
fmt: str,
378377
parent: DNode = None,
379-
parser_lyb_mod_update: bool = False,
380-
parser_no_state: bool = False,
381-
parser_parse_only: bool = False,
382-
parser_opaq: bool = False,
383-
parser_ordered: bool = False,
384-
parser_strict: bool = False,
385-
validation_no_state: bool = False,
386-
validation_validate_present: bool = False,
378+
lyb_mod_update: bool = False,
379+
no_state: bool = False,
380+
parse_only: bool = False,
381+
opaq: bool = False,
382+
ordered: bool = False,
383+
strict: bool = False,
384+
validate_present: bool = False,
387385
) -> Optional[DNode]:
388386
return self.parse_data(
389387
fmt,
390388
in_type=IOType.MEMORY,
391389
in_data=data,
392390
parent=parent,
393-
parser_lyb_mod_update=parser_lyb_mod_update,
394-
parser_no_state=parser_no_state,
395-
parser_parse_only=parser_parse_only,
396-
parser_opaq=parser_opaq,
397-
parser_ordered=parser_ordered,
398-
parser_strict=parser_strict,
399-
validation_no_state=validation_no_state,
400-
validation_validate_present=validation_validate_present,
391+
lyb_mod_update=lyb_mod_update,
392+
no_state=no_state,
393+
parse_only=parse_only,
394+
opaq=opaq,
395+
ordered=ordered,
396+
strict=strict,
397+
validate_present=validate_present,
401398
)
402399

403400
def parse_data_file(
404401
self,
405402
fileobj: IO,
406403
fmt: str,
407404
parent: DNode = None,
408-
parser_lyb_mod_update: bool = False,
409-
parser_no_state: bool = False,
410-
parser_parse_only: bool = False,
411-
parser_opaq: bool = False,
412-
parser_ordered: bool = False,
413-
parser_strict: bool = False,
414-
validation_no_state: bool = False,
415-
validation_validate_present: bool = False,
405+
lyb_mod_update: bool = False,
406+
no_state: bool = False,
407+
parse_only: bool = False,
408+
opaq: bool = False,
409+
ordered: bool = False,
410+
strict: bool = False,
411+
validate_present: bool = False,
416412
) -> Optional[DNode]:
417413
return self.parse_data(
418414
fmt,
419415
in_type=IOType.FD,
420416
in_data=fileobj,
421417
parent=parent,
422-
parser_lyb_mod_update=parser_lyb_mod_update,
423-
parser_no_state=parser_no_state,
424-
parser_parse_only=parser_parse_only,
425-
parser_opaq=parser_opaq,
426-
parser_ordered=parser_ordered,
427-
parser_strict=parser_strict,
428-
validation_no_state=validation_no_state,
429-
validation_validate_present=validation_validate_present,
418+
lyb_mod_update=lyb_mod_update,
419+
no_state=no_state,
420+
parse_only=parse_only,
421+
opaq=opaq,
422+
ordered=ordered,
423+
strict=strict,
424+
validate_present=validate_present,
430425
)
431426

432427
def __iter__(self) -> Iterator[Module]:

tests/test_data.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def tearDown(self):
6969
"""
7070

7171
def test_data_parse_config_json(self):
72-
dnode = self.ctx.parse_data_mem(
73-
self.JSON_CONFIG, "json", validation_no_state=True
74-
)
72+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", no_state=True)
7573
self.assertIsInstance(dnode, DContainer)
7674
try:
7775
j = dnode.print_mem("json", with_siblings=True)
@@ -113,9 +111,7 @@ def test_data_parse_config_json(self):
113111
"""
114112

115113
def test_data_add_path(self):
116-
dnode = self.ctx.parse_data_mem(
117-
self.JSON_CONFIG, "json", validation_no_state=True
118-
)
114+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", no_state=True)
119115
dnode.new_path(
120116
'/yolo-system:conf/url[host="barfoo.com"][proto="http"]/path',
121117
"/barfoo/index.html",
@@ -131,13 +127,13 @@ def test_data_add_path(self):
131127

132128
def test_data_parse_config_json_file(self):
133129
with open(self.JSON_CONFIG_FILE, encoding="utf-8") as f:
134-
dnode = self.ctx.parse_data_file(f, "json", validation_no_state=True)
130+
dnode = self.ctx.parse_data_file(f, "json", no_state=True)
135131
self.assertIsInstance(dnode, DContainer)
136132
dnode.free()
137133

138134
with open(self.JSON_CONFIG_FILE, encoding="utf-8") as f:
139135
dnode = self.ctx.parse_data(
140-
"json", in_data=f, in_type=IOType.FILE, validation_no_state=True
136+
"json", in_data=f, in_type=IOType.FILE, no_state=True
141137
)
142138
self.assertIsInstance(dnode, DContainer)
143139
dnode.free()
@@ -146,7 +142,7 @@ def test_data_parse_config_json_file(self):
146142
"json",
147143
in_data=self.JSON_CONFIG_FILE,
148144
in_type=IOType.FILEPATH,
149-
validation_no_state=True,
145+
no_state=True,
150146
)
151147
self.assertIsInstance(dnode, DContainer)
152148
dnode.free()
@@ -180,9 +176,7 @@ def test_data_parse_config_json_file(self):
180176
"""
181177

182178
def test_data_parse_state_json(self):
183-
dnode = self.ctx.parse_data_mem(
184-
self.JSON_STATE, "json", validation_validate_present=True
185-
)
179+
dnode = self.ctx.parse_data_mem(self.JSON_STATE, "json", validate_present=True)
186180
self.assertIsInstance(dnode, DContainer)
187181
try:
188182
j = dnode.print_mem("json", with_siblings=True)
@@ -213,9 +207,7 @@ def test_data_parse_state_json(self):
213207
"""
214208

215209
def test_data_parse_config_xml(self):
216-
dnode = self.ctx.parse_data_mem(
217-
self.XML_CONFIG, "xml", validation_validate_present=True
218-
)
210+
dnode = self.ctx.parse_data_mem(self.XML_CONFIG, "xml", validate_present=True)
219211
self.assertIsInstance(dnode, DContainer)
220212
try:
221213
xml = dnode.print_mem("xml", with_siblings=True)
@@ -246,9 +238,7 @@ def test_data_parse_config_xml(self):
246238
"""
247239

248240
def test_data_parse_data_xml(self):
249-
dnode = self.ctx.parse_data_mem(
250-
self.XML_STATE, "xml", validation_validate_present=True
251-
)
241+
dnode = self.ctx.parse_data_mem(self.XML_STATE, "xml", validate_present=True)
252242
self.assertIsInstance(dnode, DContainer)
253243
try:
254244
xml = dnode.print("xml", out_type=IOType.MEMORY, with_siblings=True)
@@ -363,9 +353,7 @@ def test_data_create_invalid_regexp(self):
363353
}
364354

365355
def test_data_to_dict_config(self):
366-
dnode = self.ctx.parse_data_mem(
367-
self.JSON_CONFIG, "json", validation_validate_present=True
368-
)
356+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
369357
self.assertIsInstance(dnode, DContainer)
370358
try:
371359
dic = dnode.print_dict()
@@ -747,11 +735,11 @@ def test_notification_from_dict_module(self):
747735

748736
def test_data_diff(self):
749737
dnode1 = self.ctx.parse_data_mem(
750-
self.XML_DIFF_STATE1, "xml", validation_validate_present=True
738+
self.XML_DIFF_STATE1, "xml", validate_present=True
751739
)
752740
self.assertIsInstance(dnode1, DContainer)
753741
dnode2 = self.ctx.parse_data_mem(
754-
self.XML_DIFF_STATE2, "xml", validation_validate_present=True
742+
self.XML_DIFF_STATE2, "xml", validate_present=True
755743
)
756744
self.assertIsInstance(dnode2, DContainer)
757745

@@ -761,9 +749,7 @@ def test_data_diff(self):
761749
dnode2.free()
762750

763751
def test_find_one(self):
764-
dnode = self.ctx.parse_data_mem(
765-
self.JSON_CONFIG, "json", validation_validate_present=True
766-
)
752+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
767753
self.assertIsInstance(dnode, DContainer)
768754
try:
769755
hostname = dnode.find_one("hostname")
@@ -773,9 +759,7 @@ def test_find_one(self):
773759
dnode.free()
774760

775761
def test_find_all(self):
776-
dnode = self.ctx.parse_data_mem(
777-
self.JSON_CONFIG, "json", validation_validate_present=True
778-
)
762+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
779763
self.assertIsInstance(dnode, DContainer)
780764
try:
781765
urls = dnode.find_all("url")

0 commit comments

Comments
 (0)