-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_to_header.py
More file actions
90 lines (72 loc) · 3.78 KB
/
python_to_header.py
File metadata and controls
90 lines (72 loc) · 3.78 KB
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
import sys
import os
import time
HEADER_FILE_NAME = 'W:\\ns2 stuff\\programming\\blender compiler\\BlenderCompiler\\BlenderCompiler\\generated_python_literals.h'
PYTHON_FILES = ['W:\\ns2 stuff\\programming\\blender spark compiler (python)\\export_spark_model.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\model_compile_parser.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\spark_animation.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\spark_common.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\spark_model.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\spark_physics.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\spark_writer.py',
'W:\\ns2 stuff\\programming\\blender spark compiler (python)\\blender_compile.py']
contents = '//Automatically generated by python_to_header.py Do not modify'
contents += '\n\n#include <vector>\n#include <string>\n\n'
contents += 'const unsigned long long LAST_UPDATED = <time>;'
contents += '\n\ntypedef const std::wstring p_line;'
contents += '\n\ntypedef const std::vector<p_line> p_file;'
contents += '\n\n'
contents = contents.replace('<time>', str(int(time.time())))
# create a vector that stores the python file names (no directories, just the names)
line_beginning = 'const std::vector<const std::wstring> PYTHON_FILE_NAMES = { '
for i in range(len(PYTHON_FILES)):
if i == 0:
contents += line_beginning
else:
contents += ' ' * len(line_beginning)
file_name = PYTHON_FILES[i].replace('\\','/').split('/')[-1]
contents += 'L"' + file_name + '"'
if i == len(PYTHON_FILES) - 1:
contents += '};\n\n'
else:
contents += ',\n'
#create a vector that stores the python file data
definition = 'const std::vector<p_file> PYTHON_FILE_DATAS = { '
indent = ' ' * len(definition)
for i in range(len(PYTHON_FILES)):
# read python file
f = open(PYTHON_FILES[i], 'r')
python_file_contents = f.read()
f.close()
python_file_contents = python_file_contents.split('\n')
# remove previous timestamp if it exists
first_line = python_file_contents[0].strip().split(' ')
first_line = [x for x in first_line if x] # remove blanks caused by consecutive spaces
if len(first_line) >= 2:
if first_line[0] == '#':
try:
int(first_line[1])
python_file_contents.pop(0)
except ValueError:
pass
python_file_contents.insert(0, '# ' + str(int(time.time())))
file_name = PYTHON_FILES[i].replace('\\','/').split('/')[-1]
for j in range(len(python_file_contents)):
if i == 0 and j == 0:
python_file_contents[j] = definition + '\n' + indent + '// ' + file_name + '\n' + indent + '{\n' + indent + 'L"' + python_file_contents[j].replace('\\','\\\\').replace('"','\\"') + '\\n"'
elif j == 0:
python_file_contents[j] = '\n' + indent + '// ' + file_name + '\n' + indent + '{\n' + indent + 'L"' + python_file_contents[j].replace('\\','\\\\').replace('"','\\"') + '\\n"'
else:
python_file_contents[j] = indent + 'L"' + python_file_contents[j].replace('\\','\\\\').replace('"','\\"') + '\\n"'
if j == len(python_file_contents)-1:
python_file_contents[j] += '}'
else:
python_file_contents[j] += ','
python_file_contents = '\n'.join(python_file_contents)
contents += python_file_contents
if i == len(PYTHON_FILES)-1:
contents += '};'
else:
contents += ',\n'
with open(HEADER_FILE_NAME, 'w') as new_header_file:
new_header_file.write(contents)