Skip to content

Commit 962b93e

Browse files
committed
Adding missing files
1 parent 82fad4e commit 962b93e

File tree

3 files changed

+396
-2
lines changed

3 files changed

+396
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ venv.bak/
6666
virtualenv/
6767

6868
# RethinkDB
69-
convert_protofile.py
70-
prepare_remote_test.py
7169
rethinkdb/ql2_pb2.py
7270
rethinkdb/*.proto
7371
rethinkdb_data/

scripts/convert_protofile.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Copyright 2018-present RethinkDB
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4+
# this file except in compliance with the License. You may obtain a copy of the
5+
# License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software distributed
10+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
# specific language governing permissions and limitations under the License.
13+
#
14+
# This file incorporates work covered by the following copyright:
15+
#
16+
# Copyright 2010-present, The Linux Foundation, portions copyright Google and
17+
# others and used with permission or subject to their respective license
18+
# agreements.
19+
#
20+
# Licensed under the Apache License, Version 2.0 (the "License");
21+
# you may not use this file except in compliance with the License.
22+
# You may obtain a copy of the License at
23+
#
24+
# http://www.apache.org/licenses/LICENSE-2.0
25+
#
26+
# Unless required by applicable law or agreed to in writing, software
27+
# distributed under the License is distributed on an "AS IS" BASIS,
28+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
# See the License for the specific language governing permissions and
30+
# limitations under the License.
31+
32+
'''
33+
Take a .proto file as input and output the a definitions file for a
34+
supported language: javascript, python, ruby
35+
36+
Usually the input file should be ../src/rdb_protocol/ql2.proto
37+
'''
38+
39+
import os
40+
import re
41+
import sys
42+
43+
languageDefs = {
44+
"python": {
45+
"initialIndentLevel": 0,
46+
"header": "# DO NOT EDIT\n# Autogenerated by %s\n" %
47+
os.path.basename(__file__),
48+
"separator": "",
49+
"open": "\n%(tabs)sclass %(name)s:",
50+
"value": "\n%(tabs)s%(name)s = %(value)s",
51+
"empty": "pass",
52+
"close": None,
53+
"closeAlwaysNewLine": False,
54+
"footer": "\n"
55+
},
56+
"ruby": {
57+
"initialIndentLevel": 1,
58+
"header": "# DO NOT EDIT\n# Autogenerated by %s\n\nmodule RethinkDB"
59+
% os.path.basename(__file__),
60+
"separator": "",
61+
"open": "\n%(tabs)smodule %(name)s",
62+
"value": "\n%(tabs)s%(name)s = %(value)s",
63+
"empty": None,
64+
"close": "end",
65+
"closeAlwaysNewLine": True,
66+
"footer": "\nend\n"
67+
},
68+
"javascript": {
69+
"initialIndentLevel": 1,
70+
"header":
71+
"// DO NOT EDIT\n// Autogenerated by %s\n\nmodule.exports = {"
72+
% os.path.basename(__file__),
73+
"separator": ",",
74+
"open": "\n%(tabs)s%(name)s: {",
75+
"value": "\n%(tabs)s%(name)s: %(value)s",
76+
"empty": None,
77+
"close": "}",
78+
"closeAlwaysNewLine": False,
79+
"footer": "\n}\n"
80+
}
81+
}
82+
83+
84+
def convertFile(inputFile, outputFile, language):
85+
assert(inputFile is not None and hasattr(inputFile, 'read'))
86+
assert(outputFile is not None and hasattr(outputFile, 'write'))
87+
assert(language in languageDefs)
88+
89+
messageRegex = re.compile('\s*(message|enum) (?P<name>\w+) \{')
90+
valueRegex = re.compile('\s*(?P<name>\w+)\s*=\s*(?P<value>\w+)')
91+
endRegex = re.compile('\s*\}')
92+
93+
indentLevel = languageDefs[language]["initialIndentLevel"]
94+
lastIndentLevel = languageDefs[language]["initialIndentLevel"] - 1
95+
96+
# -- write headers
97+
98+
outputFile.write(languageDefs[language]["header"])
99+
100+
# -- convert the body
101+
102+
levelHasContent = False
103+
104+
for line in inputFile:
105+
# - open
106+
match = messageRegex.match(line)
107+
if match is not None:
108+
if indentLevel == lastIndentLevel:
109+
outputFile.write(languageDefs[language]["separator"])
110+
if levelHasContent:
111+
outputFile.write("\n" + "\t" * indentLevel)
112+
outputFile.write(languageDefs[language]["open"] % {
113+
'tabs': "\t" * indentLevel,
114+
'name': match.group('name')
115+
})
116+
lastIndentLevel = indentLevel
117+
indentLevel += 1
118+
levelHasContent = False
119+
continue
120+
121+
# - value
122+
match = valueRegex.match(line)
123+
if match is not None:
124+
if indentLevel == lastIndentLevel:
125+
outputFile.write(languageDefs[language]["separator"])
126+
value = match.group('value')
127+
if value.startswith('0x'):
128+
value = int(value, 0)
129+
outputFile.write(languageDefs[language]["value"] % {
130+
'tabs': "\t" * indentLevel,
131+
'name': match.group('name'),
132+
'value': value,
133+
})
134+
lastIndentLevel = indentLevel
135+
levelHasContent = True
136+
continue
137+
138+
# - close
139+
match = endRegex.match(line)
140+
if match is not None:
141+
if not levelHasContent and \
142+
languageDefs[language]["empty"] is not None:
143+
outputFile.write(
144+
"\n" + "\t" * indentLevel +
145+
languageDefs[language]["empty"]
146+
)
147+
lastIndentLevel = indentLevel
148+
if languageDefs[language]["close"] is not None:
149+
if indentLevel == lastIndentLevel or \
150+
languageDefs[language]["closeAlwaysNewLine"] is True:
151+
outputFile.write("\n" + "\t" * (indentLevel - 1))
152+
outputFile.write(languageDefs[language]["close"])
153+
indentLevel -= 1
154+
lastIndentLevel = indentLevel
155+
levelHasContent = True
156+
157+
# -- write footer
158+
outputFile.write(languageDefs[language]["footer"])
159+
160+
if __name__ == '__main__':
161+
import optparse
162+
163+
inputFile = sys.stdin
164+
outputFile = sys.stdout
165+
166+
# -- parse input
167+
168+
parser = optparse.OptionParser()
169+
parser.add_option(
170+
"-l", "--language",
171+
dest="language",
172+
help="write output for language",
173+
metavar="LANG",
174+
choices=list(languageDefs.keys()),
175+
default=None,
176+
)
177+
parser.add_option(
178+
"-i", "--input-file",
179+
dest="inputFile",
180+
help="read from FILE (default STDIN)",
181+
metavar="FILE",
182+
default=None,
183+
)
184+
parser.add_option(
185+
"-o", "--output-file",
186+
dest="outputFile",
187+
help="write to FILE (default STDOUT)",
188+
metavar="FILE",
189+
default=None,
190+
)
191+
192+
(options, args) = parser.parse_args()
193+
194+
if options.language is None:
195+
parser.error("A language option is required")
196+
197+
if options.inputFile is not None:
198+
try:
199+
inputFile = open(options.inputFile, 'r')
200+
except Exception as e:
201+
parser.error("Unable to open the given input file <<%s>>"
202+
", got error: %s" % (inputFile, str(e)))
203+
204+
if options.outputFile is not None:
205+
try:
206+
outputFile = open(options.outputFile, 'w')
207+
except Exception as e:
208+
parser.error("Unable to open the given output file <<%s>>,"
209+
" got error: %s" % (outputFile, str(e)))
210+
211+
convertFile(inputFile, outputFile, options.language)

0 commit comments

Comments
 (0)