Skip to content
Snippets Groups Projects

Feature/#483 cleanup opengl mock

Merged Jan Delember requested to merge feature/#483_cleanup_opengl_mock into master
2 files
+ 291
71
Compare changes
  • Side-by-side
  • Inline
Files
2
#------------------------------------------------------------------------------
# Project Phoenix
#
# Copyright (c) 2017-2018 RWTH Aachen University, Germany,
# Virtual Reality & Immersive Visualization Group.
#------------------------------------------------------------------------------
# License
#
# Licensed under the 3-Clause BSD License (the "License");
# you may not use this file except in compliance with the License.
# See the file LICENSE for the full text.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
import textwrap
import os
from collections import defaultdict
params_to_be_sorted = ["functions_to_mock", "allow_calls_provided",
"gl_types"]
def sortListAlphabetically(content):
prefix = content[0:content.find("=")]
print("... sorting " + prefix)
sub_content = content[content.find("[")+1:content.find("']")]
params = sub_content.split("',")
params = sorted(params, key=lambda s: s.lower())
if (params_to_be_sorted[1] in prefix):
sub_content = sortGlewBeforeGl(params)
elif (params_to_be_sorted[2] in prefix):
sub_content = sortGLBeforeRest(params)
else:
sub_content = ', '.join(str(val)+"'" for val in params)
return (prefix+ " = [" + sub_content+"]")
def sortGlewBeforeGl(params):
d = defaultdict(list)
for val in params:
d["glew" in val].append(val)
sortedParams = ','.join(str(p)+"'" for p in d[True])
sortedParams += ','
sortedParams += ','.join(str(p)+"'" for p in d[False])
return sortedParams
def sortGLBeforeRest(params):
d = defaultdict(list)
for val in params:
d["GL" in val].append(val)
sortedParams = ', '.join(str(p)+"'" for p in d[True])
sortedParams += ','
sortedParams += ','.join(str(p)+"'" for p in d[False])
return sortedParams
def printToFile(file, content):
nr_of_blanks = len(content[0:content.find("[")+1])
if (params_to_be_sorted[1] in content):
printWithLineBreakAtComma(file, content, nr_of_blanks)
else:
printWith80CharacterLimit(file, content, nr_of_blanks)
def printWith80CharacterLimit(file, content, nr_of_blanks):
input = ('\n'+' '*nr_of_blanks).join(textwrap.wrap(content,
80-nr_of_blanks))
input += "\n"
file.write(input)
def printWithLineBreakAtComma(file, content, nr_of_blanks):
content = content.replace ("',", "',\n")
params = content.split("\n")
for val in params:
file.write(val+"\n" if val == params[0] else ' '*(nr_of_blanks)+val+"\n")
def main():
file_in = open("Create_openGL_mock.py","r")
file_out = open("tmp.py","w")
collecting_tbs_lines = False
lines_tbs = ''
for line in file_in:
if any(val+"=[" in line.replace(' ', '') for val in params_to_be_sorted):
collecting_tbs_lines = True
if collecting_tbs_lines:
lines_tbs += line.replace('\n', '').replace('\t', '').replace(' ', '')
if "]" in line:
collecting_tbs_lines = False
printToFile(file_out, sortListAlphabetically(lines_tbs))
lines_tbs = ''
else:
file_out.write(line)
file_in.close()
os.remove("Create_openGL_mock.py")
file_out.close()
os.rename("tmp.py", "Create_openGL_mock.py")
print "Done."
print "Cleanup openGL mock ..."
main()
Loading