-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
153 lines (140 loc) · 4.04 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import kv2
import pprint
example_input = r'''
<!-- dmx encoding keyvalues2 1 format dmx 18 -->
"DmElement"
{
"id" "elementid" "d1d1ffff-18a8-4fa7-ac0c-b436a32e5004"
"name" "string" "untitled"
"testarray" "element_array"
[
"element" "6a1cd593-a1bf-48d2-843a-cc7c8f3ae05e",
"element" "e6f90bc8-3b4c-4ea2-b863-c5ccdfef5954",
"element" "bf783c0a-f0ac-4255-abc3-78e1a958296d",
"element" "84231e1d-1315-46ad-9878-540757b6dfe9",
"nested1"
{
"id" "elementid" "e1dc7c15-75a9-43fa-8f27-20d5ce8993fb"
"name" "string" "asdf1"
"functionName" "string" "asdf2"
},
"nested2"
{
"id" "elementid" "e1dc7c15-75a9-43fa-8f27-20d5ce8993fb"
"name" "string" "asdf3"
"functionName" "string" "asdf4"
}
]
"blah" "element_array"
[
]
"stringtest" "string_array"
[
"foo",
"bar"
]
}
"DmElement2"
{
"id" "elementid" "d1d1ffff-18a8-4fa7-ac0c-b436a32e5004"
"name" "string" "untitled"
"testarray" "element_array"
[
"element" "6a1cd593-a1bf-48d2-843a-cc7c8f3ae05e",
"element" "e6f90bc8-3b4c-4ea2-b863-c5ccdfef5954",
"nested1"
{
"id" "elementid" "e1dc7c15-75a9-43fa-8f27-20d5ce8993fb"
"name" "string" "asdf1"
"functionName" "string" "asdf2"
},
"nested2"
{
"id" "elementid" "e1dc7c15-75a9-43fa-8f27-20d5ce8993fb"
"name" "string" "asdf3"
"functionName" "string" "asdf4"
}
]
"blah" "element_array"
[
]
"stringtest" "string_array"
[
"foo",
"bar"
]
}
'''
# parse a string
input_parsed = kv2.KV2(example_input)
# show the array based representation of the data
# the data is stored as a list of lists in the "base" member
# each list has the member "node" which is a string representing what type of node it is
print(input_parsed.base)
'''
KV2
[ [ 'DmElement',
[ ['id', 'elementid', 'd1d1ffff-18a8-4fa7-ac0c-b436a32e5004'],
['name', 'string', 'untitled'],
[ 'testarray',
'element_array',
[ ['element', '6a1cd593-a1bf-48d2-843a-cc7c8f3ae05e'],
['element', 'e6f90bc8-3b4c-4ea2-b863-c5ccdfef5954'],
['element', 'bf783c0a-f0ac-4255-abc3-78e1a958296d'],
['element', '84231e1d-1315-46ad-9878-540757b6dfe9'],
[ 'nested1',
[ ['id', 'elementid', 'e1dc7c15-75a9-43fa-8f27-20d5ce8993fb'],
['name', 'string', 'asdf1'],
['functionName', 'string', 'asdf2']]],
[ 'nested2',
[ ['id', 'elementid', 'e1dc7c15-75a9-43fa-8f27-20d5ce8993fb'],
['name', 'string', 'asdf3'],
['functionName', 'string', 'asdf4']]]]],
['blah', 'element_array', []],
['stringtest', 'string_array', ['foo', 'bar']],
['n', 'int', 'v']]],
[ 'DmElement2',
[ ['id', 'elementid', 'd1d1ffff-18a8-4fa7-ac0c-b436a32e5004'],
['name', 'string', 'untitled'],
[ 'testarray',
'element_array',
[ ['element', '6a1cd593-a1bf-48d2-843a-cc7c8f3ae05e'],
['element', 'e6f90bc8-3b4c-4ea2-b863-c5ccdfef5954'],
[ 'nested1',
[ ['id', 'elementid', 'e1dc7c15-75a9-43fa-8f27-20d5ce8993fb'],
['name', 'string', 'asdf1'],
['functionName', 'string', 'asdf2']]],
[ 'nested2',
[ ['id', 'elementid', 'e1dc7c15-75a9-43fa-8f27-20d5ce8993fb'],
['name', 'string', 'asdf3'],
['functionName', 'string', 'asdf4']]]]],
['blah', 'element_array', []],
['stringtest', 'string_array', ['foo', 'bar']]]]]
'''
# add something
# label it with the proper node type or it can't be serialized to string
new_item1 = kv2.NodeList(["n", "int", "v"], 'value_typed')
input_parsed.base[0][1].append(new_item1)
# to add something complicated it is better to do it through a string
# and pass it to the parser
complicated = '''
"CustomObject"
{
"a" "b" "c"
"blah" "element_array"
[
"element" "asdffdsa"
"foo"
{
"bar" "baz" "box"
}
]
}
'''
complicated_array_form = kv2.KV2(complicated).base[0]
input_parsed.base.append(complicated_array_form)
# delete things how you would normally delete from a regular python list
input_parsed.base.pop()
# serialize back to a string
string_form = str(input_parsed)
#print(string_form)