preprocess.py 947 B

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. from datetime import datetime
  3. def generate_python_sources():
  4. sources = {}
  5. for file in os.listdir("python"):
  6. assert file.endswith(".py")
  7. key = file.split(".")[0]
  8. with open("python/" + file) as f:
  9. value = f.read()
  10. value = value.encode('utf-8').hex(':')
  11. value = '\\x' + value.replace(':', '\\x')
  12. sources[key] = value
  13. timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  14. header = '''#pragma once
  15. // generated on ''' + timestamp + '''
  16. #include <map>
  17. #include <string>
  18. namespace pkpy{
  19. inline static std::map<std::string, const char*> kPythonLibs = {
  20. '''
  21. for key, value in sources.items():
  22. header += ' '*8 + '{"' + key + '", "' + value + '"},'
  23. header += '\n'
  24. header += '''
  25. };
  26. } // namespace pkpy
  27. '''
  28. return header
  29. with open("src/_generated.h", "w", encoding='utf-8') as f:
  30. f.write(generate_python_sources())