update prebuild.py

This commit is contained in:
blueloveTH 2024-03-24 17:39:41 +08:00
parent 881fbb01dd
commit c60fdd29ab
2 changed files with 19 additions and 28 deletions

File diff suppressed because one or more lines are too long

View File

@ -8,11 +8,8 @@ def generate_python_sources():
key = file.split(".")[0] key = file.split(".")[0]
with open("python/" + file) as f: with open("python/" + file) as f:
value = f.read() value = f.read()
value = value.encode('utf-8').hex() const_array = ','.join([str(b) for b in value.encode('utf-8')])
new_value = [] sources[key] = '(const char[]){' + const_array + '}'
for i in range(0, len(value), 2):
new_value.append("\\x" + value[i:i+2])
sources[key] = "".join(new_value)
header = '''#pragma once header = '''#pragma once
// generated by prebuild.py // generated by prebuild.py
@ -21,17 +18,12 @@ def generate_python_sources():
#include <string> #include <string>
namespace pkpy{ namespace pkpy{
inline static std::map<std::string, const char*> kPythonLibs = { inline const std::map<std::string, const char*> kPythonLibs = {
''' '''
for key, value in sources.items(): for key, value in sources.items():
CHAR_LIMIT = 5000 header += f' {{ "{key}", {value} }},\n'
value = ['"' + value[i:i+CHAR_LIMIT] + '" ' for i in range(0, len(value), CHAR_LIMIT)]
value = ''.join(value)
header += ' '*8 + '{"' + key + '", ' + value + '},'
header += '\n'
header += ''' header += ''' };
};
} // namespace pkpy } // namespace pkpy
''' '''
return header return header