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
|
#!/usr/bin/env python
MAX_NUM_FIELDS = 20
HEADER = """\
<block>
<name>Struct Variable</name>
<key>variable_struct</key>
<import>def struct(data): return type('Struct', (object,), data)()</import>
<var_make>self.$id = $id = struct({{#slurp
#for $i in range({0}):
#set $field = $getVar('field' + str(i))
#if len(str($field)) > 2
$field: $getVar('value' + str(i)), #slurp
#end if
#end for
}})</var_make>
<var_value>struct({{#slurp
#for $i in range({0}):
#set $field = $getVar('field' + str(i))
#if len(str($field)) > 2
$field: $getVar('value' + str(i)), #slurp
#end if
#end for
}})</var_value>
<make></make>
"""
FIELD0 = """\
<param>
<name>Field 0</name>
<key>field0</key>
<value>field0</value>
<type>string</type>
<hide>part</hide>
<tab>Fields</tab>
</param>
"""
FIELDS = """\
<param>
<name>Field {0}</name>
<key>field{0}</key>
<value></value>
<type>string</type>
<hide>part</hide>
<tab>Fields</tab>
</param>
"""
VALUES = """\
<param>
<name>$field{0}()</name>
<key>value{0}</key>
<value>0</value>
<type>raw</type>
<hide>#if $field{0}() then 'none' else 'all'#</hide>
</param>
"""
CHECKS = """\
<check>($str($field{0}) or "a")[0].isalpha()</check>
<check>($str($field{0}) or "a").isalnum()</check>
"""
FOOTER = """\
<doc>This is a simple struct/record like variable.
Attribute/field names can be specified in the tab 'Fields'.
For each non-empty field a parameter with type raw is shown.
Value access via the dot operator, e.g. "variable_struct_0.field0"
</doc>
</block>
"""
def make_xml(num_fields):
return ''.join((
HEADER.format(num_fields),
FIELD0, ''.join(FIELDS.format(i) for i in range(1, num_fields)),
''.join(VALUES.format(i) for i in range(num_fields)),
''.join(CHECKS.format(i) for i in range(num_fields)),
FOOTER
))
if __name__ == '__main__':
import sys
try:
filename = sys.argv[1]
except IndexError:
filename = __file__[:-3]
data = make_xml(MAX_NUM_FIELDS)
with open(filename, 'w') as fp:
fp.write(data.encode())
|