My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

configuration.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #
  2. # configuration.py
  3. # Apply options from config.ini to the existing Configuration headers
  4. #
  5. import re, shutil, configparser
  6. from pathlib import Path
  7. verbose = 0
  8. def blab(str,level=1):
  9. if verbose >= level: print(f"[config] {str}")
  10. def config_path(cpath):
  11. return Path("Marlin", cpath, encoding='utf-8')
  12. # Apply a single name = on/off ; name = value ; etc.
  13. # TODO: Limit to the given (optional) configuration
  14. def apply_opt(name, val, conf=None):
  15. if name == "lcd": name, val = val, "on"
  16. # Create a regex to match the option and capture parts of the line
  17. regex = re.compile(rf'^(\s*)(//\s*)?(#define\s+)({name}\b)(\s*)(.*?)(\s*)(//.*)?$', re.IGNORECASE)
  18. # Find and enable and/or update all matches
  19. for file in ("Configuration.h", "Configuration_adv.h"):
  20. fullpath = config_path(file)
  21. lines = fullpath.read_text(encoding='utf-8').split('\n')
  22. found = False
  23. for i in range(len(lines)):
  24. line = lines[i]
  25. match = regex.match(line)
  26. if match and match[4].upper() == name.upper():
  27. found = True
  28. # For boolean options un/comment the define
  29. if val in ("on", "", None):
  30. newline = re.sub(r'^(\s*)//+\s*(#define)(\s{1,3})?(\s*)', r'\1\2 \4', line)
  31. elif val == "off":
  32. newline = re.sub(r'^(\s*)(#define)(\s{1,3})?(\s*)', r'\1//\2 \4', line)
  33. else:
  34. # For options with values, enable and set the value
  35. newline = match[1] + match[3] + match[4] + match[5] + val
  36. if match[8]:
  37. sp = match[7] if match[7] else ' '
  38. newline += sp + match[8]
  39. lines[i] = newline
  40. blab(f"Set {name} to {val}")
  41. # If the option was found, write the modified lines
  42. if found:
  43. fullpath.write_text('\n'.join(lines), encoding='utf-8')
  44. break
  45. # If the option didn't appear in either config file, add it
  46. if not found:
  47. # OFF options are added as disabled items so they appear
  48. # in config dumps. Useful for custom settings.
  49. prefix = ""
  50. if val == "off":
  51. prefix, val = "//", "" # Item doesn't appear in config dump
  52. #val = "false" # Item appears in config dump
  53. # Uppercase the option unless already mixed/uppercase
  54. added = name.upper() if name.islower() else name
  55. # Add the provided value after the name
  56. if val != "on" and val != "" and val is not None:
  57. added += " " + val
  58. # Prepend the new option after the first set of #define lines
  59. fullpath = config_path("Configuration.h")
  60. with fullpath.open(encoding='utf-8') as f:
  61. lines = f.readlines()
  62. linenum = 0
  63. gotdef = False
  64. for line in lines:
  65. isdef = line.startswith("#define")
  66. if not gotdef:
  67. gotdef = isdef
  68. elif not isdef:
  69. break
  70. linenum += 1
  71. lines.insert(linenum, f"{prefix}#define {added:30} // Added by config.ini\n")
  72. fullpath.write_text(''.join(lines), encoding='utf-8')
  73. # Fetch configuration files from GitHub given the path.
  74. # Return True if any files were fetched.
  75. def fetch_example(url):
  76. if url.endswith("/"): url = url[:-1]
  77. if url.startswith('http'):
  78. url = url.replace("%", "%25").replace(" ", "%20")
  79. else:
  80. brch = "bugfix-2.1.x"
  81. if '@' in path: path, brch = map(str.strip, path.split('@'))
  82. url = f"https://raw.githubusercontent.com/MarlinFirmware/Configurations/{brch}/config/{url}"
  83. # Find a suitable fetch command
  84. if shutil.which("curl") is not None:
  85. fetch = "curl -L -s -S -f -o"
  86. elif shutil.which("wget") is not None:
  87. fetch = "wget -q -O"
  88. else:
  89. blab("Couldn't find curl or wget", -1)
  90. return False
  91. import os
  92. # Reset configurations to default
  93. os.system("git reset --hard HEAD")
  94. # Try to fetch the remote files
  95. gotfile = False
  96. for fn in ("Configuration.h", "Configuration_adv.h", "_Bootscreen.h", "_Statusscreen.h"):
  97. if os.system(f"{fetch} wgot {url}/{fn} >/dev/null 2>&1") == 0:
  98. shutil.move('wgot', config_path(fn))
  99. gotfile = True
  100. if Path('wgot').exists(): shutil.rmtree('wgot')
  101. return gotfile
  102. def section_items(cp, sectkey):
  103. return cp.items(sectkey) if sectkey in cp.sections() else []
  104. # Apply all items from a config section
  105. def apply_ini_by_name(cp, sect):
  106. iniok = True
  107. if sect in ('config:base', 'config:root'):
  108. iniok = False
  109. items = section_items(cp, 'config:base') + section_items(cp, 'config:root')
  110. else:
  111. items = section_items(cp, sect)
  112. for item in items:
  113. if iniok or not item[0].startswith('ini_'):
  114. apply_opt(item[0], item[1])
  115. # Apply all config sections from a parsed file
  116. def apply_all_sections(cp):
  117. for sect in cp.sections():
  118. if sect.startswith('config:'):
  119. apply_ini_by_name(cp, sect)
  120. # Apply certain config sections from a parsed file
  121. def apply_sections(cp, ckey='all'):
  122. blab(f"Apply section key: {ckey}")
  123. if ckey == 'all':
  124. apply_all_sections(cp)
  125. else:
  126. # Apply the base/root config.ini settings after external files are done
  127. if ckey in ('base', 'root'):
  128. apply_ini_by_name(cp, 'config:base')
  129. # Apply historically 'Configuration.h' settings everywhere
  130. if ckey == 'basic':
  131. apply_ini_by_name(cp, 'config:basic')
  132. # Apply historically Configuration_adv.h settings everywhere
  133. # (Some of which rely on defines in 'Conditionals_LCD.h')
  134. elif ckey in ('adv', 'advanced'):
  135. apply_ini_by_name(cp, 'config:advanced')
  136. # Apply a specific config:<name> section directly
  137. elif ckey.startswith('config:'):
  138. apply_ini_by_name(cp, ckey)
  139. # Apply settings from a top level config.ini
  140. def apply_config_ini(cp):
  141. blab("=" * 20 + " Gather 'config.ini' entries...")
  142. # Pre-scan for ini_use_config to get config_keys
  143. base_items = section_items(cp, 'config:base') + section_items(cp, 'config:root')
  144. config_keys = ['base']
  145. for ikey, ival in base_items:
  146. if ikey == 'ini_use_config':
  147. config_keys = map(str.strip, ival.split(','))
  148. # For each ini_use_config item perform an action
  149. for ckey in config_keys:
  150. addbase = False
  151. # For a key ending in .ini load and parse another .ini file
  152. if ckey.endswith('.ini'):
  153. sect = 'base'
  154. if '@' in ckey: sect, ckey = ckey.split('@')
  155. other_ini = configparser.ConfigParser()
  156. other_ini.read(config_path(ckey))
  157. apply_sections(other_ini, sect)
  158. # (Allow 'example/' as a shortcut for 'examples/')
  159. elif ckey.startswith('example/'):
  160. ckey = 'examples' + ckey[7:]
  161. # For 'examples/<path>' fetch an example set from GitHub.
  162. # For https?:// do a direct fetch of the URL.
  163. elif ckey.startswith('examples/') or ckey.startswith('http'):
  164. fetch_example(ckey)
  165. ckey = 'base'
  166. elif ckey == 'all':
  167. apply_sections(cp)
  168. else:
  169. # Apply keyed sections after external files are done
  170. apply_sections(cp, 'config:' + ckey)
  171. if __name__ == "__main__":
  172. #
  173. # From command line use the given file name
  174. #
  175. import sys
  176. args = sys.argv[1:]
  177. if len(args) > 0:
  178. if args[0].endswith('.ini'):
  179. ini_file = args[0]
  180. else:
  181. print("Usage: %s <.ini file>" % sys.argv[0])
  182. else:
  183. ini_file = config_path('config.ini')
  184. if ini_file:
  185. user_ini = configparser.ConfigParser()
  186. user_ini.read(ini_file)
  187. apply_config_ini(user_ini)
  188. else:
  189. #
  190. # From within PlatformIO use the loaded INI file
  191. #
  192. import pioutil
  193. if pioutil.is_pio_build():
  194. Import("env")
  195. try:
  196. verbose = int(env.GetProjectOption('custom_verbose'))
  197. except:
  198. pass
  199. from platformio.project.config import ProjectConfig
  200. apply_config_ini(ProjectConfig())