convert.py 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. if len(sys.argv) == 1:
  5. print("I need a config.py (filename) to convert.")
  6. sys.exit(2)
  7. filename = sys.argv[1]
  8. print("Processing:", filename)
  9. yamlout = filename
  10. pos = yamlout.rindex('.')
  11. if pos != -1:
  12. yamlout = yamlout[0:pos+1] + "yaml"
  13. else:
  14. print("I can't determine the extension.", filename)
  15. sys.exit(2)
  16. if os.path.exists(yamlout):
  17. print("Sorry, the file {0} already exists.".format(yamlout))
  18. sys.exit(2)
  19. print("Converting {0} to {1}...".format(filename, yamlout))
  20. with open(filename, 'r') as fp:
  21. with open(yamlout, 'w') as fpout:
  22. for line in fp:
  23. line = line.strip()
  24. if " = " in line:
  25. var, _, data = line.partition('=')
  26. var = var.strip().lower()
  27. line = "{0}: {1}".format(var, data.strip())
  28. print("{0}".format(line), file=fpout)
  29. print("Done")