config.py 3.1 KB

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