home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 July / maximum-cd-2010-07.iso / DiscContents / wesnoth-1.8-win32.exe / data / tools / expand-terrain-macros.py < prev    next >
Encoding:
Python Source  |  2009-03-01  |  6.1 KB  |  182 lines

  1. #!/usr/bin/env python
  2.  
  3. #  expand-terrain-macros.py - Expand "meta-macros" for terrain WML
  4. #
  5. #  Copyright (C) 2008 - 2009 by Moritz Goebelbecker
  6. #  Part of the Battle for Wesnoth Project http://www.wesnoth.org
  7. #
  8. #  This program is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License version 2
  10. #  or at your option any later version.
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY.
  13. #
  14. #  See the COPYING file for more details.
  15.  
  16.  
  17. #
  18. #  Meta-Macro syntax:
  19. #  #meta-macro BASENAME [{NORMAL_PARAM, OPTIONAL_PARAM} [...]]
  20. #
  21. #  NORMAL_PARAM: Macro parameter that will be passed unmodified to the base macro
  22. #  OPTIONAL_PARAM: Macro parameter that will sometimes be passed to the base macro
  23. #                  and sometimes be replaced with a default value
  24. #                  The script will create one macro for each possible combination of
  25. #                  optional parameters
  26. #
  27. #  Syntax:         ABBREV=NAME=DEFAULT
  28. #    ABBREV:       One letter that is appended to macros taking that argument
  29. #    NAME:         Name of the parameter that is used when it's passed to the base macro
  30. #    ABBREV:       Default value that is used when the parameter is not passed to the base macro
  31. #
  32. #
  33. #  !!! ONLY USE THIS IF YOU KNOW WHAT YOU ARE DOING !!!
  34.  
  35. import sys
  36. import getopt
  37.  
  38. def printUsage():
  39.     print "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
  40.     print "Usage: expand-terrain-macros.py [OPTIONS] filename1 [filename2 [...]]"
  41.     print ""
  42.     print "Options:"
  43.     print "  -i  Insert the expanded sections into the input file(s) immediately after"
  44.     print "      their macro definitions."
  45.     print "  -a  Append the expanded sections to the input file(s)"
  46.     print "  -r  Replace the input file(s) with the resulting output. Previously generated"
  47.     print "      expansions will be removed. Implies -i if nothing else is specified."
  48.     print ""
  49.     print "If no options are specified, only the expanded sections will be printed to"
  50.     print "stdout"
  51.  
  52. insert = False
  53. append = False
  54. replace = False
  55.  
  56. try:
  57.     (opts, args) = getopt.getopt(sys.argv[1:], 'iar')
  58. except getopt.GetoptError, e:
  59.     print 'Error parsing command-line arguments: %s' % e
  60.     printUsage()
  61.     sys.exit(1)
  62. for (option, parameter) in opts:
  63.     if option == '-i':
  64.         insert = True
  65.     if option == '-a':
  66.         append = True
  67.     if option == '-r':
  68.         replace = True
  69.  
  70. if replace and not append:
  71.     insert = True
  72.  
  73. if insert and append:
  74.     print "Error: cannot use -i and -a at the same time"
  75.     printUsage()
  76.     sys.exit(1)
  77.     
  78.  
  79. if len(args) == 0:
  80.     printUsage()
  81.     sys.exit(1)
  82.  
  83. for filename in args:
  84.     f = file(filename)
  85.     content = f.readlines()
  86.     f.close()
  87.  
  88.     changed = False
  89.     output=[]
  90.     appended=[]
  91.     
  92.     autogenerated = False
  93.     for line in content:
  94.         if line.strip() == "#The following code is autogenerated by expand-terrain-macros.py":
  95.             autogenerated = True
  96.  
  97.         if (insert or append)  and not autogenerated:
  98.             output.append(line.rstrip("\n"))
  99.  
  100.         if line.strip() == "#end of generated code":
  101.             autogenerated = False
  102.  
  103.         if line.startswith('#meta-macro'):
  104.             elems = line[12:].strip().split()
  105.             basename = elems[0]
  106.             params= []
  107.             optional_params=[]
  108.             
  109.             for param in elems[1:]:
  110.                 split_param = param.split('=')
  111.                 if len(split_param) == 3:
  112.                     optional_params.append(split_param[0])
  113.                 elif len(split_param) != 1:
  114.                     print "Error in line:\n" + line
  115.                     sys.exit(1)
  116.                     
  117.                 params.append(split_param)
  118.  
  119.             base_macro_suffix = "_" + "".join(optional_params)
  120.  
  121.             result=[]
  122.             result.append("#The following code is autogenerated by expand-terrain-macros.py")
  123.             if append:
  124.                 result.append("#generated from: " + line.strip())
  125.             result.append("#Please do not modify")
  126.             
  127.             for i in range(2**len(optional_params) - 2, -1, -1):
  128.                 enabled_map = dict([(param, i & (1<<index) != 0)  for index, param in enumerate(optional_params)])
  129.  
  130.                 suffix =""
  131.                 params_external = []
  132.                 params_internal = []
  133.  
  134.                 for param in params:
  135.                     #normal parameter
  136.                     if len(param) == 1:
  137.                         params_external.append(param[0])
  138.                         params_internal.append('({'+param[0]+'})')
  139.                     else:
  140.                         #enabled optional parameter
  141.                         if enabled_map[param[0]]:
  142.                             suffix += param[0]
  143.                             params_external.append(param[1])
  144.                             params_internal.append('({'+param[1]+'})')
  145.                         else:
  146.                             params_internal.append(param[2])
  147.  
  148.                 if len(suffix) > 0:
  149.                     suffix = "_"+suffix
  150.                             
  151.                 result.append("#define " + basename + suffix + " " + " ".join(params_external))
  152.                 result.append("    {" + basename + base_macro_suffix + " "  + " ".join(params_internal) + "}")
  153.                 result.append("#enddef")
  154.                 
  155.             result.append("#end of generated code")
  156.             changed = True
  157.  
  158.             if insert:
  159.                 output += result
  160.             elif append:
  161.                 appended += result
  162.             else:
  163.                 for r in result:
  164.                     print r
  165.  
  166.     if (insert or append) and not replace:
  167.         for line in output:
  168.             print line
  169.         if append:
  170.             for line in appended:
  171.                 print line
  172.                 
  173.     elif replace and changed:
  174.         f = open(filename, 'w')
  175.         for line in output:
  176.             f.write(line+"\n")
  177.         if append:
  178.             for line in appended:
  179.                 f.write(line+"\n")
  180.         f.close()
  181.