This repository was archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgen_args_class.py
194 lines (162 loc) · 6.6 KB
/
gen_args_class.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Code was forked from java SDK to generated C# classes rather than Java.
# Refer to Java SDK for instructions on how to use this. Search for the same file name there.
import sys
import re
# change the line below to point to a spec file in Java SDK.
input = open(
'specs\JobExportArgs__GET_search_jobs_export.argspec',
'r')
output = sys.stdout
def read_line():
"""
Returns the next line from the input, including the EOL sequence.
Throws StopIteration on EOF.
Skips comment lines automatically.
"""
while True:
line = next(input)
if line.startswith('#'):
continue
return line
def fix_doc(string):
match = re.search(r"(.*)<p>(.*)", string)
if match:
string = match.group(1) + """
/// <para>
/// """ + match.group(2) + """
/// </para>
"""
match = re.search(r"(.*){@link #set(.*)}", string)
if match:
string = match.group(1);
string += '<see cref="'
string += match.group(2)
string += '''"/>
'''
return string;
def lowercase_first_letter(string):
return string[0].lower() + string[1:]
output.write(""" /* BEGIN AUTOGENERATED CODE */
""")
ignore_duplicates = False
args_already_seen = set()
ignored_machine_names = set()
try:
while True:
machine_name = read_line()[:-1]
if machine_name.startswith('!'):
# Actually this is a directive. Process it.
directive = machine_name
if directive == '!SET ignore_duplicates 1':
ignore_duplicates = True
elif directive.startswith('!SUPPRESS '):
machine_name = directive[len('!SUPPRESS '):]
ignored_machine_names.add(machine_name)
else:
sys.exit("Unknown directive: %s" % directive)
continue
java_name = read_line()[:-1]
type = read_line()[:-1]
description_lines = []
reading_description = True
code_lines = None
while True:
line = read_line()
if line.startswith('!CODE'):
code_lines = []
reading_description = False
continue
if line.startswith('='):
break
if reading_description:
description_lines.append(line)
else:
code_lines.append(line)
if not java_name[0].isupper():
sys.exit("Expected first letter to be uppercase: %s" % java_name)
java_name_lower = lowercase_first_letter(java_name)
# Generate the setter code
if code_lines is not None:
# Custom code for this setter
code = ''.join(code_lines)[:-1];
else:
# Standard code for this setter, depending on its parameter type
code = """ set { this["%s"] = value; }""" % (machine_name)
# Convert Java type to C# equivalent
if type == 'boolean':
type = 'bool'
if type == 'OutputMode' or type == 'TruncationMode' or type == 'SearchMode':
type += 'Enum'
code = """ set { this["%s"] = value.GetSplunkEnumValue(); }""" % (machine_name)
if type == 'String':
type = 'string'
if '[]' in type or type == 'Date':
if type == 'String[]-MULTIPLE':
type = 'string[]'
elif type == 'String[]-CSV':
type = 'string[]'
code = """ // string[] were simply passed thru, it would be encoded as
// as Splunk REST API multi value parameter.
// Instead we want a comma separated string.
this["%s"] = value.ToCsv();""" % (java_name_lower, java_name_lower, machine_name)
else:
sys.exit("Don't know how to encode an array of type: %s" % type);
# Split description_lines -> {method_description_lines, param_description_lines}
method_description_lines = []
param_description_lines = []
saw_description_separator = False
for line in description_lines:
if line.startswith('-'):
saw_description_separator = True
continue
if not saw_description_separator:
method_description_lines.append(line)
else:
param_description_lines.append(line)
if not saw_description_separator:
print ('WARNING: No separate method description found for ' +
'argument %s. This is against convention.') % machine_name
param_description_lines = method_description_lines
method_description_lines = []
# Format method_description_lines
method_description_formatted = ''
if method_description_lines != []:
for line in method_description_lines:
method_description_formatted += ' /// %s' % line
# Fix java doc tags for C# XML Docs for method description.
method_description_formatted = fix_doc(method_description_formatted)
# Format param_description_lines
param_description_formatted = ''
for line in param_description_lines:
param_description_formatted += ' * %s' % line
# Fix java doc tags for C# XML Docs for parameter description.
param_description_formatted = fix_doc(param_description_formatted)
cur_arg = (machine_name, type)
if cur_arg in args_already_seen:
if ignore_duplicates:
continue
else:
sys.exit("Multiple definitions for argument: %s %s" % (
type, machine_name));
else:
args_already_seen.add(cur_arg)
# Output the current argument
if machine_name not in ignored_machine_names:
output.write(""" /// <summary>
%s /// </summary>
public """ % (method_description_formatted))
# "Count" is a Property on ICollection. "new" is needed to avoid conflicts.
if machine_name == 'count':
output.write('new ')
output.write("""%s %s
{
%s
}
""" % (type, java_name,
code))
except StopIteration:
# Done!
pass
output.write(""" /* END AUTOGENERATED CODE */
""")
pass