format_idf_target.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import re
  2. TARGET_NAMES = {'esp32': 'ESP32', 'esp32s2': 'ESP32-S2'}
  3. TOOLCHAIN_NAMES = {'esp32': 'esp', 'esp32s2': 'esp32s2'}
  4. CONFIG_PREFIX = {'esp32': 'ESP32', 'esp32s2': 'ESP32S2'}
  5. TRM_EN_URL = {'esp32': 'https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf',
  6. 'esp32s2': 'https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf'}
  7. TRM_CN_URL = {'esp32': 'https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_cn.pdf',
  8. 'esp32s2': 'https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_cn.pdf'}
  9. class StringSubstituter:
  10. """ Allows for string substitution of target related strings
  11. before any markup is parsed
  12. Supports the following replacements (examples shown is for target=esp32s2):
  13. {IDF_TARGET_NAME}, replaced with the current target name, e.g. ESP32-S2 Beta
  14. {IDF_TARGET_PATH_NAME}, replaced with the path name, e.g. esp32s2
  15. {IDF_TARGET_TOOLCHAIN_NAME}, replaced with the toolchain name, e.g. esp32s2
  16. {IDF_TARGET_CFG_PREFIX}, replaced with the prefix used for config parameters, e.g. ESP32S2
  17. {IDF_TARGET_TRM_EN_URL}, replaced with the url to the English technical reference manual
  18. {IDF_TARGET_TRM_CH_URL}, replaced with the url to the Chinese technical reference manual
  19. Also supports defines of local (single rst file) with the format:
  20. {IDF_TARGET_TX_PIN:default="IO3",esp32="IO4",esp32s2="IO5"}
  21. This will define a replacement of the tag {IDF_TARGET_TX_PIN} in the current rst-file, see e.g. uart.rst for example
  22. """
  23. RE_PATTERN = re.compile(r'^\s*{IDF_TARGET_(\w+?):(.+?)}', re.MULTILINE)
  24. def __init__(self):
  25. self.substitute_strings = {}
  26. self.local_sub_strings = {}
  27. def add_pair(self, tag, replace_value):
  28. self.substitute_strings[tag] = replace_value
  29. def init_sub_strings(self, app, config):
  30. self.target_name = config.idf_target
  31. self.add_pair("{IDF_TARGET_NAME}", TARGET_NAMES[config.idf_target])
  32. self.add_pair("{IDF_TARGET_PATH_NAME}", config.idf_target)
  33. self.add_pair("{IDF_TARGET_TOOLCHAIN_NAME}", TOOLCHAIN_NAMES[config.idf_target])
  34. self.add_pair("{IDF_TARGET_CFG_PREFIX}", CONFIG_PREFIX[config.idf_target])
  35. self.add_pair("{IDF_TARGET_TRM_EN_URL}", TRM_EN_URL[config.idf_target])
  36. self.add_pair("{IDF_TARGET_TRM_CN_URL}", TRM_CN_URL[config.idf_target])
  37. def add_local_subs(self, matches):
  38. for sub_def in matches:
  39. if len(sub_def) != 2:
  40. raise ValueError("IDF_TARGET_X substitution define invalid, val={}".format(sub_def))
  41. tag = "{" + "IDF_TARGET_{}".format(sub_def[0]) + "}"
  42. match_default = re.match(r'^\s*default(\s*)=(\s*)\"(.*?)\"', sub_def[1])
  43. if match_default is None:
  44. # There should always be a default value
  45. raise ValueError("No default value in IDF_TARGET_X substitution define, val={}".format(sub_def))
  46. match_target = re.match(r'^.*{}(\s*)=(\s*)\"(.*?)\"'.format(self.target_name), sub_def[1])
  47. if match_target is None:
  48. sub_value = match_default.groups()[2]
  49. else:
  50. sub_value = match_target.groups()[2]
  51. self.local_sub_strings[tag] = sub_value
  52. def substitute(self, app, docname, source):
  53. # Add any new local tags that matches the reg.ex.
  54. sub_defs = re.findall(self.RE_PATTERN, source[0])
  55. if len(sub_defs) != 0:
  56. self.add_local_subs(sub_defs)
  57. # Remove the tag defines
  58. source[0] = re.sub(self.RE_PATTERN,'', source[0])
  59. for key in self.local_sub_strings:
  60. source[0] = source[0].replace(key, self.local_sub_strings[key])
  61. self.local_sub_strings = {}
  62. for key in self.substitute_strings:
  63. source[0] = source[0].replace(key, self.substitute_strings[key])
  64. def setup(app):
  65. sub = StringSubstituter()
  66. # Config values not available when setup is called
  67. app.connect('config-inited', sub.init_sub_strings)
  68. app.connect('source-read', sub.substitute)
  69. return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}