config.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. '''
  2. config.py - Helper functions for config manipulation
  3. Make sure both copies always match:
  4. - buildroot/bin/config.py
  5. - buildroot/share/PlatformIO/scripts/config.py
  6. '''
  7. import re
  8. FILES = ('Marlin/Configuration.h', 'Marlin/Configuration_adv.h')
  9. def set(file_path, define_name, value):
  10. '''
  11. Replaces a define in a file with a new value.
  12. Returns True if the define was found and replaced, False otherwise.
  13. '''
  14. # Read the contents of the file
  15. with open(file_path, 'r') as f:
  16. content = f.readlines()
  17. modified = False
  18. for i in range(len(content)):
  19. # Regex to match the desired pattern
  20. match = re.match(r'^(\s*)(/*)(\s*)(#define\s+{})\s+(.*?)\s*(//.*)?$'.format(re.escape(define_name)), content[i])
  21. if match:
  22. modified = True
  23. comm = '' if match[6] is None else ' ' + match[6]
  24. oldval = '' if match[5] is None else match[5]
  25. if match[2] or value != oldval:
  26. content[i] = f"{match[1]}{match[3]}{match[4]} {value} // {match[5]}{comm}\n"
  27. # Write the modified content back to the file only if changes were made
  28. if modified:
  29. with open(file_path, 'w') as f:
  30. f.writelines(content)
  31. return True
  32. return False
  33. def add(file_path, define_name, value=""):
  34. '''
  35. Insert a define on the first blank line in a file.
  36. '''
  37. with open(file_path, 'r') as f:
  38. content = f.readlines()
  39. # Prepend a space to the value if it's not empty
  40. if value != "":
  41. value = " " + value
  42. # Find the first blank line to insert the new define
  43. for i in range(len(content)):
  44. if content[i].strip() == '':
  45. # Insert the define at the first blank line
  46. content.insert(i, f"#define {define_name}{value}\n")
  47. break
  48. else:
  49. # If no blank line is found, append to the end
  50. content.append(f"#define {define_name}{value}\n")
  51. with open(file_path, 'w') as f:
  52. f.writelines(content)
  53. def enable(file_path, define_name, enable=True):
  54. '''
  55. Uncomment or comment the named defines in the given file path.
  56. Returns True if the define was found, False otherwise.
  57. '''
  58. # Read the contents of the file
  59. with open(file_path, 'r') as f:
  60. content = f.readlines()
  61. # Prepare the regex
  62. regex = re.compile(r'^(\s*)(/*)(\s*)(#define\s+{}\b.*?)(\s*//.*)?$'.format(re.escape(define_name)))
  63. # Find the define in the file and uncomment or comment it
  64. found = False
  65. modified = False
  66. for i in range(len(content)):
  67. match = regex.match(content[i])
  68. if not match: continue
  69. found = True
  70. if enable:
  71. if match[2]:
  72. modified = True
  73. comment = '' if match[5] is None else ' ' + match[5]
  74. content[i] = f"{match[1]}{match[3]}{match[4]}{comment}\n"
  75. else:
  76. if not match[2]:
  77. modified = True
  78. comment = '' if match[5] is None else match[5]
  79. if comment.startswith(' '): comment = comment[2:]
  80. content[i] = f"{match[1]}//{match[3]}{match[4]}{comment}\n"
  81. break
  82. # Write the modified content back to the file only if changes were made
  83. if modified:
  84. with open(file_path, 'w') as f:
  85. f.writelines(content)
  86. return found