Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # This file is a part of Netacka 3D.
- #
- # Copyright 2010
- # Leszek Bartkowski, Mikołaj Dądela, Paweł Marczewski, Jan Szejko
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import sys
- import re
- class Ignore(Exception):
- pass
- def replace_symbol(pattern, replacement, text):
- return re.sub(r"\b" + pattern + r"\b", replacement, text)
- def preprocess_line(line):
- global trans
- for f in trans: line = f(line)
- indent = re.match("^(\s*).*?$", line).group(1)
- words = tuple(line.split())
- if not words: return ""
- prettify = lambda ws: indent + " ".join(ws)
- # enabling/disablnig lines of code
- if r"//#." in line:
- if words[0].startswith("//#."):
- return prettify(words[1:] + (words[0],))
- if words[-1].startswith("//#."):
- return prettify((words[-1],) + words[:-1])
- raise Exception("tag should be placed on beginning or end of line")
- # commands
- if words[0].startswith(r"//#"):
- cmd = words[0][3:]
- if cmd == "autogenerated":
- raise Exception("this file is autogenerated, preprocessing it is dangerous!")
- if cmd == "ignore":
- raise Ignore
- if cmd == "replace":
- trans.append(lambda s: replace_symbol(words[1], words[2], s))
- return prettify(("// replaced:",) + words[1:])
- raise Exception("unknown command: "+words[0])
- return line
- def preprocess(str):
- global trans; trans = []
- return "//#autogenerated\n\n" + "\n".join(map(preprocess_line, str.splitlines()))+"\n"
- def preprocess_file(infile, outfile):
- try:
- infileH = open(infile, 'r')
- result = preprocess(infileH.read())
- outfileH = open(outfile, 'w')
- outfileH.write(result)
- except Ignore:
- pass
- def main():
- if len(sys.argv) != 3:
- test()
- print("Usage: python preprocessor.py infile outfile")
- sys.exit()
- preprocess_file(sys.argv[1], sys.argv[2])
- def test():
- assert(replace_symbol("GL","GL10","gl.GL2(GL10);") == 'gl.GL2(GL10);')
- assert(replace_symbol("GL","GL10","GL.foo()") == 'GL10.foo()')
- assert(preprocess("//#replace a b \n\t a(c);\n").endswith("\n\t b(c);\n"))
- assert(preprocess("\t //#.foo bar\n").endswith('\t bar //#.foo\n'))
- assert(preprocess(" bar //#.foo\n").endswith(' //#.foo bar\n'))
- try: preprocess("foo //#.bar baz\n"); assert(False)
- except: pass
- try: preprocess("//#autogenerated"); assert(False)
- except: pass
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement