create_material.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python3
  2. import optparse
  3. def main():
  4. # Command line args
  5. parser = optparse.OptionParser("usage: %prog [options]")
  6. parser.add_option("-t", "--template", dest="template",
  7. type="string", help="specify material template")
  8. parser.add_option("-o", "--output", dest="out",
  9. type="string", help="specify the output filename")
  10. parser.add_option("-v", "--vars", dest="vars",
  11. type="string", help="specify the variables to replace. "
  12. "Format var:val,var1:val1")
  13. (options, args) = parser.parse_args()
  14. if not options.template or not options.out:
  15. parser.error("argument is missing")
  16. # Open template
  17. ftempl = open(options.template, "r")
  18. templ = ftempl.read()
  19. ftempl.close()
  20. # Parse vars
  21. if options.vars:
  22. varvals = options.vars.split(",")
  23. for varval in varvals:
  24. (var, val) = varval.split(":")
  25. print("-- Replacing %%%s%% with %s" % (var, val))
  26. templ = templ.replace("%" + var + "%", val)
  27. # Write out file
  28. fout = open(options.out, "w")
  29. fout.write(templ)
  30. fout.close()
  31. if __name__ == "__main__":
  32. main()