My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mc-apply.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. #
  3. # Create a Configuration from marlin_config.json
  4. #
  5. import json
  6. import sys
  7. import shutil
  8. opt_output = '--opt' in sys.argv
  9. output_suffix = '.sh' if opt_output else '' if '--bare-output' in sys.argv else '.gen'
  10. try:
  11. with open('marlin_config.json', 'r') as infile:
  12. conf = json.load(infile)
  13. for key in conf:
  14. # We don't care about the hash when restoring here
  15. if key == '__INITIAL_HASH':
  16. continue
  17. if key == 'VERSION':
  18. for k, v in sorted(conf[key].items()):
  19. print(k + ': ' + v)
  20. continue
  21. # The key is the file name, so let's build it now
  22. outfile = open('Marlin/' + key + output_suffix, 'w')
  23. for k, v in sorted(conf[key].items()):
  24. # Make define line now
  25. if opt_output:
  26. if v != '':
  27. if '"' in v:
  28. v = "'%s'" % v
  29. elif ' ' in v:
  30. v = '"%s"' % v
  31. define = 'opt_set ' + k + ' ' + v + '\n'
  32. else:
  33. define = 'opt_enable ' + k + '\n'
  34. else:
  35. define = '#define ' + k + ' ' + v + '\n'
  36. outfile.write(define)
  37. outfile.close()
  38. # Try to apply changes to the actual configuration file (in order to keep useful comments)
  39. if output_suffix != '':
  40. # Move the existing configuration so it doesn't interfere
  41. shutil.move('Marlin/' + key, 'Marlin/' + key + '.orig')
  42. infile_lines = open('Marlin/' + key + '.orig', 'r').read().split('\n')
  43. outfile = open('Marlin/' + key, 'w')
  44. for line in infile_lines:
  45. sline = line.strip(" \t\n\r")
  46. if sline[:7] == "#define":
  47. # Extract the key here (we don't care about the value)
  48. kv = sline[8:].strip().split(' ')
  49. if kv[0] in conf[key]:
  50. outfile.write('#define ' + kv[0] + ' ' + conf[key][kv[0]] + '\n')
  51. # Remove the key from the dict, so we can still write all missing keys at the end of the file
  52. del conf[key][kv[0]]
  53. else:
  54. outfile.write(line + '\n')
  55. else:
  56. outfile.write(line + '\n')
  57. # Process any remaining defines here
  58. for k, v in sorted(conf[key].items()):
  59. define = '#define ' + k + ' ' + v + '\n'
  60. outfile.write(define)
  61. outfile.close()
  62. print('Output configuration written to: ' + 'Marlin/' + key + output_suffix)
  63. except:
  64. print('No marlin_config.json found.')