Advertisement
Guest User

Untitled

a guest
Mar 26th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # This file is a part of Netacka 3D.
  4. #
  5. # Copyright 2010
  6. # Leszek Bartkowski, Mikołaj Dądela, Paweł Marczewski, Jan Szejko
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. #     http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19.  
  20.  
  21. import sys
  22. import re
  23.  
  24. class Ignore(Exception):
  25.     pass
  26.  
  27. def replace_symbol(pattern, replacement, text):
  28.     return re.sub(r"\b" + pattern + r"\b", replacement, text)
  29.  
  30. def preprocess_line(line):
  31.     global trans
  32.  
  33.     for f in trans: line = f(line)
  34.     indent = re.match("^(\s*).*?$", line).group(1)
  35.     words = tuple(line.split())
  36.     if not words: return ""
  37.  
  38.     prettify = lambda ws: indent + " ".join(ws)
  39.  
  40.     # enabling/disablnig lines of code
  41.     if r"//#." in line:
  42.         if words[0].startswith("//#."):
  43.             return prettify(words[1:] + (words[0],))
  44.         if words[-1].startswith("//#."):
  45.             return prettify((words[-1],) + words[:-1])
  46.         raise Exception("tag should be placed on beginning or end of line")
  47.  
  48.     # commands
  49.     if words[0].startswith(r"//#"):
  50.         cmd = words[0][3:]
  51.         if cmd == "autogenerated":
  52.             raise Exception("this file is autogenerated, preprocessing it is dangerous!")
  53.         if cmd == "ignore":
  54.             raise Ignore
  55.         if cmd == "replace":
  56.             trans.append(lambda s: replace_symbol(words[1], words[2], s))
  57.             return prettify(("// replaced:",) + words[1:])
  58.         raise Exception("unknown command: "+words[0])
  59.     return line
  60.  
  61. def preprocess(str):
  62.     global trans; trans = []
  63.     return "//#autogenerated\n\n" + "\n".join(map(preprocess_line, str.splitlines()))+"\n"
  64.  
  65. def preprocess_file(infile, outfile):
  66.     try:
  67.         infileH = open(infile, 'r')
  68.         result = preprocess(infileH.read())
  69.         outfileH = open(outfile, 'w')
  70.         outfileH.write(result)
  71.     except Ignore:
  72.         pass
  73.  
  74. def main():
  75.     if len(sys.argv) != 3:
  76.         test()
  77.         print("Usage: python preprocessor.py infile outfile")
  78.         sys.exit()
  79.     preprocess_file(sys.argv[1], sys.argv[2])
  80.  
  81.  
  82. def test():
  83.     assert(replace_symbol("GL","GL10","gl.GL2(GL10);") == 'gl.GL2(GL10);')
  84.     assert(replace_symbol("GL","GL10","GL.foo()") == 'GL10.foo()')
  85.  
  86.     assert(preprocess("//#replace a b \n\t a(c);\n").endswith("\n\t b(c);\n"))
  87.  
  88.     assert(preprocess("\t //#.foo bar\n").endswith('\t bar //#.foo\n'))
  89.     assert(preprocess("   bar //#.foo\n").endswith('   //#.foo bar\n'))
  90.     try: preprocess("foo //#.bar baz\n"); assert(False)
  91.     except: pass
  92.  
  93.     try: preprocess("//#autogenerated"); assert(False)
  94.     except: pass
  95.  
  96.  
  97. if __name__ == "__main__":
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement