-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow add_multi_constructor with None (#358)
Loader.add_multi_constructor(None, myconstructor) Also add test for add_multi_constructor('!', ...) etc. See issue #317
- Loading branch information
Showing
8 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{'Tag1': ['a', 1, 'b', 2]}, | ||
{'Tag2': ['a', 1, 'b', 2]}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
- !Tag1 [a, 1, b, 2] | ||
- !!Tag2 [a, 1, b, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import yaml | ||
import pprint | ||
import sys | ||
|
||
def _load_code(expression): | ||
return eval(expression) | ||
|
||
def myconstructor1(constructor, tag, node): | ||
seq = constructor.construct_sequence(node) | ||
return {tag: seq } | ||
|
||
def myconstructor2(constructor, tag, node): | ||
seq = constructor.construct_sequence(node) | ||
string = '' | ||
try: | ||
i = tag.index('!') + 1 | ||
except: | ||
try: | ||
i = tag.rindex(':') + 1 | ||
except: | ||
pass | ||
if i >= 0: | ||
tag = tag[i:] | ||
return { tag: seq } | ||
|
||
class Multi1(yaml.FullLoader): | ||
pass | ||
class Multi2(yaml.FullLoader): | ||
pass | ||
|
||
def test_multi_constructor(input_filename, code_filename, verbose=False): | ||
input = open(input_filename, 'rb').read().decode('utf-8') | ||
native = _load_code(open(code_filename, 'rb').read()) | ||
|
||
# default multi constructor for ! and !! tags | ||
Multi1.add_multi_constructor('!', myconstructor1) | ||
Multi1.add_multi_constructor('tag:yaml.org,2002:', myconstructor1) | ||
|
||
data = yaml.load(input, Loader=Multi1) | ||
if verbose: | ||
print('Multi1:') | ||
print(data) | ||
print(native) | ||
assert(data == native) | ||
|
||
|
||
# default multi constructor for all tags | ||
Multi2.add_multi_constructor(None, myconstructor2) | ||
|
||
data = yaml.load(input, Loader=Multi2) | ||
if verbose: | ||
print('Multi2:') | ||
print(data) | ||
print(native) | ||
assert(data == native) | ||
|
||
|
||
test_multi_constructor.unittest = ['.multi', '.code'] | ||
|
||
if __name__ == '__main__': | ||
import test_appliance | ||
test_appliance.run(globals()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import yaml | ||
import pprint | ||
import sys | ||
|
||
def _load_code(expression): | ||
return eval(expression) | ||
|
||
def myconstructor1(constructor, tag, node): | ||
seq = constructor.construct_sequence(node) | ||
return {tag: seq } | ||
|
||
def myconstructor2(constructor, tag, node): | ||
seq = constructor.construct_sequence(node) | ||
string = '' | ||
try: | ||
i = tag.index('!') + 1 | ||
except: | ||
try: | ||
i = tag.rindex(':') + 1 | ||
except: | ||
pass | ||
if i >= 0: | ||
tag = tag[i:] | ||
return { tag: seq } | ||
|
||
class Multi1(yaml.FullLoader): | ||
pass | ||
class Multi2(yaml.FullLoader): | ||
pass | ||
|
||
def test_multi_constructor(input_filename, code_filename, verbose=False): | ||
input = open(input_filename, 'rb').read().decode('utf-8') | ||
native = _load_code(open(code_filename, 'rb').read()) | ||
|
||
# default multi constructor for ! and !! tags | ||
Multi1.add_multi_constructor('!', myconstructor1) | ||
Multi1.add_multi_constructor('tag:yaml.org,2002:', myconstructor1) | ||
|
||
data = yaml.load(input, Loader=Multi1) | ||
if verbose: | ||
print('Multi1:') | ||
print(data) | ||
print(native) | ||
assert(data == native) | ||
|
||
|
||
# default multi constructor for all tags | ||
Multi2.add_multi_constructor(None, myconstructor2) | ||
|
||
data = yaml.load(input, Loader=Multi2) | ||
if verbose: | ||
print('Multi2:') | ||
print(data) | ||
print(native) | ||
assert(data == native) | ||
|
||
|
||
test_multi_constructor.unittest = ['.multi', '.code'] | ||
|
||
if __name__ == '__main__': | ||
import test_appliance | ||
test_appliance.run(globals()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters